// Tabs
$.fn.tabs = function() {

	$(".panel").hide();
	if ($(".active").length) {
		var CurrentActiveHash = $(this).children(".active").attr("href").indexOf("#");
		var CurrentActiveID = $(this).children(".active").attr("href").slice(CurrentActiveHash);
		$(CurrentActiveID+'.panel').show();
	};

	if (location.href.match("/#")) {
		var URLHashPos = location.href.indexOf("#");
		var URLPanelId = location.href.slice(URLHashPos);
		var RelatedTabParent = $(URLPanelId).parent().prev().attr("id");
		$('#'+RelatedTabParent).children().each(function() {
			if ($(this).attr("href").match(URLPanelId) && $(this).hasClass("default")) {
				$('#'+RelatedTabParent+' a').removeClass("active").addClass("default");
				$(this).removeClass("default").addClass("active");
			};
		});
		$(".panel").hide();
		$(URLPanelId).show();
	};

	$(this).children().click(function() {
		if (!$(this).hasClass("ignore")) {
			var HashPos = $(this).attr("href").indexOf("#");
			var CurrentTab = $(this).attr("href").slice(HashPos);
			var TabParent = $(this).parent().attr("id");
			var TabAdjacent = $('#'+TabParent).next().attr("id");
			if (CurrentTab == "#search") {
				$("#search").fadeIn(250);
			} else {
				$('#'+TabAdjacent+' .panel').hide();
				$(CurrentTab).show();
				$('#'+TabParent+' a').removeClass("active").addClass("default");
				$(this).addClass("active").removeClass("default");
			};
			return false;
		};
	});
}

$(document).ready(function() {
	
	// Initiate content switcher
	if ($("#switcher").length) {
		$("#switcher").tabs();
	};
	
	// Initiate about switcher
	if ($("#about-switcher").length) {
		$("#about-switcher").tabs();
	};
	
	// Initiate menu switcher
	if ($("#menu-switcher").length) {
		$("#menu-switcher").tabs();
	};
	
	if ($("#microsite").length) {
		$("#all-microsite-posts").show();
		
		$("#home-tab").click(function() {
			$("#all-microsite-posts").show();
			$("#switcher.microsite a").removeClass("active");
			$("#switcher.microsite #all-posts-tab").addClass("active");
		});
	};
	
	/* Show/hide tags if greater than 5 in summary
	$(".home .post-right .tags, .page-id-16 .post-right .tags, .page-id-8 .post-right .tags").each(function() {
		if ($(this).children(".hide_tag").length) {
			var hiddenTags = $(this).children(".hide_tag").length;
			$(this).append('<a class="show-tags" href="#">+ '+hiddenTags+' more tags</a>').children(".hide_tag").hide();
		};
	});*/
	
	$(".show-tags").toggle(function() {
		$(this).parent().children(".hide_tag").fadeIn(250);
	}, function() {
		$(this).parent().children(".hide_tag").fadeOut(250);
	});
	
});
