
//##############################################################
//this is the functionality for the submit new topic form
function submit_new_topic(){
	$.ajax({
		url: "index.php",
		type: "POST",
		data: "a=new_topic&new_post_comment=" + $("#new_post_comment").val() + "&new_topic_title=" + $("#new_topic_title").val(),
		dataType: "html",
		complete: function(){},
		success: function(html){
			if(html == "bad"){
				$(".disp_message").remove();
				$("#add_comment_form_topic").prepend("<p class='disp_message' style=\"width: 386px\">Please fill out both the topic title and the comment field.</p>");
			} else {
				//html is returned as the new topic id
				pageload(html);
			}
		},
		error: function(){
			alert("There was an error processing your information.");
			return false;
		}
	});

	return false;
}

//##############################################################
//this is the functionality for the submit new comment form
function submit_new_post(topic_id){
	$.ajax({
		url: "index.php",
		type: "POST",
		data: "a=new_post&new_post_comment=" + $("#new_post_comment").val() + "&topic_id=" + topic_id,
		dataType: "html",
		complete: function(){},
		success: function(html){
			if(html == "bad"){
				$(".disp_message").remove();
				$("#add_comment_form_comment").prepend("<p class='disp_message' style=\"width: 586px;\">Please fill out the entry field.</p>")
			} else {
				$("#new_post_comment").val("");
				alert("Thank you for your post.");
				$("#topic_replys").remove();						//remove post listing
				$("#comment_register_form").remove();				//remove the comment form
				//html is returned as the new topic id
				pageload(html);
			}
		},
		error: function(){
			alert("There was an error processing your information.");
		}
	});
	return false;
}

//##############################################################
//This function uses ajax to get the posts and replaces the forum with the posts
function show_posts(topic_id){
	$("#entire_forum").html('<p id="loading_posts"><img src="images/ajax-loader2.gif" /> Loading Topics...</p>');

	$.ajax({
		url: "index.php",
		type: "POST",
		data: "a=topic_replys&id=" + topic_id,
		dataType: "html",
		complete: function(){},
		success: function(html){
			$("#entire_forum").html(html);
			$("#back_to_forum").click(redisplay_forum);
		},
		error: function(){
			alert("There was an error processing your information.");
		}
	});

	return false;
}

//##############################################################
//remove the posts and the post comment form and places the topics and post topic form back
function redisplay_forum(){
	$.ajax({
		url: "index.php",
		type: "POST",
		data: "a=all_topics",
		dataType: "html",
		complete: function(){},
		success: function(html){
			$("#entire_forum").html(html);
		},
		error: function(){
			alert("There was an error processing your information.");
		}
	});

	return false;
}

//##############################################################
// This function is called when:
// 1. after calling $.historyInit(); 2. after calling $.historyLoad(); 3. after pushing "Go Back" button of a browser
function pageload(hash) {
	// hash doesn't contain the first # character.
	hash = (hash == "video_place_holder") ? null : hash;

	if(hash) {
		//respond page forum history - this brings you back to the posts listing
		if($("body#respond-page").length > 0){
			show_posts(hash);
		}
	} else {
		//respond page forum history - this brings you back to the topic listing
		if($("body#respond-page").length > 0 && $("#related_topics_listing").length < 1){
			redisplay_forum();
		}
	}
}

//##############################################################
//the video player will call this function to update the number of views the video had.
function update_num_views(file_name){
	$.ajax({
		url: "index.php",
		type: "POST",
		data: "a=update_views&file_name=" + file_name,
		dataType: "html",
		complete: function(){
			return false;
		},
		success: function(html){
			return false;
		},
		error: function(){
			return false;
		}
	});
	return false;
}

//##########################################################################################
//THE MEDIA LISTING SWITCHER FUNCTIONS

var listing = "videos";

//##############################################################
function menu_click_allVideos() {
	if(listing == "podcasts"){
		$("#podcast_section_of_media_center").css({ display: "none" });
		$("#media_listing").attr("id", "podcast_media_listing");
		$("#podcasts_media_nav-selected").attr("id", "podcasts_media_nav");
	} else if(listing == "playlists"){
		$("#playlist_section_of_media_center").css({ display: "none" });
		$("#media_listing").attr("id", "playlist_media_listing");
		$("#playlist_media_nav-selected").attr("id", "playlist_media_nav");
	} else {
		return false;
	}

	listing = "videos";

	$("#all_videos_media_nav").attr("id", "all_videos_media_nav-selected");
	$("#video_section_of_media_center").css({ display: "block" });
	$("#all_videos_media_listing").attr("id", "media_listing");

	return false;
}

//##############################################################
function menu_click_playlist() {
		if(listing == "videos"){
			$("#video_section_of_media_center").css({ display: "none" });
			$("#media_listing").attr("id", "all_videos_media_listing");
			$("#all_videos_media_nav-selected").attr("id", "all_videos_media_nav");
		} else 	if(listing == "podcasts"){
			$("#podcast_section_of_media_center").css({ display: "none" });
			$("#media_listing").attr("id", "podcast_media_listing");
			$("#podcasts_media_nav-selected").attr("id", "podcasts_media_nav");
		} else {
			return false;
		}

		listing = "playlists";

		$("#playlist_media_nav").attr("id", "playlist_media_nav-selected");
		$("#playlist_section_of_media_center").css({ display: "block" });
		$("#playlist_media_listing").attr("id", "media_listing");
		return false;
}

