﻿var currItem = "";
var prevItem = "";
var currSubItem = ""; 
var activeSubMenu = ""; //this is for keeping the sub menu visible on sub pages
var timer = 3;
var t = 0;
var version = $.browser.version;
var subMenuShowing = false;
var subMenuWrapped = false;
var thirdMenuWrapped = false;
var firstRun = true;
//activate main nav for ie6
$(document).ready(function() {
    if ($.browser.msie && $.browser.version.substr(0, 1) == "6") {
        $(".sub-menu-item > .swirl > li > a > img").css({ "behavior": "url(/styles/iepngfix.htc)" });
        $(".sub-menu-sub > .third-level-nav > .third-level-nav-center > .third-level-nav-icon-list > a > img").css({ "behavior": "url(/styles/iepngfix.htc)" });
        $("img[@src=/images/btn-yellow.png]").attr({ "src": "/images/btn-dark-yellow.gif" });
    }
});
//show the active sub nav
function ShowActiveSubNav(item, currPage) {
    if (currPage == null || currPage == undefined || !currPage) {currPage = ""; }  
    if (item != '') {
        activeSubMenu = $(".nav > li > a[@href=" + item + "]").attr("rel"); 
    }
    if(activeSubMenu != ""){
        var offset = $(".top-nav").offset();
        var x = offset.left;
        var y = offset.top + 48;
        $(".sub-menu").css({ "left": x, "top": y });                

        //this fixes the issue of the sub nav menu showing up over the top nav menu when
        //the user is logged in
        if ($("#adminmenu").attr("id") != undefined && firstRun) {
            firstRun = false;
            $(".sub-menu").css({ "left": x, "top": y + 52 });
        }

        $(".sub-menu").show();

        $(".sub-menu-" + activeSubMenu).show();
       
        //set active link on sub bar
        var bg = "url(/styles/images/sub-nav-item-bg.png) no-repeat right top";
        if ($.browser.msie && $.browser.version.substr(0, 1) != "6") {
            bg = "url(/styles/images/sub-nav-item-bg-ie.png) no-repeat right top";         
        } else if ($.browser.msie && $.browser.version.substr(0, 1) == "6") {
            bg = "url(/styles/images/bg-sub-nav-ie.jpg) no-repeat right top";
        }
        $(".sub-menu-" + activeSubMenu + " > ul > li > a[@href=" + currPage + "]").css({ "background": bg });
        $(".sub-menu-" + activeSubMenu + " > ul > li > a").each(function() {
            if (currPage.indexOf($(this).attr("href").split("default.aspx").join("")) != -1) {
                $(this).css({ "background": bg });
            }
        });
        
        //when we roll over the sub menu items we want to keep the menu bar showing        
        $(".sub-menu-" + activeSubMenu + " > .sub-nav-list > li > a").bind("mouseenter", SubMenuItem_RollOverEvent);
        $(".sub-menu-" + activeSubMenu + " > .sub-nav-list > li > a").bind("mouseleave", SubMenuItem_RollOffEvent); 
                
    }
}
function ShowSubNav(item) {
    //clear last animation
    clearTimeout(t);
    //reset timer
    timer = 3;
    
    //hide active sub menu if showing
    if (activeSubMenu != "") {
        $(".sub-menu-" + activeSubMenu).hide();
    }
        
    //show sub menu if it is not visible
    if ($(".sub-menu:hidden") || subMenuShowing == false) {          
        var offset = $(".top-nav").offset();
        var x = offset.left;
        var y = offset.top + 48;
        $(".sub-menu").css({ "left": x, "top": y });
        $(".sub-menu").fadeIn("slow");
        subMenuShowing = true; //the sub-men:hidden doesn't work in ie6
    }

    //hide previous set of menu items
    if (prevItem != "" && prevItem != item) {
        $(".sub-menu-" + prevItem).hide();
    }

    //lets show the child elements    
    $(".sub-menu-" + item).show();
        
    
    //reset previous item
    prevItem = item;

    //set current item for referencing later
    currItem = item;

    //when we roll over the sub menu items we want to keep the menu bar showing
    $(".sub-menu-" + item + " > .sub-nav-list > li > a").bind("mouseenter", SubMenuItem_RollOverEvent);
    $(".sub-menu-" + item + " > .sub-nav-list > li > a").bind("mouseleave", SubMenuItem_RollOffEvent);    
}
//handle sub menu item roll over event
function SubMenuItem_RollOverEvent(event) {    
    //set current sub item
    currSubItem = $(this).attr("rel");

    //reset the timer
    timer = 3;

    //we don't want the timer to be counting down while rolled over the images
    clearTimeout(t);

    //prevent items from bubbling up
    event.stopPropagation();
    
    //check to see if there is a third level menu to show
    if ($(".sub-menu-sub-" + $(this).attr("rel")).length > 0) {
        //show third level sub menu
        var offset = $(this).offset();
        var currentItemPos = offset.left - 10; //10 is the left padding added to the item
        var currentItemWidth = $(this).width();
        x = (currentItemPos);
        x = x - ($(".sub-menu-sub-" + $(this).attr("rel")).width() / 2) + (currentItemWidth / 2);
                
        var y = 100 + $(".top-nav").offset().top;       

        //this is to prevent the menu from going off the screen
        var xcheck = x - $(".top-nav").offset().left;        
        if(xcheck < $("#wrapper").offset().left){
            x = $(".top-nav").offset().left;
        }

        $(".sub-menu-sub-" + $(this).attr("rel")).css({ "left": x, "top": y });
        $(".sub-menu-sub-" + $(this).attr("rel")).show();

        //bind rollover event for third level menu
        $(".sub-menu-sub-" + $(this).attr("rel")).bind("mouseenter", ThirdLevelMenuItem_RollOverEvent);
        $(".sub-menu-sub-" + $(this).attr("rel")).bind("mouseleave", ThirdLevelMenuItem_RollOffEvent);
    }
}
//handle sub menu item image roll off event
function SubMenuItem_RollOffEvent(event) {
    //start the timer for hiding the menu bar
    CheckStatus();

    //unbind image rollover and rollout events
    //$(this).unbind("mouseenter",SubMenuItem_RollOverEvent);
    //$(this).unbind("mouseleave",SubMenuItem_RollOffEvent);

    //hide third level sub menu
    $(".sub-menu-sub-" + $(this).attr("rel")).hide();
}
//handle third level menu item rollover evnet
function ThirdLevelMenuItem_RollOverEvent(event) {
    //keep this item showing    
    $(this).show();

    //keep the parent item background highlighted
    $("a[rel=" + currSubItem + "]").addClass("sub-menu-sub-active-parent");
        
    //reset the timer
    timer = 3;

    //we don't want the timer to be counting down while rolled over the images
    clearTimeout(t);

    //prevent items from bubbling up
    event.stopPropagation();
}
function ThirdLevelMenuItem_RollOffEvent(event) {
    //start the timer for hiding the menu bar
    CheckStatus();
   
    //hide this third level item now
    $(this).hide();        

    //remove background item from parent
    $("a[rel=" + currSubItem + "]").removeClass("sub-menu-sub-active-parent");
}
//start count down to hide sub nav menu
function HideSubNav(item) {
    CheckStatus();
}
//set timer to count down till we hide the menu
function CheckStatus() {
    //prevent interval from bubbling up
    clearTimeout(t);

    if (timer == 0) {
        timer = 3;
        //lets hide the child elements        
        $(".sub-menu-" + currItem).hide();
        subMenuShowing = false;
        if (activeSubMenu == "") {
            //hide sub menu            
            $(".sub-menu").fadeOut("slow");            
            subMenuShowing = false;
        } else {
            ShowActiveSubNav('');
        }
        currItem = "";
    } else {
        timer--;
        t = setTimeout("CheckStatus()", 700);
    }
}
//if window is resized we need to reposition the menu
$(window).resize(function() {
    var c = $(".top-nav");
    if ($(".top-nav").length > 0) {
        var offset = $(".top-nav").offset();
        var x = offset.left;
        var y = offset.top + 48;
        $(".sub-menu").css({ "left": x, "top": y });
    }
});