var menu_backgroundHiliteClass          = "menu_item_text_hover";
var menu_backgroundNotHiliteClass       = "menu_item_text";
var menu_background2HiliteClass         = "menu_submenu_icon_hover";
var menu_background2NotHiliteClass      = "menu_submenu_icon";
var menu_borderHiliteClass              = "menu_border_hilite";
var menu_borderNotHiliteClass           = "menu_border";

var onWindowsIE = (navigator.appVersion.indexOf("MSIE") != -1 &&
                navigator.appVersion.indexOf("Windows") != -1);

// Menu scripts
var visibleSubMenu      = null;
var subMenuToShow       = null;
var subMenuToHide       = null;
var reenterMenuCount    = 0;
var submenu_shadows     = new Array

function menuFindParentCell(el)
{
    var curEl = el;
    while (curEl != null && curEl.className != "menu_item")
        curEl = curEl.parentElement;
    return curEl;
}

function setTableCellClass(tbl, row, col, cssClass)
{
    cell = tbl.tBodies[0].rows[row].cells[col];
    if (cell != null)
        cell.className = cssClass;
}

function menu_onfocus(el)
{
    menu_onmouseover(el);
}

function menu_onblur(el)
{
    menu_onmouseout(el);
}

function menu_onmouseover(el)
{
    var menuCell = menuFindParentCell(el);
    if (menuCell == null)
        return;
    
    setTableCellClass(menuCell, 0, 0, menu_borderHiliteClass);
    setTableCellClass(menuCell, 1, 0, menu_borderHiliteClass);
    setTableCellClass(menuCell, 1, 3, menu_borderHiliteClass);
    setTableCellClass(menuCell, 2, 0, menu_borderHiliteClass);

    setTableCellClass(menuCell, 1, 1, menu_backgroundHiliteClass);
    setTableCellClass(menuCell, 1, 2, menu_background2HiliteClass);
}

function menu_onmouseout(el)
{
    var menuCell = menuFindParentCell(el);
    if (menuCell == null)
        return;

    setTableCellClass(menuCell, 0, 0, menu_borderNotHiliteClass);
    setTableCellClass(menuCell, 1, 0, menu_borderNotHiliteClass);
    setTableCellClass(menuCell, 1, 3, menu_borderNotHiliteClass);
    setTableCellClass(menuCell, 2, 0, menu_borderNotHiliteClass);

    setTableCellClass(menuCell, 1, 1, menu_backgroundNotHiliteClass);
    setTableCellClass(menuCell, 1, 2, menu_background2NotHiliteClass);
}

function menu_onclick(el, href, openInNewWindow)
{
    var menuCell = menuFindParentCell(el);

    if (menuCell == null)
        return;

    if (href != null)
    {
        // Is the user holding down the shift key?
        if (openInNewWindow || window.event.shiftKey)
            window.open(href);
        else
            window.location = href;
    }
}

function combo_disable(menuID)
{
    var el = document.getElementById("root" + menuID);
    if (el == null)
        return;
        
    el.className = "combo_table_disabled";
}

function combo_enable(menuID)
{
    var el = document.getElementById("root" + menuID);
    if (el == null)
        return;
    
    el.className = "combo_table";
}

function combo_show(menuID)
{
    var el = document.getElementById("root" + menuID);
    if (el == null)
        return;
        
    if (el.className == "combo_table_disabled")
        return;
        
    var subMenu = document.getElementById(menuID);
    if (subMenu == null)
        return;
    
    submenu_adjustDimensions(subMenu);
        
    // Position the submenu relative to its parent menu     
    subMenu.style.top = (submenu_calcMenuTop(el, menuID) + el.offsetHeight) + "px";
    subMenu.style.left = submenu_calcMenuLeft(el) + "px";
    subMenu.style.width = el.style.width;

    subMenuToShow = subMenu;
    setTimeout("submenu_deferredDisplaySubMenu(\'" + menuID + "\')", 2);
        
    var curSelectedIndex = 0;
    curSelectedIndex = el.style.VSComboSelectedIndex;
    setTimeout("combo_set_option_focus('" + menuID + "'," + curSelectedIndex + ")", 10);

    var bodyEl = document.getElementById("body");
    if (bodyEl == null)
        return;
        
    bodyEl.onclick = combo_hide;
}

