var post_id = 0;

$(document).ready(function()
{
	post_id = $(".show_comments").attr("id");
	
	$("ul.stories li a.news").bind("click",function()
	{
		var current = $(this);
		
		current.parent().addClass("loading");
		
		$("ul.stories li a.news").removeClass("selected");
		$(this).addClass("selected");
		$("div.story").fadeTo("fast", "0.1", function()
		{
			$("div.story").load(current.attr("href").replace("permalink", "get"),function()
			{
				activate_comments();
				$("div.story").fadeTo("fast", "1.0");
				$("ul.stories li").removeClass("loading");
			});
		});
		
		return false;
	});
	
	activate_comments();

});



function activate_comments()
{
	$("a.show_comments").unbind().bind("click", function()
	{
		$(this).parent().fadeOut("fast", function()
		{
			get_comments();
		});
		
		return false;
	});
}

function get_comments()
{
	if($("#comments").html() == "")
	{
		$("#comments").hide();
	}

	set_post_id($(".show_comments").attr("id"));
	$("#comments").load("/blog/comments/" + post_id + "/" + Math.ceil(Math.random()*100),function()
	{

		$("#comments").fadeIn("fast", function()
		{
			ajax_form();
		});
	});
}

function ajax_form()
{
	$("#post_comment").ajaxForm(
	{
		beforeSubmit:  function()
		{
			$("input[value='Save']").attr("src", "/lib/images/btn_wait.gif").attr("disabled","disabled");
		},
		success : function()
		{
			get_comments();
		}
	});
}

function set_post_id(new_post_id)
{
	if(new_post_id > 0)
	{
		post_id = new_post_id;
	}
}