<!--

var activeSubMenus = new Array();

var hideSubMenuDelay       = 0.005; // Netscape requires an interval of at least 0.005, or the sub menu will flicker.
var animationIntervalDelay = 25; // Upwards of one, in miliseconds.

var currentSection = null;

function onMenuMouseOver(menu) {
    try {
	    menu        = $(menu);
	    menu.active = true;

	    var subMenu = $(menu.id + "-sub");
	    var button  = $(menu.id + "-button");
    	
	    animationValidate();
	    activeSubMenus.push(menu.id);
	    subMenuShow(menu, subMenu);
    	
	    if (button) {
		    button.className = "menu-main-button-hl";
	    }
    } catch (e) { }
}

function onMenuMouseOut(menu) {
    try {
	    menu        = $(menu);
	    menu.active = false;
    	
	    var subMenu = $(menu.id + "-sub");
	    var button  = $(menu.id + "-button");

	    function hide() {	
	        try {	
		        subMenuHide(menu, subMenu);
            } catch (e) { }
	    }
    	
	    clearInterval(menu.interval);
	    menu.interval = setInterval(hide, hideSubMenuDelay * 1000);
    } catch (e) { }
}

function removeButtonHighlight(button) {
    if (button) {
    
        // Determine what section of the website the user is currently in.
        if (currentSection == null) {
            var url            = window.location.href.replace("://", "");
            var relativePath   = url.substring(url.indexOf(pathOffWebRoot) + pathOffWebRoot.length, url.length);
            var currentSection = relativePath;
            
            if (relativePath.length > 0 && relativePath.indexOf("/") > -1) {
                currentSection = relativePath.substring(0, relativePath.indexOf("/"));
            }
        }
    
        // Find out the text in the menu item, stripped of all formatting and punctuation characters.
        if (button.buttonText == null) {
            button.getElementsByClassName('menu-main-button-content').each(function(element) {
                button.buttonText = element.innerHTML.replace(/([\W\s])/g, "");
            });
        }

        // If this is the current section of the website, do not remove the highlight from the button.
        if (button.buttonText != currentSection) {
		    button.className = "menu-main-button";
		}
	}
}

function subMenuShow(menu, subMenu) {
	if (subMenu != null) {
		animateDown(menu, subMenu, 0.08);
	}
}

function subMenuHide(menu, subMenu) {	
	if (subMenu != null) {
		if (!menu.active) { 
			animateUp(menu, subMenu, 0.08);
		}
	
	} else {
	    if (!menu.active) {
	        removeButtonHighlight($(menu.id + "-button"));
	    }
	}
	
	clearInterval(menu.interval);
}

function onSubMenuMouseOver(menu) {
	menu = $(menu);
	
	// Opera will use the :hover event to perform this effect, as it causes a redraw of the elements that breaks the layout of the menu button.
	// This cannot be done for all browsers as Internet Explorer 6 does not support the :hover method.
	if (navigator.userAgent.indexOf("Opera")== -1){
		menu.className = "menu-main-sub-item-hl";
	}
}

function onSubMenuMouseOut(menu) {
	menu = $(menu);
	menu.className = "menu-main-sub-item";
}

function animateDown(menu, subMenu, duration) {
    var endPoint = -2;
    
    animationInit(menu, subMenu);
    $(subMenu.id + "-effect-wrapper").show();

    function slideDown() {
        try { 
        
            // If menu is down, stop animating.  
            if (parseInt(subMenu.style.marginTop) == endPoint) {
                subMenu.animation = false;
                clearInterval(subMenu.interval)
            
            } else {   
                var step = animationGetStep(subMenu, duration);
                
                // If the next step would exceed the end point, adjust the step.
                if (parseInt(subMenu.style.marginTop) + step > endPoint) {
                    step = endPoint - parseInt(subMenu.style.marginTop);
                }
                
                subMenu.animation       = true;
                subMenu.style.marginTop = (parseInt(subMenu.style.marginTop) + step) + "px";  
            }  
        } catch (e) { clearInterval(subMenu.interval); }
    }

    clearInterval(subMenu.interval);
    subMenu.animation = false;
    subMenu.interval  = setInterval(slideDown, animationIntervalDelay);
}

