$(window).load(function(){
 if($('#s').val()){
 	$('label[for=s]').hide();
 }
 
 $('#s, label[for=s]').focus(function(){
 	if(!$('#s').val()){
 		$('label[for=s]').hide();
 	}
 });
 
 $('#s').blur(function(){
 	if($('#s').val() == ''){
 		$('label[for=s]').show();
 	}
 });
 
 $('#sidebar .widgettitle').click(function(){
 	return false;
 });
 
 $('#sidebar .widgettitle').after('<a class="toggle">-</a>');
 $('.post h2').after('<a class="toggle">-</a>');
 
 $('#sidebar .toggle').click(function(){
 	$(this).next().stop(true,true);
 	if($(this).next().innerHeight() == '25'){ // is collapsed, expand now
 		$(this).parent().removeClass('collapsed');	
 		$(this).next().animate({
	 		height: $(this).next().data('height')
	 	}, 500);	
	 	$(this).html('-');
	} else { // is expanded, collapse now
		$(this).next().data('height', $(this).next().innerHeight()-25);
		$(this).parent().addClass('collapsed');
		$(this).next().animate({
	 		height: 0
	 	}, 500);
	 	$(this).html('+');
	}
	storeWidgets();
 	return false;
 });
 
 $('.post .meta:gt(0), .post .entry:gt(0)').each(function(idx){
 	$(this).data('height', $(this).innerHeight());
 });
 
 $('.post .meta:gt(0)').css({
 	height: 25
 });
 
 $('.post .entry:gt(0)').css({
 	height: 0
 });
 
 $('.post .toggle:gt(0)').html('+');
 
 $('.post .toggle').live('click',function(){
 	$(this).siblings('.entry, .meta').stop(true,true);
 	if($(this).siblings('.entry').innerHeight() == '10'){ // is collapsed, expand now
 		$(this).siblings('.entry').animate({
	 		height: $(this).siblings('.entry').data('height')
	 	}, 500);
	 	
	 	$(this).siblings('.meta').animate({
	 		height: $(this).siblings('.meta').data('height')
	 	}, 500);
	 	$(this).html('-');	
	} else { // is expanded, collapse now
		$(this).siblings('.entry').data('height', $(this).siblings('.entry').innerHeight());
		$(this).siblings('.entry').animate({
	 		height: 0
	 	}, 500);
	 	
	 	$(this).siblings('.meta').data('height', $(this).siblings('.meta').innerHeight());
		$(this).siblings('.meta').animate({
	 		height: 20
	 	}, 500);
	 	$(this).html('+');
	}
 	return false;
 });
 
 $('#sidebar ul').sortable({
 	forcePlaceholderSize: true,
 	placeholder: 'ui-state-highlight',
	tolerance: 'pointer',
	update: function(){
		storeWidgets();
	}
 });
 $("#sidebar ul").disableSelection();
 
 // WIDGETS COOKIE
 var widgets = $.evalJSON($.cookie('widgets'));
 if(widgets){ // we have a cookie
 	sortWidgets(widgets);
 } else { // no cookie yet, store default states now
 	storeWidgets();
 }

});

function sortWidgets(widgets){
	$.each(widgets, function(idx, el){
		
		if(idx!=$('#'+el.id).index()){
			$('#'+el.id).insertBefore('#sidebar li.widget:eq('+idx+')');
		}
		
		if(el.collapsed){
			$('#'+el.id).find('.toggle').next().data('height', $('#'+el.id).find('.toggle').next().innerHeight()-25).parent().addClass('collapsed'),
			$('#'+el.id).find('.toggle').html('+').next().css({
				height: 0
			});
		}
	});
}

function storeWidgets() {
	var widgets = new Array();
 	$('#sidebar ul li.widget').each(function(idx){
 		widgets.push({
 			id: $(this).attr('id'),
 			collapsed: $(this).hasClass('collapsed')
 		});
 	});
 	
 	$.cookie('widgets', $.toJSON(widgets));
}
