﻿function initTabs() {
	showDefault();
	$("ul.tabs").find("a").each(function(i) {
		$(this).click(function(e) {
			e.preventDefault();
			location.hash = $(this).attr("href");
			setTab($(this));
			hideTabs();
			$($(this).attr("href") + "_").removeClass("hidden");
		});
	});
}

function setTab(tab) {
	$("ul.tabs").find("a").each(function(i) {
		$(this).removeClass("selected");
	});
	tab.addClass("selected");

	try {
		pageTracker._trackPageview(location.pathname + location.search + location.hash.replace("#", "$"));
	} catch (err) { }
	
}

function showDefault() {
	$("div.tabs-content > div").each(function() {
		var showId = $(this).attr("id");
		$(this).attr("id", showId + "_");
	});

	hideTabs();
	if (location.hash.length > 0 && $(location.hash + "_").length > 0) {
		$(location.hash + "_").removeClass("hidden");
		setTab($("a[href='" + location.hash + "']"));
	}
	else {
		$($("div.tabs-content > div")[0]).removeClass("hidden");
		setTab($($("ul.tabs").find("a")[0]));
	}
}

function hideTabs() {
	$("div.tabs-content > div").each(function() {
		var showId = $(this).attr("id");
		//$(this).hide();
		$(this).addClass("hidden");
	});
}

function setArticleTableButtonHrefs(anchor) {
	$("#articles-table tbody .more-info").each(function () {
		var href = $(this).attr("href");
		var hashTagPosition = href.indexOf("#");	
		var baseUrl = hashTagPosition == -1 ? href : href.substring(0, hashTagPosition);
		$(this).attr("href", baseUrl + anchor);
	});
}

$(document).ready(function () {
	var hashTagPosition = document.URL.indexOf("#");

	if (hashTagPosition > -1) {
		var anchor = document.URL.substr(hashTagPosition);
		setArticleTableButtonHrefs(anchor);
	}
});

$(".tabs a").click(function () {
	var anchor = $(this).attr("href");
	setArticleTableButtonHrefs(anchor);
});