function combo_set_option_focus(menuID, optionIndex)
{
    var targetElt = document.getElementById(menuID+"_anchor_"+optionIndex);
    if (targetElt != null)
        targetElt.focus()
}

// global variable hack because you can't define arguments to the onclick function
var combo_scrolling = false;

function combo_anchorkeyuphandler(menuID)
{
    // on a up/down arrow, open up the menu
    if (window.event.keyCode == 38 || window.event.keyCode == 40)
    {
        // if it's already visible entry
        var el = document.getElementById(menuID);
        if (el != null && el != visibleSubMenu)
            combo_show(menuID);
    }
}

function combo_anchorkeydownhandler(menuID)
{
    // on a tab, close up the menu
    if (window.event.keyCode == 9)
    {
        combo_hide(menuID);
    }
}

function combo_anchorfocus(menuID)
{
    var el = document.getElementById("root" + menuID);
    if (el == null)
        return;
    if (el.className == "combo_table_disabled")
        return;
        
    document.getElementById("text" + menuID).className = "combo_option_selected";
}

function combo_anchorblur(menuID)
{
    document.getElementById("text" + menuID).className = "combo_text";
}

function combo_hide()
{
    submenu_hide();
}

function combo_optionkeyhandler(el)
{
    // identify the combo container and index
    var comboName = el.style.VSComboContainer;
    var comboIndex = parseInt(el.style.VSComboIndex, 10);
    var targetIndex;
    
    //window.alert(window.event.keyCode);
    
    if (window.event.keyCode == 38) 
    {
        targetIndex = comboIndex - 1;
    }
    if (window.event.keyCode == 40) 
    {
        targetIndex = comboIndex + 1;
    }
    
    //window.alert("looking for combo_"+comboName+"_option_"+targetIndex);  
    
    var targetElt = document.getElementById("combo_"+comboName+"_anchor_"+targetIndex);
    if (targetElt != null)
    {
        targetElt.focus();
        
        var targetOptionElt = document.getElementById("combo_"+comboName+"_option_"+targetIndex);
        combo_scrolling = true;
        targetOptionElt.onclick();
        combo_scrolling = false;
    }
}

function combo_optionkeydownhandler(menuID)
{
    // on a tab, close up the menu
    if (window.event.keyCode == 9)
    {
        combo_hide(menuID);
    }
}

function combo_option(el, formfield, val, textval)
{
    var formEl = document.getElementById(formfield);
    if (formEl == null)
        return;
        
    formEl.value = val;
    document.getElementById("textcombo_" + formfield).innerHTML = textval;
    
    if (formEl.onchange != null)
        formEl.onchange();
        
    el.className = "combo_option_selected";
    
    var rootComboEl = document.getElementById("rootcombo_" + formfield);
    if (rootComboEl != null)
        rootComboEl.style.VSComboSelectedIndex = el.style.VSComboIndex;
    
    if (!combo_scrolling) {
        combo_hide();
        document.getElementById("anchorcombo_" + formfield).focus();
    }
}

function combo_mouseover(el)
{
    if (el == null) return;
    var elId = el.id;
    var a = elId.lastIndexOf("_");
    var i;
    for (i = 0; i >= 0; i++)
    {
        var elt = document.getElementById(elId.substring(0, a+1) + i);
        if (elt != null) {
            elt.className = "combo_option";
        } else {
            i = -2;
        }
    }
    
    el.className = "combo_option_hovered";
}

function combo_mouseout(el)
{
    if (el.className == "combo_option_hovered")
        el.className = "combo_option";
}

