if (!Blog) var Blog = new Object();
Blog.replayID = 0;

function clear_location () {return document.location.href.replace(/\#\S*/ig,"").replace(/[\&\?]post=\S*/ig,"").replace(/[\&\?]=\d+/ig,"").replace(/[\&\?]f=\d+/ig,"").replace (/[\&\?][ad]\=\d{4}\/\d{2}\/\d{2}/ig,"").replace(/[\&\?]p\=\S{32}/ig,"").replace(/[\&\?]tag\=\S*/ig,"").replace(/[\&\?]add/ig,"").replace(/[\&\?]edit/ig,"");}

function refreshCaptcha()
{
  $.getJSON (clear_location()+"&captcha&rand"+Math.random(), function (r) { 
  $("#captcha_img").attr("src",r.result); 
  } );
	return false;
}

function init_tags()
{
	$(".tag-link").bind ("click", function () {
		var new_link = clear_location();
		if (new_link.search ("\\?")==-1) new_link = new_link+ "?";
		else new_link = new_link + "&";
		new_link = new_link.replace ("&&","&");
		new_link = new_link + "tag="+$(this).html().replace ("&nbsp;"," ");		
		document.location.href = new_link;
	} );
}
function init_size_preferences()
{
	$(".size_link").bind ("click", function () {
		$(".record_text").css ("font-size", 15+ parseInt($(this).attr("val")) +"px");		
		$(".comment_text").css ("font-size", 12+ parseInt($(this).attr("val")) +"px");		
	} );

}
function init_archives()
{
	$(".archive_button").bind("click", function () {
		var selected_date = new Date();		
		selected_date.setTime(Date.parse($(this).attr("year")+"/"+$(this).attr("month")+"/"+"01"));
		select_date (selected_date,true);
	} );
}
function init_comments()
{
	$(".post_show_comments").bind ("click", function () {
		var new_link = clear_location();
		if (new_link.search ("\\?")==-1) new_link = new_link+ "?p="+$(this).attr("pid");
		else new_link = new_link + "&p="+$(this).attr("pid");//document.location.href.replace ("#","");
		document.location.href = new_link;
	} );
	$(".post_add_reply").removeAttr("disabled");
	$(".post_add_reply").bind ("click", function (e) {
		if ($.trim($("#commentUser").val()) == "") {
			alert ("Enter user name");
			return;
		}
		if ($.trim($("#commentEmail").val()) == "") {
			alert ("Enter email");
			return;
		}
		if ($.trim($("#commentContent").val()) == "") {
			alert ("Enter message");
			return;
		}		
		var interval,button = $(".post_add_reply");
		button.attr("disabled","true");
		button.val ("Commenting");
    		interval = window.setInterval(function(){
		    var text = button.val();
		    if (text.length < 15){
			button.val(text + '.');
		    } else {
			button.val('Commenting');
		    }
		}, 200); 			
		$.post (clear_location()+"&comment",
			{
				user:$.trim($("#commentUser").val()),
				email:$.trim($("#commentEmail").val()),
				message:$.trim($("#commentContent").val()),
				post_id: $(this).attr ("pid"),			
				replay_id: Blog.replayID,
				captcha:$("#captcha").val(),
                captcha_src:$("#captcha_img").attr("src")
			},
			function (r) {
				if (parseInt(r)==0) {
					alert ("Sorry, you've entered incorrect captcha string\nTry again");
					refreshCaptcha();
					window.clearInterval (interval); 
              		button.val ("Submit comment"); 
              		button.removeAttr ("disabled");					
				} else {
				window.clearInterval (interval);
				$("#commentUser").val("");
				$("#commentEmail").val("");
				$("#commentContent").val("");
//				$("#captcha").val("");
				document.location.reload();
				}
			}
		);		
	} );
	$(".reply").click( function (e) {
		if (!$(this).attr("cid")) return;
		Blog.replayID = $(this).attr("cid");
		var cname = $(this).parent().children()[0].innerHTML;
		$("#in_reply_to").show();
		$("#in_reply_name").html (cname+" ");
		$("#in_reply_name").attr("href","#c"+Blog.replayID);
	} );
	$(".reply_delete").click ( function (e) {
		Blog.replayID = 0;
		$("#in_reply_to a").html ("");
		$("#in_reply_to").hide();
	} );
}

function select_date(date,is_archive)
{
	var new_link = clear_location();
	var s = "&";
	new_link = new_link.replace ("&&","&");
	if (new_link.search ("\\?")==-1) s="?";
	var date_query = s+"d=";
	if (is_archive) date_query = s+"a=";
	var m = parseInt(date.getMonth()+1);
	var d = parseInt (date.getDate());
	if (m<10) m = "0"+m;
	if (d<10) d = "0" +d;
	var str_date = date.getFullYear()+"/"+m+"/"+d;	
	document.location.href =new_link + date_query+str_date;
	return false;	
}
function update_calendar(date)
{
	if (!date) return;
	$.getJSON (clear_location()+"&calendar="+date.getMonth()+"/"+date.getFullYear(), function (r) {
		var cal = $(".calendar_table");
		$.each (r.days, function (i,val) {
			$(".calendar_table td[val="+val+"]").addClass ("cal_on");			
    	} );	
	} );
}

$().ready(function () {
	init_archives();
	init_size_preferences();
	init_tags();
	init_comments();
	update_calendar();

	var current_date = new Date();
	if (document.location.href.indexOf ("d=") != -1) {
		var reg = /d\=\d{4}\/\d{2}\/\d{2}/i;		
		var d = document.location.href.match (reg)[0].replace("d=","");
		current_date.setTime(Date.parse(d));	
	}
	$("#cal-event").calendar( {year: current_date.getFullYear(), month: current_date.getMonth(), dateChanged: select_date,current:current_date,onRefresh:update_calendar  } );
	$(".click_back").click ( function () { document.location.href = clear_location();} );
	$(".permament_link").click ( function () { 
		var new_link = clear_location();
		var s = "&";
		if (new_link.search ("\\?")==-1) s="?";
		document.location.href = new_link + s +"post="+$(this).attr("value");
	} );
});

