﻿// JScript File

function renderDropDowns(newState) {
    
    var elements = document.documentElement.getElementsByTagName('select');
 
    for (var i=0; i<elements.length; i++) {
        elements[i].style.visibility = newState;
    }
}

var timer = null; 
var menuFinalMargin;
var menuMoving;
var cancelClose;

function moveMenu()
{
    if (menuMoving != null) {
        menuMoving.style.marginTop = parseInt(menuMoving.style.marginTop.replace(new RegExp("em"),"")) + 1 + "em";
        if (parseInt(menuMoving.style.marginTop.replace(new RegExp("em"),"")) >= parseInt(menuFinalMargin.replace(new RegExp("em"),""))) {
            menuMoving.style.marginTop = menuFinalMargin;
            menuMoving = null;
            timer = null;            
            return;
        }
   
        timer = window.setTimeout("moveMenu()",20);
    }
}

function showMenuLevel(e,srcCount)
{
    if (!e) var e = window.event;
    cancelClose = true;
    menu = document.getElementById("menu_" + srcCount);

    var subMenuDiv  = menu.nextSibling;
    renderDropDowns("hidden");
    hideMenus(menu);
    

    if (subMenuDiv) {
        if (subMenuDiv.tagName == "DIV" && subMenuDiv.style.display != "block") {            
            menuFinalMargin = "-2.3em";
            subMenuDiv.style.display="block"; 
            subMenuDiv.style.marginTop="-10em";
            menuMoving = subMenuDiv;
            moveMenu();
        }
    }
    else
        subMenuDiv = menu;
    // Show parent menus
    
    while (subMenuDiv.className != "menuRollOut") {
        subMenuDiv = subMenuDiv.parentNode;
        subMenuDiv.style.display = "block";
    }
    

	// Stop event from bubbling otherwise menus with always be hidden by mouseover event on the page
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

function hideMenus(currentMenu)
{
    var menu = document.getElementById("mainMenu");
    var menus = menu.getElementsByTagName("DIV");
    var subMenus;
    var subMenusVisibile;
    
    if (currentMenu)
        currentMenu = currentMenu.parentNode;
    else
        currentMenu = menu; // Dummy setting to fail tests
    
    // Due to an IE bug, we need to hide the children before we hide the parents
    var count=1;
    while (count > 0) {
        count = 0;
        for (var i=0;i<menus.length;i++) {
            // See if all the sub menus are already hidden
            if (menus[i].style.display=="block") {
                if (menus[i].id != currentMenu.id) count++;
                subMenus = menus[i].getElementsByTagName("DIV");
                subMenusVisibile = false;
                for (var j=0;j<subMenus.length;j++) {
                    if (subMenus[j].style.display=="block") 
                        if (menus[i].id != currentMenu.id) subMenusVisibile = true;
                }
                if (subMenusVisibile == false)
                    if (menus[i].id != currentMenu.id) menus[i].style.display = "none";
            }
        }   
    }
}

function initMenuLevel(src)
{   
    var iFrame = document.createElement("IFRAME");
    iFrame.className = "mask";
    var menu = document.getElementById("mainMenu");
    var menus = menu.getElementsByTagName("a");
    var enterScript;
    for (var i=0;i<menus.length;i++) {
        menus[i].id = "menu_" + i;
        enterScript = "showMenuLevel(e," + i + ");";
        menus[i].onmouseover = Function ("e",enterScript);
        try {
            if (menus[i].nextSibling.tagName == "DIV") {
                menus[i].name = "menuMore";        
                menus[i].innerHTML += "&nbsp;...";
                menus[i].nextSibling.id = "div_" + i;
            }
        }
        catch (ex) {
        }
    }
}

function closeMenus()
{
    cancelClose = false;
    timer = window.setTimeout("closeMenusFinal()",500);
}

function closeMenusFinal()
{
    if (!cancelClose) {
        hideMenus();
        renderDropDowns('visible');
    }
}



