function _set_search() {
    var s = $('#search');
    s.val('Google').css('color', '#333');
    s.focus(function(){$(this).val('').css('color','#000');});
    s.change(function() {
	location.href = 'http://www.google.com/search?ie=UTF-8&q=' +
	    $(this).val() + '+site%3A' + location.host + blog_root + '/';
    });
}

function _set_calendar() {
    if ($.cookie('last_calendar')) {
	var ym = $.cookie('last_calendar').split('-');
	var y = parseInt(ym[0]);
	var m = parseInt(ym[1]);
	cal_table(y, m, false)
    }
}

function _set_comment_form() {
    if ($.cookie('comm_author'))
	$('#comm_author').val($.cookie('comm_author'));
    $('#comm_title').val('Re: ' + $('h2 > a').text());
    $('<input type="button" />')
     .val('Preview')
     .click(preview)
     .appendTo('#comm_append');
    $('<input type="submit" id="posting" />')
     .val('Post')
     .attr('disabled', 'disabled')
     .appendTo('#comm_append');
    $('<div id="preview" />').appendTo($('#comm_field'));
}

var cal_cache = new Array();
function cal_table(y, m, dir) {
    var hdir = false;
    var sdir = false;
    if (dir == 'prev') {
	if (m == 1) {
	    y = y - 1;
	    m = 12;
	} else {
	    m = m - 1;
	}
	hdir = 'right';
	sdir = 'left';
    } else if (dir == 'next') {
	if (m == 12) {
	    y = y + 1;
	    m = 1;
	} else {
	    m = m + 1;
	}
	hdir = 'left';
	sdir = 'right';
    }

    var url = blog_root + '/api.py/cal_table?year=' + y + '&month=' + m;
    var ym = y + '-' + m;
    if (ym in cal_cache) {
 	$('#calendar').hide(
	    'slide', {direction: hdir}, 'fast',
	    function() {
		$(this).html(cal_cache[ym])
                       .show('slide', {direction: sdir}, 'fast');
	    });
    } else {
	$.get(url, function(table) {
 	    $('#calendar').hide(
		'slide', {direction: hdir}, 'fast',
		function() {
		    $(this).html(table)
                           .show('slide', {direction: sdir}, 'fast');
		    cal_cache[ym] = table;
		});
	});
    }
    $.cookie('last_calendar', ym);
}

function expand_all_tags() {
    var url = blog_root + '/api.py/all_tags';
    $.get(url, function(ul) {
	$('#allTags').hide(
	    'slide', {direction: 'up'}, 'slow',
	    function() {
		$(this).html(ul)
		.show('slide', {direction: 'up'}, 'slow');
	    });
    });
}

function preview() {
    if (!previewed) {
	$('<input type="hidden" name="eid" />')
        .val(eid)
        .appendTo($('#comm_field'));
	$('<input type="hidden" name="comm_time" />')
	.val(new Date().getTime() / 1000)
	.appendTo($('#comm_field'));
	$('<input type="hidden" name="pbody" />')
	.val($('#comm_body').val())
	.appendTo($('#comm_field'));
	previewed = true;
    }

    if ($('#comm_author').val()) {
	$.cookie('comm_author', $('#comm_author').val(),
		 {expires: 30, path: blog_root + '/'});
    } else {
	alert('Please input your name.');
	$('#comm_author').focus();
	return;
    }
    if (! $('#comm_author').val()) {
	alert('Please input your name.');
	$('#comm_author').focus();
	return;
    }

    $('#preview').empty();
    $('#preview').html($('#comm_body').val().replace('\n', '<br />'));
    $('#posting').removeAttr('disabled');
    $('#comm_form').attr('action', blog_root + '/entry.py/post_comment');
}

$(document).ready(function() {
    _set_search();
    _set_calendar();

    if (c = document.getElementById('comm_field')) {
	if ($('div.paging span').length == 2)
	    $('div.paging').height(Math.max(
		$('div.paging span.older').height(),
		$('div.paging span.newer').height()));
	else
	    $('div.paging').height($('div.paging > span').height());
	_set_comment_form();

	if (location.hash) 
	    $(location.hash).animate(
		{backgroundColor: 'white'}, 1500,
		function() {
		    $(this).animate(
			{backgroundColor: $('body').css('backgroundColor')},
			3000);
		});
    }
});
