//Tab Content
$(document).ready(function(){
    var setsLength = 0;
    var tabPre = "tabSet_";
    var contentPre = "tabContentSet_";
    $("ul.tabNav").each(function(i){
        $(this).attr("id", tabPre+(i+1));
         setsLength = i+1;
    });
    $("div.tabContainer").each(function(i){
        $(this).attr("id", contentPre+(i+1));
    });
    var cID = null;
    var cTabIndex = null;
    var theURL = unescape(window.location.hash);
	//does showTab marker exist?
	if(theURL.indexOf("#st=") !== -1) {
		//strip out extraneous characters
		var locStartMarker = theURL.indexOf("#st=");
		var locEndMarker = theURL.indexOf("/")+1;
		theURL = theURL.slice(locStartMarker,locEndMarker);
			//make sure theURL follows correct regex pattern
   			if (theURL.match(/^(#st=)([a-z\d]+)(_{1}[a-z\d]+)?\/$/i) && theURL.length != "0"){
				//parse the URL, with or without the underscore
				var locStartMarker = theURL.indexOf("#st=")+4;
				var locEndMarker = theURL.indexOf("/");
				theURL = theURL.slice(locStartMarker,locEndMarker);
				if (theURL.match("_") == null) {
					theURL = [ '1', theURL.charAt(theURL.length-1)]
				} else if (theURL.match("_")) {
					theURL = theURL.split("_");
				}
				//set cID and cTabIndex, and replace any undefined elements
				if (theURL[0] !== undefined  && theURL[1] !== undefined) {
					//set any undefined elements to 1
					for (var i = 0; i <  theURL.length; i++) {
						if (theURL[i] === undefined) {
							theURL[i] = 1;   
						} else {
							theURL[i] = theURL[i].charAt(theURL[i].length-1);
						}
					}
					cID = parseInt(theURL[0]);
					cTabIndex = parseInt(theURL[1])
				}
			   //resolve NaN to 1, only if there is at least one legitimate number out of two
			   if ((!isNaN(cID) && !isNaN(cTabIndex)) || (cID != 1 && cTabIndex != 1) || (!isNaN(cID) && cTabIndex >1) || (cID > 1 && !isNaN(cTabIndex))) {
					if (!isNaN(cID) || !isNaN(cTabIndex)) {
						if (isNaN(cID) ) {
							cID = 1;
						} else if (isNaN(cTabIndex)) {
							cTabIndex = 1;
						}
					}
					var cTab = "ul#" + tabPre + cID;
					var cContent = "div#" + contentPre + cID;
					var tabsLength = $(cTab).children('li').size();
					if (cID <= setsLength && cTabIndex <= tabsLength) {
						showTab(cTab, cContent, cTabIndex,  cID);
					}
				}   
			}
		}
// Display tabs on click
    $('ul.tabNav a').click(function(){
        cID = $(this).parents('li').parents('ul').attr("id");
        cID = cID.charAt(cID.length - 1);
        cTab = "ul#" + tabPre + cID;
        cContent = "div#" + contentPre + cID;
        cTabIndex = $(this).parent().prevAll().length + 1;
		tabsLength = $(cContent).children('div.content').size();
        if ( $(this).parent().hasClass('tabSwitchOff') ){
            return true;
        } else {
			showTab(cTab, cContent, cTabIndex, cID);
			return false;
        }
    });
// Navigate to specified tab onclick internal page link with class "goToTab"
//  - href must be equal to "#", unless navigating to a named anchor within that tab
//  - tab set / number placed inside the "name" attribute, separated by an underscore
//  ex <a href="#" class="goToTab" name="2_3">Go to another tab in this page</a>
    $('a.goToTab').click(function(){
        var anchorContent = $(this).attr("name");
		if (anchorContent.length == 1) {
			cID = 1;
			cTabIndex = anchorContent.charAt(0);
		} else if (anchorContent.match(/^\d_\d$/)) {
			cID = anchorContent.charAt(0);
			cTabIndex = anchorContent.charAt(2);
		}
        cTab = "ul#" + tabPre + cID;
        cContent = "div#" + contentPre + cID;
        tabsLength = $(cTab).children('li').size();
        if (!isNaN(cID) && cID <= setsLength && !isNaN(cTabIndex) && cTabIndex <= tabsLength) {
            showTab(cTab, cContent, cTabIndex, cID);
        }   
    });
    function showTab(tabs, content, tabIndex, cID) {
        showOneTab (tabs, content, tabIndex);
		//find origin (containing) tabsets
        for (var x = cID-1; x > 0; x--) {
            var target = tabs;
            var o = $(target).closest("div.tabContainer").attr("id");
            if (o != undefined) {
                var oContent = ($(target).closest("div.content").attr("class"));
                oContent = oContent.charAt(oContent.indexOf("content_") + 8);
                var oIndex = o.charAt(o.length-1);
                tabs = "ul#" + tabPre + oIndex;
                tabIndex = oContent.charAt(oContent.length-1);
                content = "div#" + contentPre + oIndex;
                showOneTab (tabs, content, tabIndex);
            }
            else if (o == undefined){
                break;
            }
        }
    }
    function showOneTab (tabs, content, tabIndex){
        $(tabs).children("li").removeClass("current");
        $(content).children("div.content").removeClass("current");
        $(tabs).children("li.tab" + tabIndex).addClass("current");
        $(content).children("div.content_" + tabIndex).addClass("current");
    }
});
