$(function() {
	// Tabs
	$(".tab-content").hide(); //Hide all tabs

	// No default tab passed
	if (!window.location.hash || $(".search-sections A[href=" + window.location.hash + "]").length == 0) {
		if ($(".default-tab").length > 0) {
			// a default tab has been specified, which is not the first in the LI
			var href = $(".search-sections A.default-tab").addClass("active").attr("href");
			$(href + ".tab-content").show();
		}
		else {
			// no default specified, just show the first one
			$(".search-sections A:first").addClass("active");
			$(".tab-content:first").show();
		}
	}
	else {
		var tab = window.location.hash;
		$(".search-sections A[href=" + tab + "]").addClass("active");
		$(".tab-content" + tab).show();
	}
	
	$(".search-sections A").click(function (e) {
		$(".search-sections A").removeClass("active");
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab-content").hide(); //Hide all tab content

		var activeTab = $(this).attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).show()
		
		stopFragmentJump(activeTab);
		return false;
	});
});

function stopFragmentJump(hash) {
	// this complicated code is to stop the scrolling behaviour of the browser working on the #fragment
	// explanation here - http://stackoverflow.com/questions/1489624/modifying-document-location-hash-without-page-scrolling
	hash = hash.replace(/^#/, '');
	var fx, node = $('#' + hash);
	if (node.length) {
		fx = $('<div></div>')
			.css({
				position:'absolute',
				visibility:'hidden',
				top: $(window).scrollTop() + 'px'
			})
			.attr( 'id', hash )
			.appendTo( document.body );
		node.attr( 'id', '' );
	}
	document.location.hash = hash;
	if (node.length) {
		fx.remove();
		node.attr( 'id', hash );
	}
}