function submenu_calcMenuTop(subMenu, menuID)
{
    var curEl = subMenu;
    var topVal = 0;
    while (curEl != null)
    {
        topVal += curEl.offsetTop;
        curEl = curEl.offsetParent;
    }
    
    var newMenu = document.getElementById(menuID);
    if (newMenu != null)
    {
        var docTop = 0;
        var screenHeight = 0;
        // ie
        if (document.all != null)
        {
            if (document.documentElement.clientHeight != null)
            {
                docTop = document.documentElement.scrollTop;
                screenHeight = document.documentElement.clientHeight;
            }
            else
            {
                docTop = document.body.scrollTop;
                screenHeight = document.body.clientHeight;
            }
        }
        // netscape or mozilla
        else
        {
            docTop = window.pageYOffset;
                screenHeight = window.innerHeight;
        }
            
        if (topVal - docTop + newMenu.offsetHeight > screenHeight)
            topVal -= newMenu.offsetHeight - 21;

        if (topVal < docTop)
            topVal = docTop - 2;
    }
    
    return topVal;
}

function submenu_calcMenuLeft(subMenu)
{
    var curEl = subMenu;
    var leftVal = 0;
    while (curEl != null)
    {
        leftVal += curEl.offsetLeft;
        curEl = curEl.offsetParent;
    }

    return leftVal;
}

function submenu_keypressopen(el, menuID)
{
    // if the key is left arrow, open the menu
    if (window.event.keyCode == 39)
    {
        var containingTable = el.offsetParent.offsetParent;
        if (containingTable != null)
        {
            submenu_onmouseover(containingTable, menuID);
            containingTable.focus;
        }
    }
}

function submenu_tabfocusin(el, menuID)
{
    menu_onmouseover(el);
}

function submenu_tabfocusout(el, menuID)
{
    var focusEl = document.activeElement;
    
    var menuCell = menuFindParentCell(focusEl);
    if (menuCell != null)
    {
        var activeMenuID = menuCell.style.VSMenuContainer;
        if (activeMenuID == menuID)
            return false;
    }
        
    var containingTable = el.offsetParent.offsetParent;
    if (containingTable != null)
        submenu_onmouseout(containingTable, menuID);
}

function submenu_adjustDimensions(subMenu)
{
    maxHeight = parseInt(subMenu.style.customHeight);
    maxWidth  = parseInt(subMenu.style.customWidth);
    bHorScroll  = (subMenu.style.customScroll == "XScroll" || subMenu.style.customScroll == "XYScroll") ? true : false;
    bVerScroll  = (subMenu.style.customScroll == "YScroll" || subMenu.style.customScroll == "XYScroll") ? true : false;

    hScrollAdded = false;

    if (maxWidth > 0)
    {
        if (bHorScroll)
        {
            // see if we need to validate the maximum width requirements
            if (maxWidth > 0)
            {
                if (subMenu.offsetWidth > maxWidth)
                {
                    subMenu.style.width = maxWidth + "px";
                    subMenu.style.overflowX = "auto";
    
                    // adjust maxWidth with the new value of offsetWidth
                    // This is required, so that we don't enter this function
                    // as offsetWidth will always be a bit higher than maxWidth
                    subMenu.style.customWidth = subMenu.offsetWidth;
    
                    // add the scroll bar height to current size, because
                    // otherwise in extreme case, when there is only one VM
                    // and it has very long name, the horizontal scroll bar
                    // will hide its name.
                    subMenu.style.height = subMenu.offsetHeight + 16; // 16 is approximate height of scroll bar
    
                    // So that we know we added a horizontal scroll bar
                    hScrollAdded = true;
                }
            }
        }
        else
        {
            subMenu.style.width = (maxWidth + "px");
            subMenu.style.overflowX = "hidden";
        }
    }

    if (maxHeight > 0)      
    {
        if (bVerScroll)
        {
            // Now verify that we haven't exceeded maximum height
            if (subMenu.offsetHeight > maxHeight)
            {
                subMenu.style.height = maxHeight  + "px";
                subMenu.style.overflowY = "auto";
                
                // adjust the height so that we don't enter this path again
                // unless the page is refreshed
                subMenu.style.customHeight = subMenu.offsetHeight;

                // if we didn't add a horizontal scroll bar, that there may be
                // a case when all the VM are only one letter name, and in that
                // case, the scroll bar will hide their names, so we need to increase
                // the width
                if (hScrollAdded == false)
                {
                    subMenu.style.width = subMenu.offsetWidth + 16; // 16 is approximate width of scroll bar
                }
            }
        }
        else
        {
            subMenu.style.height = (maxHeight + "px");
            subMenu.style.overflowY = "hidden";
        }
    }
}

