// global menu state
var menuReady = false;

// pre-cache menubar image pairs
if (document.images) {
    var imagesNormal = new Array();
    imagesNormal["home"] = new Image(25, 100);
    imagesNormal["home"].src = "../../meridianoff.gif";
    imagesNormal["catalog"] = new Image(25, 100);
    imagesNormal["catalog"].src  = "../../norstaroff.gif";
    imagesNormal["about"] = new Image(25, 100);
    imagesNormal["about"].src  = "../../siemensoff.gif";
    imagesNormal["rolm"] = new Image(25, 100);
    imagesNormal["rolm"].src  = "../../rolmoff.gif";
    imagesNormal["avaya"] = new Image(25, 100);
    imagesNormal["avaya"].src  = "../../avayaoff.gif";
    imagesNormal["accessories"] = new Image(25, 100);
    imagesNormal["accessories"].src  = "../../accessoroff.gif";

    var imagesHilite = new Array();
    imagesHilite["home"] = new Image(25, 100);
    imagesHilite["home"].src = "../../meridianon.gif";
    imagesHilite["catalog"] = new Image(25, 100);
    imagesHilite["catalog"].src  = "../../norstaron.gif";
    imagesHilite["about"] = new Image(25, 100);
    imagesHilite["about"].src  = "../../siemenson.gif";
    imagesHilite["rolm"] = new Image(25, 100);
    imagesHilite["rolm"].src  = "../../rolmon.gif";
    imagesHilite["avaya"] = new Image(25, 100);
    imagesHilite["avaya"].src  = "../../avayaon.gif";
    imagesHilite["accessories"] = new Image(25, 100);
    imagesHilite["accessories"].src  = "../../accessoron.gif";
}

function getElementStyle(elem, IEStyleProp, CSSStyleProp) {
    if (elem.currentStyle) {
        return elem.currentStyle[IEStyleProp];
    } else if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(elem, "");
        return compStyle.getPropertyValue(CSSStyleProp);
    }
    return "";
}

// carry over some critical menu style sheet attribute values
var CSSRuleValues = {menuItemHeight:"20px",
                     menuItemLineHeight:"2.2em",
                     menuWrapperBorderWidth:"1px",
                     menuWrapperPadding:"3px",
                     defaultBodyFontSize:"8pt"
                    };

// specifications for menu contents and menubar image associations
var menus = new Array();
menus[0] = {mBarImgId:"menuImg_1",
            mBarImgNormal:imagesNormal["home"],
            mBarImgHilite:imagesHilite["home"],
            menuItems:[ {text:"Business Phones", href:"../../phones/meridian/index1.php"},
            			{text:"Data Networks", href:"../../data/index.php"},
						{text:"Symposium Call Center", href:"../../systems/nortel/symposium.php"},
						{text:"CS1000 IP Systems", href:"../../systems/nortel/cs1000.php"},
						{text:"Max Call Center", href:"../../systems/meridian/max.php"},
						{text:"Meridian PBX Systems", href:"../../systems/meridian/index1.php"},
						{text:"Nortel Messaging", href:"../../voicemail/meridian/index1.php"},
                        {text:"Troubleshooting", href:"../../techtips/troubleshooting.php"},
			{text:"Support", href:"../../support.php"}
		      ],
            elemId:""
           };
menus[1] = {mBarImgId:"menuImg_2",
            mBarImgNormal:imagesNormal["catalog"],
            mBarImgHilite:imagesHilite["catalog"],

            menuItems:[ {text:"Norstar Phones", href:"../../phones/norstar/index1.php"},
                        {text:"Norstar Voicemail", href:"../../voicemail/norstar/index1.php"},
                        {text:"ICS Key Systems", href:"../../systems/norstar/index1.php"},
                        {text:"BCM (recommended)", href:"../../systems/nortel/bcm.php"},
                        {text:"Troubleshooting", href:"../../techtips/troubleshooting.php"},
			{text:"Support", href:"../../support.php"}
		      ],
            elemId:""
           };
menus[2] = {mBarImgId:"menuImg_3", 
            mBarImgNormal:imagesNormal["about"],
            mBarImgHilite:imagesHilite["about"],
            menuItems:[ {text:"Phones", href:"../../phones/siemens/index1.php"},
                        {text:"Phonemail", href:"../../voicemail/siemens/index1.php"},
						{text:"Hicom Systems", href:"../../systems/siemens/index1.php?hicom"},
                        {text:"Hipath IP Systems", href:"../../systems/siemens/index1.php?hipath"},
                        {text:"Documentation", href:"../../techtips/users_guides.php?manufacturerid=3"},
                        {text:"Troubleshooting", href:"../../techtips/troubleshooting.php"},
			{text:"Support", href:"../../support.php"}
                      ],
            elemId:""
           };
