//New Menu Script

var menuHideSpeed = 200;
var menuShowSpeed = 200;
var w = window.location.pathname;
var pageId = w.substring(w.lastIndexOf("/") + 1, w.lastIndexOf("."));
var actionTimer;
var actionTimeout = 200;

/*
 * this function extracts the menu class name reguardless of position
 * class name must begin with "menu_" in order to be valid
 * 'classString' can be any valid string of class names serperated by spaces.
 *
 */

function findMenuName(classSting) {
	var classStringArray = classSting.split(" ");
	for (var i = 0; i < classStringArray.length; i++) {
		if (classStringArray[i].indexOf("menu_") != -1) {
			return classStringArray[i];
		}
	}
}

/*
 * this function will automatically run on document ready
 * it will search out the active HREF (as detected by page name) and
 * apply a class name of 'activeLink' to this HREF and its parent menu HREF(s)
 *
 */

$(function() {
    //Set 'activeLink' class for the active page.
    $("a[href$=" + pageId + ".aspx]").addClass("activeLink");

    //Get name of menu of the current active page
    if ($(".nav:has(a.activeLink)").length == 0) return;
    var menu1 = findMenuName($(".nav:has(a.activeLink)").get(0).className);
    //Set 'activeLink' class for HREF with matching menu class 
    if (menu1) { $("a." + menu1).addClass("activeLink"); }

    //Now check for a third menu and set 'activeLink' as well - this script only supports three level.
    if ($(".nav:has(a." + menu1 + ")").length != 0) {
        var menu2 = findMenuName($(".nav:has(a." + menu1 + ")").get(0).className);
        if (menu2) { $("a." + menu2).addClass("activeLink"); }
    }


    //the following code locates all the menu that contain HREFs with 'activeLink'
    //classes and sets each parent menu to 'activeMenu' - this is so we can reset when 
    //user finishes with the nav


    //for every activeLink set it's parent menu to 'activeMenu'
    $(".nav:has(a.activeLink)").addClass("activeMenu");
    //for every activeMenu search it's children to find activeLink
    //then find the menu that matches the same "menu_" class name and add class 'activeMenu'
    $(".nav.activeMenu").each(function() {
        $(this).children(".activeLink").each(function() {
            $(".nav." + findMenuName(this.className)).addClass("activeMenu");
        });
    });

});


/*
 * These functions will are part of the hide/show menu system
 *
 */

//saves the current active page HREFs
function saveActivePageLinks() {
	$(".activeLink").addClass("activePageLink");
}
//removes all activeLink classes
function removeActiveLinks() {
	$(".activeLink").removeClass("activeLink");
}
//restore the stored active page HREFs
function restoreActivePageLinks() {
	removeActiveLinks();
	$(".activePageLink").addClass("activeLink");
}

//shows all hidden dropdowns
function showActiveLink()
{
	restoreActivePageLinks();
	$(".nav.activeMenu:hidden").show(menuShowSpeed);
}

//hides all dropdowns
function hideAllDropdowns() {
	removeActiveLinks();
	var openDropdowns = $(".navLevelTwo .nav:not(.activeMenu):visible, .navLevelThree .nav:not(.activeMenu):visible");

	if (openDropdowns.length == 0) {
		//nothing open so just show the active page menus
		showActiveLink();
	} else {
		for (var i = 0; i < openDropdowns.length; i++) {
			if (i < openDropdowns.length - 1) {
				//menus open so hide each one...
				$(openDropdowns[i]).hide(menuHideSpeed);
			}
			else {
				//when last one completes run showActiveLink function
				$(openDropdowns[i]).hide(menuHideSpeed, showActiveLink);
			}
		}
	}
}

//hides menus for level two and three
function hideLevelTwo()
{
	removeActiveLinks();
	$(".navLevelTwo .nav:visible, .navLevelThree .nav:visible").hide(menuHideSpeed);
}
//hides menus for level three only
function hideLevelThree()
{
	$(".navLevelThree .nav:visible").hide(menuHideSpeed);
}
//opens the submenu if it is not an activeLink (this is the function that is 
//run from the event handler) moved to seperate function to add delay.
function openSubMenu(menuToOpen) {
	var activeMenuToOpen = $(".menuToOpen");
	if (!(activeMenuToOpen.hasClass("activeLink"))) {
		//check to see if this is a level one menu
		//if so hide all levels below (2 and 3)
		if (activeMenuToOpen.parents(".navLevelOne").length) { hideLevelTwo(); }
		//else check to see if it's a level two menu
		//if so hide only level three
		else if (activeMenuToOpen.parents(".navLevelTwo").length) { hideLevelThree(); }
		//check to see if it does not have a class of activeLink - if so add it
		//but remove it from any siblings otherwise we get two highlights

		activeMenuToOpen.siblings().removeClass("activeLink");
		activeMenuToOpen.addClass("activeLink");
		if (activeMenuToOpen.length) {
			$(".nav." + findMenuName(activeMenuToOpen.get(0).className) + ":hidden").show(menuShowSpeed);
		}
	}	
}

/*
 * function to run when document loads to show activeMenus and 
 * to bind event handlers to elements
 *
 */

$(function() {
	//show the current active link and save for later.
	saveActivePageLinks();
	showActiveLink();
	//add event handler to dropdowns
	$("a.dropdown").mouseenter(function() {
		$(this).addClass("menuToOpen");
		actionTimer = window.setTimeout(openSubMenu, actionTimeout);
	});
	$("a.dropdown").mouseleave(function() {
		$(this).removeClass("menuToOpen");
	});

	//add event handler to non-dropdown
	$(".navLevelOne a").not(".dropdown").mouseenter(hideLevelTwo);
	$(".navLevelTwo a").not(".dropdown").mouseenter(hideLevelThree);

	//add event handler to "navigation" div element
	$(".navigation").mouseenter(function() {
		clearTimeout(actionTimer);
	});

	$(".navigation").mouseleave(function() {
		actionTimer = window.setTimeout(hideAllDropdowns, actionTimeout);
	});

});