//##############################################################
function menu_click_podcasts() {
	if(listing == "videos"){
		$("#video_section_of_media_center").css({ display: "none" });
		$("#media_listing").attr("id", "all_videos_media_listing");
		$("#all_videos_media_nav-selected").attr("id", "all_videos_media_nav");
	} else if(listing == "playlists"){
		$("#playlist_section_of_media_center").css({ display: "none" });
		$("#media_listing").attr("id", "playlist_media_listing");
		$("#playlist_media_nav-selected").attr("id", "playlist_media_nav");
	} else {
		return false;
	}

	listing = "podcasts";

	$("#podcasts_media_nav").attr("id", "podcasts_media_nav-selected");
	$("#podcast_section_of_media_center").css({ display: "block" });
	$("#podcast_media_listing").attr("id", "media_listing");

	return false;
}

//##########################################################################################
//##########################################################################################

$(document).ready(function(){

	// Initialize history plugin.
	// The callback is called at once by present location.hash.
	$.historyInit(pageload);

	// set onlick event for buttons
	$("a[@rel='history']").click(function(){
		//
		var hash = this.href;
		hash = hash.replace(/^.*#/, '');
		// moves to a new page.
		// pageload is called at once.
		$.historyLoad(hash);
		return false;
	});

	//add inplace edit functionality to submit new topic button
	if($("#profile-page").length > 0){
		 $(".visitorInPlaceEditor").editInPlace({
		    url: "index.php?a=update_visitor",
		    bg_over: "#353535",
		    saving_image: "images/ajax-loader.gif"
		});
	}

	//5 star rating system
	if($("ul.star-rating a").length > 0){
		$("ul.star-rating a").click(function(){

			$.ajax({
				url: this.href,
				type: "POST",
				dataType: "html",
				success: function(html){
					$("div#rating_sys").html(html);
				},
				error: function(){
					alert("There was an error processing your information.");
				}

			}); //end of AJAX

			return false;

		}); //end of onclick function
	}

	//add to playlist functionality
	if($("#add_to_playlist").length > 0){
		$("#add_to_playlist").click(function(){

			$.ajax({
				url: this.href,
				type: "POST",
				dataType: "html",
				success: function(html){
					$("#playlist_section_of_media_center").html(html);
					menu_click_playlist();
					$("#add_to_playlist_holder").html("Your playlist has been updated.");
				},
				error: function(){
					alert("There was an error processing your information.");
				}

			}); //end of AJAX

			return false;

		}); //end of onclick function
	}

	//##########################################################################################
	//THE MEDIA LISTING SWITCHER

	//############################
	//CLICK ALL VIDEOS
	$("#all_videos_media_nav, #all_videos_media_nav-selected").click(menu_click_allVideos);

	//############################
	//CLICK PLAYLIST
	$("#playlist_media_nav, #playlist_media_nav-selected").click(menu_click_playlist);

	//############################
	//CLICK PODCASTS
	$("#podcasts_media_nav, #podcasts_media_nav-selected").click(menu_click_podcasts);

	//############################
	//if($(document.body).attr("class") == "mp3-class" && $(document.body).attr("id") != "respond-page"){
	//	menu_click_podcasts();
	//} else if($(document.body).attr("class") == "flv-class" && $(document.body).attr("id") != "respond-page"){
		menu_click_allVideos();
	//}

	/*#############################################################################################
	CHANGE PASSWORD FUNCTIONALITY 																*/

	if($(".change_password_trigger").length > 0){

		$(".change_password_trigger").click(function(){

			var change_password_trigger = $(this);
			var pass_form_holder = $("#change_pass_holder");

			pass_form_holder.load(change_password_trigger.href(), '', function(){

				$("#submit_change_password").click(function(){

					var submit_button = $(this);
					$(this).attr("disabled", "disabled");

					pass_form_holder.append('<img src="./images/ajax-loader.gif" id="loading_icon" />');

					$.ajax({
						url: "index.php?a=handle_change_password",
						type: "POST",
						data: "password=" + $("#password").val() + "&password_confirm=" + $("#password_confirm").val() + "&entry_id=" + change_password_trigger.id(),
						dataType: "html",
						success: function(html){
							pass_form_holder.html(html);
						},
						error: function(){
							pass_form_holder.html("There was an error.");
						}
					});

					//end of onclick submit change password
					return false;

				});

				$("#cancel_change_password").click(function(){
					pass_form_holder.empty();
					return false;
				});

			});

			//end of change password link functionality
			return false;
		});

	}//end if change_password_trigger

	if($("#forgot_pass_link").length > 0){
		$("#forgot_pass_link").click(function(){
			$("div#forgot_pass_form").css("display", "block");
			return false;
		});

		$("#cancel_reset_pass").click(function(){
			$("div#forgot_pass_form").css("display", "none");
			return false;
		});

		$("#send_new_pass").click(function(){
			var submit_button = $(this);
			submit_button.attr('value', "Saving...");

			$.ajax({
				url: "index.php?a=handle_forgot_password",
				type: "POST",
				data: "email=" + $("#email_forgot_pass").val(),
				dataType: "html",
				success: function(html){
					if(html == "bad"){
						submit_button.attr('value', "Send my password");
						$(".disp_message").remove();
						$("div#forgot_pass_form").prepend("<p class='disp_message'>The email address you entered was not found in our database.</p>");
					} else if(html == "good"){
						$("#forgot_pass_link").remove();
						$(".disp_message").remove();
						$("div#forgot_pass_form").html("<p class='disp_message'>Your temporary password was sent to you. Please use it to login and then change it in your profile to something you'll remeber.</p>");
					} else {
						submit_button.attr('value', "Send my password");
						$(".disp_message").remove();
						$("div#forgot_pass_form").html("<p class='disp_message'>There was an error on the server side.</p>");
					}
				},
				error: function(){
					pass_form_holder.html("There was an error.");
				}
			});

			return false;
		});
	}

});