menus[3] = {mBarImgId:"menuImg_4", 
            mBarImgNormal:imagesNormal["rolm"],
            mBarImgHilite:imagesHilite["rolm"],
            menuItems:[ {text:"Classic Phones", href:"../../phones/rolm/index1.php"},
                        {text:"Phonemail", href:"../../voicemail/siemens/index1.php"},
                        {text:"Documentation", href:"../../techtips/users_guides.php?manufacturerid=3"},
                        {text:"Troubleshooting", href:"../../techtips/troubleshooting.php"},
			{text:"Support", href:"../../support.php"}
                      ],
            elemId:""
           };
menus[4] = {mBarImgId:"menuImg_5", 
            mBarImgNormal:imagesNormal["avaya"],
            mBarImgHilite:imagesHilite["avaya"],
            menuItems:[ {text:"Phones", href:"../../phones/avaya/index1.php"},
                        {text:"Voicemail Options", href:"../../voicemail/avaya/index1.php"},
                        {text:"Definity Systems", href:"../../systems/avaya/index1.php?definity"},
						{text:"Partner Systems", href:"../../systems/avaya/index1.php?partner"},
                        {text:"Merlin Legend/Magix", href:"../../systems/avaya/index1.php?merlin"},
						{text:"Documentation", href:"../../techtips/users_guides.php?manufacturerid=5"},
                        {text:"Troubleshooting", href:"../../techtips/troubleshooting.php"},
						{text:"Support", href:"../../support.php"}
                      ],
            elemId:""
           };
menus[5] = {mBarImgId:"menuImg_6", 
            mBarImgNormal:imagesNormal["accessories"],
            mBarImgHilite:imagesHilite["accessories"],
            menuItems:[ {text:"Conference Phones", href:"../../accessories/conference/index1.php"},
                        {text:"Headsets", href:"../../accessories/headsets/index1.php"},
						{text:"Analog Phones", href:"../../accessories/analog/index1.php"},
                        {text:"Installation Equipment", href:"../../accessories/install/index1.php"},
                        {text:"UPS", href:"../../acccessories/UPS/index1.php"},
                        {text:"Call Accounting", href:"../../callaccount/index1.php"},
                        {text:"Featured Products", href:"../../specials/index.php"},
						{text:"Support", href:"../../support.php"}
                      ],
            elemId:""
           };

// create hash table-like lookup for menu objects with id string indexes
function makeHashes() {
    for (var i = 0; i < menus.length; i++) {
        menus[menus[i].elemId] = menus[i];
        menus[menus[i].mBarImgId] = menus[i];
    }
}

// assign menu label image event handlers
function assignLabelEvents() {
    var elem;
    for (var i = 0; i < menus.length; i++) {
        elem = document.getElementById(menus[i].mBarImgId);
        elem.onmouseover = swap;
        elem.onmouseout = swap;
    }
}

