$.fn.formHandling.defaults.errorContainer = 'div.errors';
$.fn.formHandling.defaults.errorWrapper = '<div class="error_msg"></div>';

$(function() {
	/* Show only when js */
	$('#print, a.b_email, a.b_share').css('display', 'block');

/*  -------------------------------------------------------------------
    Side bar items
	------------------------------------------------------------------- */
	/* Reduce to a maximum lenght */
	$('#recent-posts, #recent-comments, #categories-86989731, #authors, #links').each(function () {
		var	$block = $(this);
		var text = '', limit = 5, diff = 2;
		
		switch (this.id) {
			case 'recent-posts': text='See more posts'; break;
			case 'recent-comments': text='See more comments'; break;
			case 'categories-86989731': text='See all categories'; limit=12; break;
			case 'authors': text='See all bios'; limit=8; diff=1; break;
			case 'links': text='See all sites'; limit=6; break;
			default: text='See more';
		}
		
		var $items = $('li', this).slice(limit);
		
		if ($items.size()-diff>0) {
			$items.hide();
			
			var $link = $('<a href="#" class="follow-arrow">'+text+'</a>').click(function () {
				$('li', $block).show();
				$link.hide();
				return false;
			});
			$('ul', this).after($link);
		}
	});
	
	/* Toogle function */
	$("#sidebar h2 span.toggle").css('cursor', 'pointer').toggle(
		function () {
			$(this)
				.parents('h2').css('backgroundPosition', 'right top')
				.siblings('div').slideUp('fast');
			return false;
		},
		function () {
			$(this)
				.parents('h2').css('backgroundPosition', 'right bottom')
				.siblings('div').slideDown('fast');
			return false;
		}
	);
	
	$('#archives a.year').click(function () {
		if (!$(this).hasClass('selected')) {
			$('#archives a.selected').removeClass('selected');
			$('#archives ul.open').hide();
			$(this).addClass('selected').siblings('ul').addClass('open').show();
		}
		return false;
	});
	
/*  -------------------------------------------------------------------
    Forms
    ------------------------------------------------------------------- */

	// Search form
	$('#frm_search').submit(function () {
		$('div.error', this).remove();
		if(!$('#s', this).val()) {
			$(this).append('<div class="error">You need to enter something to search for.</div>');
			return false;
		}
	});
	
	// Comments
	$('#frm_comment').formHandling({ajax: false});
	
	// Send to a Friend
	$('a.b_email').click(function () {
		$.ajax({
			type: "GET",
			url: $(this).attr('href'),
			data: "",
			success: function(resp){
				// Black background
				var $bgcover = $('<div id="bgcover">&nbsp;</div>').appendTo('body').css('opacity', '0.30');
				$bgcover.css('height', Math.max($('html').height(), $bgcover.height()));

				$("body").append(resp);
				$("#stf").css("top",getCenteredPos($('div.popin').height()));
				$("#frm_stf").formHandling({
					invalidMsg: 'Looks like you\'ve missed a field, or the email address you entered is not valid.<br />Please try again.',
					contentHandler: 'div.popin_content',
					formContainer: 'div.popin'
				});
			},
			error: function(req) { }
		});
		return false;
	});
	
/*  -------------------------------------------------------------------
    Utilities
    ------------------------------------------------------------------- */
	$('a.b_share').click(function () {return false;}).parent().hover (
		function () { $('ul', this).show() },
		function () { $('ul', this).hide() }
	);
	
	$('ul.shares a:not(.b_email)').bind("click", function(event){
		var size, which = $(this).attr("class").replace('b_', '');
		switch (which) {
			case 'facebook': size = "width=620,height=436"; break;
			case 'delicious': size = "width=725,height=400"; break;
			case 'digg': size = "width=962,height=500"; break;
			case 'stumbleupon':
			case 'magnolia':
			default: size = "width=500,height=500";
		}
		var my_popup = window.open($(this).attr("href"), "share_"+which, "scrollbars=yes,resizable=yes,toolbar=no,location=no,status=no,"+size);
		if (my_popup) {my_popup.focus();}
		return !my_popup;
	});

/*  -------------------------------------------------------------------
    External Links
    ------------------------------------------------------------------- */

	$("a.external")
		.append('<span class="softhide"> opens in a new window</span>')
		.bind("click", function(){ return !window.open(this.href); });

/*  -------------------------------------------------------------------
    Google Analytics
    ------------------------------------------------------------------- */
	var pageTracker = _gat._getTracker("UA-796315-16");
	pageTracker._initData();
	pageTracker._trackPageview();
	
});

function closePopin(){
	$('#bgcover, div.popin').remove();
}
function getCenteredPos(objheight){
	var ScrollTop = document.body.scrollTop;
	if (ScrollTop == 0){
		if (window.pageYOffset){
			ScrollTop = window.pageYOffset;
		}else{
			ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
		}
	}
	
	var myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myHeight = document.body.clientHeight;
	}
	
	return ScrollTop + (myHeight/2)-(objheight/2);
}