﻿
var tabberOptions = {

	'onClick': function(argsObj) {

		var t_object = argsObj.tabber; /* Tabber object */
		var t_id = t_object.id; /* ID of the main tabber DIV */
		var selectedTabIndex = argsObj.index; /* Which tab was clicked (0 is the first tab) */
		var eventObject = argsObj.event; /* Event object */

		// Cookies
		var sPath = window.location.pathname;
		var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
		setCookie(sPage, selectedTabIndex);

		return true;
	},

	'onLoad': function(argsObj) {
		// Wire up anchor detection for automatic tab selection links

		// Page name
		var sPath = window.location.pathname;
		var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);

		var selectedTab = "";

		// Use the hash is requested
		if (location.hash != "") {
			if (location.hash == "#1") {
				selectedTab = 0;
			}
			else if (location.hash == "#2") {
				selectedTab = 1;
			}
			else if (location.hash == "#3") {
				selectedTab = 2;
			}
			else if (location.hash == "#4") {
				selectedTab = 2;
			}
			else if (location.hash == "#servicedesk") {
				selectedTab = 0;
			}
			else if (location.hash == "#helpdesk") {
				selectedTab = 1;
			}
			else if (location.hash == "#assetmanagement") {
				selectedTab = 2;
			}
			else if (location.hash == "#addon") {
				selectedTab = 3;
			}
			else if (location.hash == "#bestpractice") {
				selectedTab = 4;
			}
			else if (location.hash == "#trackit") {
				selectedTab = 0;
			}
			else if (location.hash == "#trackit!") {
				selectedTab = 0;
			}
			else if (location.hash == "#footprints") {
				selectedTab = 1;
			}
		}

		// Use query string if no hash match found
		if (selectedTab == "") {
			var queryStringValue = getQuerystring("tab");
			if (queryStringValue == "1") {
				selectedTab = 0;
			}
			else if (queryStringValue == "2") {
				selectedTab = 1;
			}
			else if (queryStringValue == "3") {
				selectedTab = 2;
			}
			else if (queryStringValue == "4") {
				selectedTab = 3;
			}
			else if (queryStringValue == "5") {
				selectedTab = 4;
			}
			else if (queryStringValue == "servicedesk") {
				selectedTab = 0;
			}
			else if (queryStringValue == "helpdesk") {
				selectedTab = 1;
			}
			else if (queryStringValue == "assetmanagement") {
				selectedTab = 2;
			}
			else if (queryStringValue == "addon") {
				selectedTab = 3;
			}
			else if (queryStringValue == "bestpractice") {
				selectedTab = 4;
			}
		}

		// Use cookie if no match found
		if (selectedTab == "") {
			if (sPage) {
				argsObj.tabber.cookie = sPage;
			}
			
			// If a cookie was previously set, restore the active tab
			var i = parseInt(getCookie(argsObj.tabber.cookie));
			if (!isNaN(i)) {
				selectedTab = i;
			}
		}

		// Select and save
		if (selectedTab != "") {
			argsObj.tabber.tabShow(selectedTab);
			setCookie(sPage, selectedTab);
		}
	}
};
    
    function getQuerystring(key, default_)
    {
      if (default_==null) default_="";
      key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
      var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
      var qs = regex.exec(window.location.href);
      if(qs == null)
        return default_;
      else
        return qs[1];
    } 
    
    /*==================================================
    Cookie functions
    ==================================================*/
    function setCookie(name, value, expires, path, domain, secure) {
        document.cookie= name + "=" + escape(value) +
            ((expires) ? "; expires=" + expires.toGMTString() : "") +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            ((secure) ? "; secure" : "");
    }

    function getCookie(name) {
        var dc = document.cookie;
        var prefix = name + "=";
        var begin = dc.indexOf("; " + prefix);
        if (begin == -1) {
            begin = dc.indexOf(prefix);
            if (begin != 0) return null;
        } else {
            begin += 2;
        }
        var end = document.cookie.indexOf(";", begin);
        if (end == -1) {
            end = dc.length;
        }
        return unescape(dc.substring(begin + prefix.length, end));
    }
    function deleteCookie(name, path, domain) {
        if (getCookie(name)) {
            document.cookie = name + "=" +
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "") +
                "; expires=Thu, 01-Jan-70 00:00:01 GMT";
        }
    }
