$(document).ready(function(){
	$("#vp_hq").hide();
	$("#standard_quality").hide();
	var high_quality_content = $("#vp_hq").html();
	var standard_quality_content = $("#video_player").html();
	$("#high_quality").click(function(){
		$("#video_player").html("");		
		$("#vp_hq").html(high_quality_content);
		$("#vp_hq").show();		
		$("#video_player").hide();		
		$("#share_cs").hide();
		$("#tg_share").html("<img src=\"../common_images/down_arrow.png\" alt=\"\" />");
		$("#send_cs").hide();
		$("#tg_send").html("<img src=\"../common_images/down_arrow.png\" alt=\"\" />");
		$("#downloads_cs").hide();
		$("#tg_downloads").html("<img src=\"../common_images/down_arrow.png\" alt=\"\" />");		
		$("#subscribe_cs").hide();
		$("#tg_subscribe").html("<img src=\"../common_images/down_arrow.png\" alt=\"\" />");
		$("#about_show_cs").hide();
		$("#tg_about").html("<img src=\"../common_images/down_arrow.png\" alt=\"\" />");
		$("#episode_guide_cs").hide();
		$("#tg_episodes").html("<img src=\"../common_images/down_arrow.png\" alt=\"\" />");		
		$("#high_quality").hide();
		$("#standard_quality").show();
		return false;		
	});
	$("#standard_quality").click(function(){
		$("#vp_hq").html("");		
		$("#video_player").html(standard_quality_content);
		$("#video_player").show();
		$("#vp_hq").hide();		
		$("#subscribe_cs").show();
		$("#tg_subscribe").html("<img src=\"../common_images/up_arrow.png\" alt=\"\" />");
		$("#about_show_cs").show();
		$("#tg_about").html("<img src=\"../common_images/up_arrow.png\" alt=\"\" />");
		$("#episode_guide_cs").show();
		$("#tg_episodes").html("<img src=\"../common_images/up_arrow.png\" alt=\"\" />");		
		$("#standard_quality").hide();
		$("#high_quality").show();
		return false;		
	});
	$("#access_gmail").click(function(){
		window.open("../show_inc/gmail_select_email.php?url="+$("#page_url").val(),"newwindow","height=700,width=750");
		return false;
	});
	//comment field stored in a variable
	var comment_field = "<div class=\"bright_warning\" id=\"com_status_message\"></div>"
						+"<div id=\"com_post_comment\" class=\"com_post_comment\">"
						+"<h3>Leave Us a Comment..</h3>"
						+"<form action=\"\" method=\"get\">"
						+"<input type=\"hidden\" value=\"0\" id=\"com_hidden_subcomment\" />"
						+"<input type=\"hidden\" value=\"\" id=\"com_hidden_subject\" />"
						+"<div><label class=\"sd_name_txt\">Name</label></div>"
						+"<div><input type=\"text\" id=\"com_enter_name\" /></div>"
						+"<div><label class=\"sd_name_txt\">Subject</label></div>"
						+"<div><input type=\"text\" id=\"com_enter_subject\" /></div>"					
						+"<div><label class=\"sd_name_txt\">Comment</label></div>"
						+"<div><textarea rows=\"\" cols=\"\" id=\"com_enter_comment\"></textarea></div>"					
						+"</form>"
						+"</div>";
	//insert the comment_field in its default location
	$("#comment_form_default").html(comment_field).show();
	
	//handle place new comment click
	$("#place_new_comment").click(function(){
		//remove all instances of the comment form
		$(".comment_form").fadeOut("slow").empty();
		$(".com_submit_comment").hide();
		//insert the comment form back at the bottom
		$("#comment_form_default").html(comment_field).fadeIn("slow");
		$("#com_submit_comment").fadeIn("slow");
		$("#place_new_comment_sur").hide();		
		return false;
	});
	
	//handle click event when a user submits a comment.
	$(".com_submit_comment").click(function(){
		if($("#com_enter_name").val()!="") {
			name = $("#com_enter_name").val();
		} else {
			name = "Anonymous";
		}
		subcomment = 0;
		//alert("name:"+name);
		if($("#com_hidden_subcomment").val()!=0) {
			subcomment = $("#com_hidden_subcomment").val();
			//alert("subcomment:"+subcomment);
		}
		if($("#com_enter_subject").attr("disabled")=="disabled") {
			subject = $("#com_hidden_subject").val();
			//alert("subject:"+subject);
		} else {
			subject = $("#com_enter_subject").val();
			//alert("subject:"+subject);
		}
		comment = $("#com_enter_comment").val();
		//alert("comment:"+comment);		
		if((subject!="") && (comment!="")){
			//alert("inside second function");			
			$.get("../show_inc/post_comment.php",{ep: $("#page_episode_id").val(),nm: name,sc: subcomment,sj: subject,cm: comment},
				function(data){
					//alert(data);
					if(data=="true"){
						$("#com_status_message").html("Success! Your comment is now under review, <br />please check back soon.").fadeIn("slow");
						$("#com_post_comment").fadeOut("slow");
						$(".com_submit_comment").fadeOut("slow");
					} else {
						//alert("false");
						$("#com_status_message").html("Oops! The system had a bit of a problem. Please try again.").fadeIn("slow");
					}
				}
			,"text");
		} else {
			$("#com_status_message").html("Oops! Please make sure you fill in all the fields <br />(name, subject and comment).").fadeIn("slow");
		}
		return false;
	});
	
	//handle when someone clicks "reply"	
	$(".com_post_reply").click(function(){
		//remove all instances of the comment form
		$(".comment_form").fadeOut("slow").empty();
		$(".com_submit_comment").hide();
		//fade in message to replace new comment box which was just faded out.
		$("#place_new_comment_sur").fadeIn("slow");		
		//create a new instance of the comment form in the current location		
		$(this).parent().parent().siblings(".comment_form").html(comment_field).fadeIn("slow");
		//show the correct submit button
		$(this).parent().parent().siblings().children(".com_submit_comment").fadeIn("slow");
		//copy the hidden value from the comment to the form.
		$("#com_hidden_subcomment").val($(this).parent().siblings('input').val());		
		$("#com_enter_subject").val("RE: "+$(this).parent().siblings('h2').html()).attr("disabled","disabled");
		$("#com_hidden_subject").val("RE: "+$(this).parent().siblings('h2').html());	
		return false; 
	}); 
	
	$("#sh_embed").click(function(){
		$("#sh_embed").select();
	});
	$("#sh_url").val($("#page_url").val());
	$("#sh_url").click(function(){
		$("#sh_url").select();
	});
	$("#image_gallery").hide();
   	$("#comments").hide();
   	$("#show_notes_link").click(function(){
   		$("#image_gallery").hide();
   		$("#image_gallery_btn").removeClass("links_foreground").addClass("links_background");
   		$("#comments").hide();
   		$("#comments_btn").removeClass("links_foreground").addClass("links_background");
   		$("#show_notes").show();
   		$("#show_notes_btn").removeClass("links_background").addClass("links_foreground");
   		return false;
   	});
   	$("#image_gallery_link").click(function(){
   		$("#image_gallery").show();
   		$("#image_gallery_btn").removeClass("links_background").addClass("links_foreground");
   		$("#comments").hide();
   		$("#comments_btn").removeClass("links_foreground").addClass("links_background");
   		$("#show_notes").hide();
   		$("#show_notes_btn").removeClass("links_foreground").addClass("links_background");
   		return false;
   	});
   	$("#comments_link").click(function(){
   		$("#image_gallery").hide();
   		$("#image_gallery_btn").removeClass("links_foreground").addClass("links_background");
   		$("#comments").show();
   		$("#comments_btn").removeClass("links_background").addClass("links_foreground");
   		$("#show_notes").hide();
   		$("#show_notes_btn").removeClass("links_foreground").addClass("links_background");
   		return false;
   	});
   	$("#subscribe_cs").hide();
	$("#tg_subscribe").html("<img src=\"../common_images/down_arrow.png\" alt=\"\" />");
	$("#share_cs").hide();
	$("#tg_share").html("<img src=\"../common_images/down_arrow.png\" alt=\"\" />");
	$("#send_cs").hide();
	$("#tg_send").html("<img src=\"../common_images/down_arrow.png\" alt=\"\" />");
	$("#become_fan_cs").css("position","absolute").css("left","-10000px");
	$("#tg_become_fan").html("<img src=\"../common_images/down_arrow.png\" alt=\"\" />");
	$("#about_show_cs").hide();
	$("#tg_about").html("<img src=\"../common_images/down_arrow.png\" alt=\"\" />");
	$("#downloads_cs").hide();
	$("#tg_downloads").html("<img src=\"../common_images/down_arrow.png\" alt=\"\" />");
	
	$(".toggle_view").click(function() {		
		if ($(this).parent().siblings('div').is(':visible')) { 
			$(this).html("<img alt=\"\" src=\"../common_images/down_arrow.png\" />");
			$(this).parent().siblings("div").hide();
		} else {
			$(this).html("<img alt=\"\" src=\"../common_images/up_arrow.png\" />");
			$(this).parent().siblings("div").show();
		}		
		return false;
	});
	$(".toggle_height").click(function() {		
		if ($("#become_fan_cs").css("position")=="relative") { 
			$("#tg_become_fan").html("<img alt=\"\" src=\"../common_images/down_arrow.png\" />");
			$("#become_fan_cs").css("position","absolute").css("left","-10000px");
		} else {
			$(this).html("<img alt=\"\" src=\"../common_images/up_arrow.png\" />");
			$("#become_fan_cs").css("position","relative").css("left","0px");
		}		
		return false;
	});
	//social bookmarks
	$("#facebook_link").click(function(){
		window.open('http://facebook.com/sharer.php?u='+escape($("#page_url").val()));
		return false;
	});
	
	$("#digg_link").click(function(){
		window.open('http://digg.com/submit?url='+encodeURIComponent($("#page_url").val())
		+'&media=video&bodytext='+encodeURIComponent($("#ep_short_desc").html())+'&topic=television&title='+encodeURIComponent("NUTV - "+$("#vp_title").html()));
		return false;
	});
	$("#delicious_link").click(function(){
		window.open('http://del.icio.us/post?v=2&url='+encodeURIComponent($("#page_url").val())
		+'&notes='+encodeURIComponent($("#ep_short_desc").html())+'&tags=nutv nutv.ca television&title='+encodeURIComponent("NUTV - "+$("#vp_title").html()));
		return false;
	});
	$("#stumbleupon_link").click(function(){
		window.open('http://www.stumbleupon.com/submit?url='+encodeURIComponent($("#page_url").val())
		+'&title='+encodeURIComponent("NUTV - "+$("#vp_title").html()));
		return false;
	});
	$("#yahoo_link").click(function(){
		window.open('http://myweb2.search.yahoo.com/myresults/bookmarklet?t='+encodeURIComponent("NUTV - "+$("#vp_title").html())
		+'&d='+encodeURIComponent($("#ep_short_desc").html())+'&tag=nutv nutv.ca television&u='+encodeURIComponent($("#page_url").val()));
		return false;
	});
	$("#google_link").click(function(){
		window.open('http://www.google.com/bookmarks/mark?op=add&hl=en&bkmk='+encodeURIComponent($("#page_url").val())
		+'&annotation='+encodeURIComponent($("#ep_short_desc").html())+'&labels=nutv nutv.ca television&title='+encodeURIComponent("NUTV - "+$("#vp_title").html()));
		return false;
	});
	$("#sd_notification").hide();
	$("#sd_email_button").click(function() {
		if($("#sd_friends_email").val()!='') {
			$.get("../show_inc/emailer.php",{email: $("#sd_friends_email").val(), comment: $("#sd_optional_comment").val(), link: $("#page_url").val()},
			function(data){
				if(data == "true") {
					$("#sd_notification").fadeOut('slow');
					$("#sd_notification").html("<p class=\"sd_success_msg\">Email Successfully Sent! Send another?</p>");
					$("#sd_notification").fadeIn('slow');
					window.setTimeout('$("#sd_notification").fadeOut(\'slow\')',10000);
				} else {
					$("#sd_notification").fadeOut('slow');
					$("#sd_notification").html("<p class=\"sd_success_msg\">Oops! The system tried to send"+
					" an email but something went wrong. Please check the email address you entered and try again.</p>");
					$("#sd_notification").fadeIn('slow');
					window.setTimeout('$("#sd_notification").fadeOut(\'slow\')',10000);
				}
			},"text");
		} else {
			$("#sd_notification").fadeOut('slow');
			$("#sd_notification").html("<p class=\"sd_success_msg\">Please Enter a Valid Email Address</p>");
			$("#sd_notification").fadeIn('slow');
			window.setTimeout('$("#sd_notification").fadeOut(\'slow\')',10000);
		}
		return false;
	});
	$(".close_window").click(function(){
		window.opener = self;
		window.close();
		return false;
	});
	$("#rve_send").click(function() {
		if($("#rve_email").val()!='') {
			$.get("../show_inc/emailer.php",{email: $("#rve_email").val(), comment: $("#rve_comment").val(), link: $("#rve_link").html()},
			function(data){
				if(data == "true") {
					$("#rve_email_preview").fadeOut('slow');					
					$("#rve_email_success").fadeIn('slow');
					window.setTimeout('$("#sd_notification").fadeOut(\'slow\')',10000);
				} else {
					$("#rve_email_preview").fadeOut('slow');					
					$("#rve_email_failure").fadeIn('slow');					
				}
			},"text");
		} else {
			$("#rve_email_preview").fadeOut('slow');					
			$("#rve_email_failure").fadeIn('slow');	
		}
		return false;
	});	
 });
