/// <reference path="jquery-1.4.1.js"/>
// Menu swapper
var currentMenu = "find"; // Set default menu
var currentButton = "findButton";
var IsSaving = true;

// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};



function showMenu(menu) { // Show menu
    if (isNav4) return;
    changeMenu(menu, true);
    currentMenu = menu;
    ManageWhatsNew(menu);
    if (window.location.hash != "#" + currentMenu + "_tab")
        window.location.hash = "#" + currentMenu + "_tab";
    ManagePrintButton();
    return true;
}

function changeMenu(handle, flag) {
    if (isNav4) return;
    var obj = jQuery("#" + handle);
    if (flag == false) {
        obj.css("visibility", "hidden");
    }
    else {
        if (currentMenu != null && currentMenu != handle)  // If there is another menu shown, hide it
            changeMenu(currentMenu, false);
        obj.css("visibility", "visible");
    }
}
function changeActiveButton(handle) {
    SetAsActive(handle);
    if (currentButton != handle)
        SetAsNotActive(currentButton);
    currentButton = handle;


}
function SetAsActive(handle) {
    var h = jQuery("#" + handle);
    h.attr('class', 'active');
    h.attr('className', 'active');
}
function SetAsNotActive(handle) {
    var h = jQuery("#" + handle);
    h.attr('className', 'Notactive');
    h.attr('class', 'Notactive');
}
function changeButton(handle, flag) {
    if (isNav4) return;
    var obj = jQuery("#" + handle);
    if (flag == false) {
        obj.css("border-bottom", "#C9B576");
        obj.css("color", "#FFFFF");
        obj.css("background", "#C9B576");
    }

    else {
        if (currentButton != handle) {
            if (currentButton != null && currentButton != handle)
                changeButton(currentButton, false);
        }
    }
}

function setActiveButton(whichButton) {
    changeActiveButton(whichButton);
}


// SH: CheckHash function is to check the URL for the a specific hash menu
// TN: This function is called from /includes/scripts/mootools-1.2.4-core-nc.js

function CheckHash() {
    Hash = window.location.hash;
    Pathname = window.location.pathname;
    //alert (Pathname);
    //TN: only do stuff if you're on the Home Page.
    if ((Pathname == "/index.php") || (Pathname == "") || (Pathname == "/")) {
        if (Hash != "") {
            Menu = Hash.split("_");
            showMenu(Menu[0].substring(1));
            setActiveButton(Menu[0].substring(1) + 'Button');
            return true;
        }
        else {
            jQuery("#find").css("visibility", "visible");
            SetAsActive("findButton");
            ManageWhatsNew("find");
            currentMenu = "find"; // Set default menu
            currentButton = "findButton";
            return true;
        }
    }
}
function changeStyle(SS) {
    if (SS == "M") {
        jQuery('#FontSizeSS').attr('href', "/includes/stylesheets/" + SS + '.css');
        jQuery("#text_m").css('backgroundColor', "#999");
        jQuery("#text_l").css('backgroundColor', "#000");
        jQuery("#text_xl").css('backgroundColor', "#000");
    }
    else
        if (SS == "L") {
            jQuery('#FontSizeSS').attr('href', "/includes/stylesheets/" + SS + '.css');
            jQuery("#text_m").css('backgroundColor', "#000");
            jQuery("#text_l").css('backgroundColor', "#999");
            jQuery("#text_xl").css('backgroundColor', "#000");
        }
        else
            if (SS == "XL") {
                jQuery('#FontSizeSS').attr('href', "/includes/stylesheets/" + SS + '.css');
                jQuery("#text_m").css('backgroundColor', "#000");
                jQuery("#text_l").css('backgroundColor', "#000");
                jQuery("#text_xl").css('backgroundColor', "#999");
            }
}

// CheckFontSize is to set the right font size from the cookie by  S.H
function CheckFontSize() {
		var cookieName="FontSize-"+window.location.hostname;
    var F = readCookie(cookieName);
    if (F != null) {
        if (F == "M") {
            changeStyle("M");
        }
        else
            if (F == "L") {
                changeStyle("L");
                document.getElementById("text_l").style.backgroundColor = "#999";
            }
            else
                if (F == "XL") {
                    changeStyle("XL");
                    document.getElementById("text_xl").style.backgroundColor = "#999";
                }
    }
}
// By S.H to change the font size 
function ChangeFontSize(Size) {
	 	var cookieName="FontSize-"+window.location.hostname;
    createCookie(cookieName, Size, 30);
    if (Size == "M") {
        changeStyle("M");
    }
    else
        if (Size == "L") {
            changeStyle("L");
        }
        else
            if (Size == "XL") {
                changeStyle("XL");
            }

}

function addLoadEvent(func) {
    //alert(func);

    //alert('Saving '+func); 
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload = function () {
            if (oldonload) {
                oldonload();
            }
            func();
        }

    }

}

// This function is to show/hide what's new depending on the tab pressed, S.H
function ManageWhatsNew(menu) {
    var Whatsnew = $("#WhatsNew");
    if (Whatsnew != null) {
        if (menu == "find")
            Whatsnew.css('visibility', "visible");
        else
            Whatsnew.css('visibility', "hidden");
    }
}

// TN: This function updates the Print button depending on tab
function ManagePrintButton() {
    currentURL = window.location.href;
    newURL = currentURL;
    uri = parseUri(currentURL);
    newURL = uri.protocol + "://" + uri.authority + uri.path + "?print=1";
    if (uri.query) {
        newURL = newURL + "&" + uri.query;
    }
    if (uri.anchor) {
        newURL = newURL + "#" + uri.anchor;
    }

    $('#myPrintHref').attr('href', newURL);
}

//Add main_menu_highlight() to window.onload S.H
function HomePageLoad() {
    // addLoadEvent(CheckHash);
    // addLoadEvent(CheckFontSize);

}

// Added for the sub pages. 
addLoadEvent(CheckFontSize);