// invoked from init(), generates the menu div elements and their contents.
// all this action is invisible to user during construction
function makeMenus() {
    var menuDiv, menuItem, itemLink, mbarImg, textNode, offsetLeft, offsetTop;
    
    // determine key adjustment factors for the total height of menu divs
    
    var menuItemH = 0;
    var bodyFontSize = parseInt(getElementStyle(document.body, "fontSize", "font-size"));
    // test to see if browser's font size has been adjusted by the user
    // and that the new size registers as an applied style property
    if (bodyFontSize == parseInt(CSSRuleValues.defaultBodyFontSize)) {
        menuItemH = (parseFloat(CSSRuleValues.menuItemHeight));
    } else {
        // works nicely in Netscape 7
        menuItemH = parseInt(parseFloat(CSSRuleValues.menuItemLineHeight) * bodyFontSize);
    }
    var heightAdjust = parseInt(CSSRuleValues.menuWrapperPadding) + 
        parseInt(CSSRuleValues.menuWrapperBorderWidth);
    if (navigator.appName == "Microsoft Internet Explorer" && 
        navigator.userAgent.indexOf("Win") != -1 && 
        (typeof document.compatMode == "undefined" || 
        document.compatMode == "BackCompat")) {
        heightAdjust = -heightAdjust;
    }
    
    // use menus array to drive div creation loop
    for (var i = 0; i < menus.length; i++) {
        menuDiv = document.createElement("div");
        menuDiv.id = "popupmenu" + i;
        // preserve menu's ID as property of the menus array item
        menus[i].elemId = "popupmenu" + i;
        menuDiv.className = "menuWrapper";
        if (menus[i].menuItems.length > 0) {
            menuDiv.style.height = (menuItemH * menus[i].menuItems.length) - 
            heightAdjust + "px";
        } else {
            // don't display any menu div lacking menu items
            menuDiv.style.display = "none";
        }
        // define event handlers
        menuDiv.onmouseover = keepMenu;
        menuDiv.onmouseout = requestHide;

        // set stacking order in case other layers are around the page
        menuDiv.style.zIndex = 1000;
        
        // assemble menu item elements for inside menu div
        for (var j = 0; j < menus[i].menuItems.length; j++) {
            menuItem = document.createElement("div");
            menuItem.id = "popupmenuItem_" + i + "_" + j;
            menuItem.className = "menuItem";
            menuItem.onmouseover = toggleHighlight;
            menuItem.onmouseout = toggleHighlight;
            menuItem.onclick = hideMenus;
            menuItem.style.top = menuItemH * j + "px";
            itemLink = document.createElement("a");
            itemLink.href = menus[i].menuItems[j].href;
            itemLink.className = "menuItem";
            itemLink.onmouseover = toggleHighlight;
            itemLink.onmouseout = toggleHighlight;
            textNode = document.createTextNode(menus[i].menuItems[j].text);
            itemLink.appendChild(textNode);
            menuItem.appendChild(itemLink);
            menuDiv.appendChild(menuItem);
        }
        // append each menu div to the body
        document.body.appendChild(menuDiv);
    }
    makeHashes();
    assignLabelEvents();
    // pre-position menu
    for (i = 0; i < menus.length; i++) {
        positionMenu(menus[i].elemId);
    }
    menuReady = true;
}

// initialize global that helps manage menu hiding
var timer;

// invoked from mouseovers inside menus to cancel hide
// request from mouseout of menu bar image et al.
function keepMenu() {
    clearTimeout(timer);
}

function cancelAll() {
    keepMenu();
    menuReady = false;
}

// invoked from mouseouts to request hiding all menus
// in 1/4 second, unless cancelled
function requestHide() {
    timer = setTimeout("hideMenus()", 250);
}

// "brute force" hiding of all menus and restoration
// of normal menu bar images
function hideMenus() {
    for (var i = 0; i < menus.length; i++) {
       document.getElementById(menus[i].mBarImgId).src = menus[i].mBarImgNormal.src;
       var menu = document.getElementById(menus[i].elemId)
       menu.style.visibility = "hidden";
    }
}

// set menu position just before displaying it
function positionMenu(menuId){
    // use the menu bar image for position reference of related div
    var mBarImg = document.getElementById(menus[menuId].mBarImgId);
    var offsetTrail = mBarImg;
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    var menuDiv = document.getElementById(menuId);
    menuDiv.style.left = offsetLeft + "px";
    menuDiv.style.top = offsetTop + mBarImg.height + "px";
}

// display a particular menu div
function showMenu(menuId) {
    if (menuReady) {
        keepMenu();
        hideMenus();
        positionMenu(menuId);
        var menu = document.getElementById(menuId);
        menu.style.visibility = "visible";
    }
}

// menu bar image swapping, invoked from mouse events in menu bar
// swap style sheets for menu items during rollovers
function toggleHighlight(evt) {
    evt = (evt) ? evt : ((event) ? event : null);
    if (typeof menuReady != "undefined") {
        if (menuReady && evt) {
            var elem = (evt.target) ? evt.target : evt.srcElement;
            if (elem.nodeType == 3) {
                elem = elem.parentNode;
            }
            if (evt.type == "mouseover") {
                keepMenu();
                elem.className ="menuItemOn";
            } else {
                elem.className ="menuItem";
                requestHide();
            }
            evt.cancelBubble = true;
        }
    }
}

function swap(evt) {
    evt = (evt) ? evt : ((event) ? event : null);
    if (typeof menuReady != "undefined") {
        if (evt && (document.getElementById && document.styleSheets) && menuReady) {
            var elem = (evt.target) ? evt.target : evt.srcElement;
            if (elem.className == "menuImg") {
                if (evt.type == "mouseover") {
                    showMenu(menus[elem.id].elemId);
                    elem.src = menus[elem.id].mBarImgHilite.src;
                } else if (evt.type == "mouseout") {
                    requestHide();
                }
                evt.cancelBubble = true;
            }
        }
    }
}

// create menus only if key items are supported
function initMenus() {
    if (document.getElementById && document.styleSheets) {
        setTimeout("makeMenus()", 5);
        window.onunload=cancelAll;
    }
}
