$(document).ready(function(){
	$(".moment_video_link").click(viewVideo);
	$(".moment_desc_link").click(viewDescription);
	$(".moment_view_link").click(viewComments);
	$(".moment_leave_link").click(leaveComment);
	$(".post_comment").submit(postComment);


	function viewVideo(){
		var $this = $(this).parents(".moment");
		$(".moment_description", $this).hide();
		$(".moment_comments", $this).hide();
		$(".moment_leave", $this).hide();
		$(".moment_content", $this).fadeIn('slow');
		return false;
	}
	
	
	function viewDescription(){
		var $this = $(this).parents(".moment");
		$(".moment_content", $this).hide();
		$(".moment_comments", $this).hide();
		$(".moment_leave", $this).hide();
		$(".moment_description", $this).fadeIn('slow');
		return false;
	}
	
	
	function viewComments(){
		var $this = $(this).parents(".moment");
		$(".moment_content", $this).hide();
		$(".moment_description", $this).hide();
		$(".moment_leave", $this).hide();
		$(".moment_comments", $this).fadeIn('slow');
		return false;
	}
	
	
	function leaveComment(){
		var $this = $(this).parents(".moment");
		$(".moment_content", $this).hide();
		$(".moment_description", $this).hide();
		$(".moment_comments", $this).hide();
		$(".moment_leave", $this).fadeIn('slow');
		return false;
	}
	
	
	function reloadComments(itemid){
		$.post(
			"/moments/get_comments.php",
			{
				'itemid' : itemid
			},
			function(data){
				var $this  = $("#moment" + itemid);
				$(".moment_comments", $this).html(data);
				var $count = $(".moment_comments", $this).find("span").length;
				$(".moment_view_link", $this).html("View Comments (" + $count + ")");
				$(".moment_leave", $this).hide();
				$(".moment_comments", $this).fadeIn('slow');
			});
	}
	
	
	function postComment(){
		var comment_id    = $(".comment_itemid", this);
		var comment_name  = $(".comment_name", this);
		var comment_email = $(".comment_email", this);
		var comment_text  = $(".comment_text", this);
		
		if(!comment_name.val() || comment_name.val() == ""){
			alert('You did not fill in your name.');
			comment_name.focus();
			return false;
		}
		
		if(!comment_email.val() || comment_email.val() == ""){
			alert('You did not fill in your email.');
			comment_email.focus();
			return false;
		}
		
		if(!comment_text.val() || comment_text.val() == ""){
			alert('You did not fill in your comment.');
			comment_text.focus();
			return false;
		}
		
		$.post(
			"/moments/post_comment.php",
			{
				'comment_id'    : comment_id.val(),
				'comment_name'  : comment_name.val(),
				'comment_email' : comment_email.val(),
				'comment_text'  : comment_text.val()
			},
			function(data){
				alert(data);
				reloadComments(comment_id.val());
			}
		);
		
		return false;
	}
});