$(function()
{

	// Expandable months.
	$("h3.month").each(
			function(i)
			{
				$(this).addClass("collapsed").bind("click", 
						function(e) 
						{
							$(e.target).toggleClass("collapsed")
									.toggleClass("expanded")
									.next("ul.posts").slideToggle(300);						
							return false;
						});
			});
	
	// Expandable Comments.
	$(".blog h3.comments").each(
			function(i)
			{
				var $self = $(this);
				$self.bind("click", 
						function(e) 
						{
							$self.next("div.comments").slideToggle(300);
							return false;
						});
			});
	
	// Assign the popup to the 'Add Comment' link.
	$("[rel=modal]").each(
			function(i)
			{
				$(this).bind("click",
						function(e)
						{
							$.get($(e.target).attr("href"),
									function(data)
									{
										$("object").hide();
										var $data = $(data);										
										$data.find("input[type=submit]").click(
												function() 
												{ 
													addComment();
													return false; 
												});
										
										$data.modal( 
												{
													closeClass: "close",
													opacity: 50,
													overlayCss: { background: "#993399" },
													onOpen: 
														function (dialog) 
														{
															dialog.overlay.fadeIn('fast', 
																	function () 
																	{
																		dialog.container.slideDown('fast', 
																				function () 
																				{
																					dialog.data.fadeIn('fast');
																				})
																	})
														},
													onClose: 
														function(dialog)
														{
															dialog.data.fadeOut("fast", 
																	function()
																	{
																		dialog.container.slideUp("fast", 
																				function()
																				{
																					dialog.overlay.fadeOut("fast", 
																							function() 
																							{ 
																								$.modal.close();
																								$("object").show();
																							})
																				})
																	})
														}
												});
									});
							
							return false;
						});
			});
});

addComment = function()
{
	var postData = 
	{
			blogEntryId: $("#addComment_blogEntryId").val(),
			name: $("#addComment_name").val(),
			comment: $("#addComment_comment").val()
	};
	if ($.trim(postData.name) == "")
	{
		alert("Please enter your name");
		return false;
	}
	else if ($.trim(postData.comment) == "")
	{
		alert("Please enter a comment");
		return false;
	}
	
	$.post("/ajax/saveComment.php", postData, 
			function(result)
			{
				var $comments = $("#comments_" + postData.blogEntryId);
				$comments.find("span").html(parseInt($comments.find("span").html()) + 1);				
				$comment = $(document.createElement("div"));
				$comment.addClass("userComment");
				$comment.html("<p>" + postData.comment + "</p><p>" + postData.name + "</p>");
				$comments.next("div.comments").prepend($comment);

				$.modal.close();
			});
};