function animateUp(menu, subMenu, duration) {
    var endPoint = subMenu.getHeight();
    
    function slideUp() { 
        try {
    
            // If the menu is up, stop animating.
            if (parseInt(subMenu.style.marginTop) * -1 == endPoint) {
                $(subMenu.id + "-effect-wrapper").hide();
                removeButtonHighlight($(menu.id + "-button"));
                
                subMenu.animation = false;
                activeSubMenus = activeSubMenus.without(menu.id);
                clearInterval(subMenu.interval)
                
            } else {     
                var step = animationGetStep(subMenu, duration);
                
                // If the next step would exceed the end point, adjust the step.
                if ((parseInt(subMenu.style.marginTop) - step) * -1 > endPoint) {
                    step = step - (((parseInt(subMenu.style.marginTop) - step) * -1) - endPoint);
                }
              
                subMenu.animation = true;
                subMenu.style.marginTop = (parseInt(subMenu.style.marginTop) - step) + "px";    
            }   
        } catch (e) { clearInterval(subMenu.interval); } 
    }

    clearInterval(subMenu.interval);
    subMenu.animation = false;
    subMenu.interval  = setInterval(slideUp, animationIntervalDelay);
}

function animationInit(menu, subMenu) {
    if (subMenu.effectInit == null) { 
    
        // Mostly needed so Opera doesn't cause the whole menu to become displaced on redraw.
        menu.style.width = menu.getWidth() + "px"; 
    
        // Do this so it doesn't flicker as we adjust heights and so forth.
        subMenu.originalMarginLeft = subMenu.style.marginLeft;
        subMenu.style.marginLeft   = "-3000px";
        
        $(subMenu.id + "-effect-wrapper").show();
             
        $(subMenu.id + "-effect-wrapper").style.width  = subMenu.getWidth() + "px";
        $(subMenu.id + "-effect-wrapper").style.height = (subMenu.getHeight() + 4) + "px";
        
        subMenu.style.marginTop  = "-" + subMenu.getHeight() + "px";
        subMenu.style.marginLeft = subMenu.originalMarginLeft;
         
        subMenu.effectInit = true;
    }
}

function animationGetStep(subMenu, duration) {
    var totalSteps = (duration * 1000) / animationIntervalDelay; 
    var step       = Math.round(subMenu.getHeight() / totalSteps);
    
    if (step <= 0) { step = 1; }
    
    return step;
}

// Makes certain that the interval to hide the menu actually gets called.
function animationValidate() {
    if (activeSubMenus.length > 0) {
	    activeSubMenus.each(function(elementId) {
	        if ($(elementId + "-sub") && !$(elementId + "-sub").animation) {
	            animateUp($(elementId), $(elementId + "-sub"), 0.08);
	        }
	    });
	}
}

// Load images needed for the menu.
// Images in hidden elements are not automatically loaded otherwise.
function loadMenuImages() {
    var path = pathOffWebRoot + "App_Assets/images/";

    var img1  = new Image();
    var img2  = new Image();
    var img3  = new Image();
    var img4  = new Image();
    var img5  = new Image();
    var img6  = new Image();
    var img7  = new Image();    
    var img8  = new Image();
    var img9  = new Image();
    var img10 = new Image();
    var img11 = new Image();
    
    img1.src = path + "bg-container-menu-main-sub-corner-topright.gif";
    img2.src = path + "bg-container-menu-main-sub-corner-bottomleft.gif";
    img3.src = path + "bg-container-menu-main-sub-corner-bottomright.gif";
    img4.src = path + "bg-container-menu-main-sub-item-corner-topleft.gif";
    img5.src = path + "bg-container-menu-main-sub-item-corner-topright.gif";
    img6.src = path + "bg-container-menu-main-sub-item-corner-bottomleft.gif";
    img7.src = path + "bg-container-menu-main-sub-item-corner-bottomright.gif";
    
    img8.src  = path + "bg-container-menu-main-button.gif";
    img9.src  = path + "bg-container-menu-main-button-left.gif";
    img10.src = path + "bg-container-menu-main-button-left-home.gif";
    img11.src = path + "bg-container-menu-main-button-right.gif";
}

//-->