function submenu_onmouseover(el, menuID)
{
    menu_onmouseover(el);

    var subMenu = document.getElementById(menuID);
    if (subMenu == null)
        return;

    submenu_adjustDimensions(subMenu);

    // See if we should cancel a previous attempt to hide this menu.
    if (subMenu == visibleSubMenu)
    {
        reenterMenuCount++;
    }
    else
    {
        // Position the submenu relative to its parent menu     
        subMenu.style.top = submenu_calcMenuTop(el, menuID) + "px";
        subMenu.style.left = (submenu_calcMenuLeft(el) + el.offsetWidth) + "px";

        subMenuToShow = subMenu;
        setTimeout("submenu_deferredDisplaySubMenu(\'" + menuID + "\')", 100);
    }
}

function submenu_deferredDisplaySubMenu(menuID)
{
    var subMenu = document.getElementById(menuID);
    if (subMenu == null)
        return;

    if (subMenu == subMenuToShow)
    {
        if (visibleSubMenu != subMenuToShow)
        {
            submenu_hide();
            visibleSubMenu = subMenu;
            subMenuToShow = null;
            subMenu.style.visibility = "visible";

            if (onWindowsIE)
            {
                // Look for the submenu container.
                var container = subMenu;
                while (container != null && container.className != "submenu_container")
                    container = container.parentElement;
                if (container == null)
                    return;
                
                container.style.zIndex = 5;
                submenu_makeDropShadow(container);
            }
        }
    }
}

function submenu_onmouseout(el, menuID)
{
    menu_onmouseout(el);

    var subMenu = document.getElementById(menuID);
    if (subMenu == null)
        return;

    if (subMenu == visibleSubMenu)
    {
        subMenuToHide = subMenu;
        
        // Schedule the submenu to go away if we don't re-enter the
        // submenu before the timer fires.
        setTimeout("submenu_deferredHideSubMenu(\'" + menuID + "\', " + reenterMenuCount + ")", 100);
    }
    else if (subMenu == subMenuToShow)
    {
        subMenuToShow = null;
    }
}

function submenu_deferredHideSubMenu(menuID, outCount)
{
    var subMenu = document.getElementById(menuID);
    if (subMenu == null)
        return;

    if (visibleSubMenu == subMenuToHide && 
        subMenu == subMenuToHide && 
        outCount == reenterMenuCount)
    {
        submenu_hide();
    }
}

function submenu_hide()
{
    if (visibleSubMenu != null)
    {
        if (onWindowsIE)
            submenu_destroyDropShadow();

        visibleSubMenu.style.visibility = "hidden";
        visibleSubMenu = null;
        subMenuToHide = null;
    }
}

function submenu_makeDropShadow(subMenu)
{
    var rectIndex;
    
    for (rectIndex = 4; rectIndex > 0; rectIndex--)
    {
        var rect = document.createElement('div');
        var rectStyle = rect.style
        rectStyle.position = "absolute";
        rectStyle.left = (subMenu.style.posLeft + rectIndex) + 'px';
        rectStyle.top = (subMenu.style.posTop + rectIndex) + 'px';
        rectStyle.width = subMenu.offsetWidth + 'px';
        rectStyle.height = subMenu.offsetHeight + 'px';
        rectStyle.zIndex = subMenu.style.zIndex - rectIndex;
        rectStyle.backgroundColor = '#888888';
        var opacity = 1 - rectIndex / (rectIndex + 1);
        rectStyle.filter = "alpha(opacity = " + (100 * opacity) + ")";
        subMenu.insertAdjacentElement("afterEnd", rect);
        
        // Track the rects so we can destroy them later
        submenu_shadows[submenu_shadows.length] = rect;
    }
}

function submenu_destroyDropShadow()
{
    if (submenu_shadows != null)
    {
        var rectIndex;
        for (rectIndex = 0; rectIndex < submenu_shadows.length; rectIndex++)
            submenu_shadows[rectIndex].removeNode(true);
        submenu_shadows = new Array();
    }
}

