// global menu state
var menuReady = false;

// pre-cache menubar image pairs
if (document.images) {
    var imagesNormal = new Array();
    imagesNormal["Solar"] = new Image(20, 198);
    imagesNormal["Solar"].src = "images/button_solar.jpg";
    imagesNormal["Semiconductor"] = new Image(20, 198);
    imagesNormal["Semiconductor"].src  = "images/button_semiconductor.jpg";
    imagesNormal["Lifescience"] = new Image (20, 198);
    imagesNormal["Lifescience"].src = "images/button_lifescience.jpg";
    imagesNormal["Ultrasonic"] = new Image(20, 198);
    imagesNormal["Ultrasonic"].src  = "images/button_ultrasonic.jpg";
	imagesNormal["Megasonic"] = new Image(20, 198);
    imagesNormal["Megasonic"].src  = "images/button_megasonic.jpg";



    var imagesHilite = new Array();
    imagesNormal["Solar"] = new Image(20, 198);
    imagesNormal["Solar"].src = "images/button_solar.jpg";
    imagesNormal["Semiconductor"] = new Image(20, 198);
    imagesNormal["Semiconductor"].src  = "images/button_semiconductor.jpg";
    imagesNormal["Lifescience"] = new Image (20, 198);
    imagesNormal["Lifescience"].src = "images/button_lifescience.jpg";
    imagesNormal["Ultrasonic"] = new Image(20, 198);
    imagesNormal["Ultrasonic"].src  = "images/button_ultrasonic.jpg";
	imagesNormal["Megasonic"] = new Image(20, 198);
    imagesNormal["Megasonic"].src  = "images/button_megasonic.jpg";
}

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:"18px",
                     menuItemLineHeight:"1.4em",
                     menuWrapperBorderWidth:"0px",
                     menuWrapperPadding:"3px",
                     defaultBodyFontSize:"12px"
               
                    };

// specifications for menu contents and menubar image associations
var menus = new Array();
menus[0] = {mBarImgId:"menuImg_1",
            mBarImgNormal:imagesNormal["Solar"],
            mBarImgHilite:imagesHilite["Solar"],
            menuItems:[ {text:"Solar Heaters", href:"heaters.htm"},
                        {text:"Wafer Spin Processors", href:"wafer_spin_processor.htm"},
                        {text:"Chemical Process", href:"heaters.htm"},
                        {text:"Contact Us", href:"contact.htm"},
                        {text:"Site Map", href:"sitemap.htm"}
					   ],
            elemId:""
           };
menus[1] = {mBarImgId:"menuImg_2", 
            mBarImgNormal:imagesNormal["Semiconductor"],
            mBarImgHilite:imagesHilite["Semiconductor"],
            menuItems:[ {text:"Particle Counters", href:"particlecounters.htm"},
                        {text:"Cleanroom Cranes", href:"cranes.htm"},
                        {text:"Cleanroom Hoists", href:"cranes.htm"},
                        {text:"Portable Lifting Devices", href:"portable.htm"},
                        {text:"Platform Lift", href:"VRC.htm"},
                        {text:"Mezzanine Lift", href:"VRC.htm"},
                        {text:"Heaters", href:"heaters.htm"},
                        {text:"Wafer Spin Processors", href:"wafer_spin_processor.htm"},
                        {text:"Cleanroom design and Build", href:"cleanroom.htm"},
                        {text:"Bellows- PTFE/ Metal", href:"bellows.htm"},
                        {text:"Chemical Process", href:"heaters.htm"},
                        {text:"Metal Cabinets", href:"cabinets.htm"},
                        {text:"Contact Us", href:"contact.htm"},
                        {text:"Site Map", href:"sitemap.htm"}

                      ],
            elemId:""
           };
menus[2] = {mBarImgId:"menuImg_3",
            mBarImgNormal:imagesNormal["Lifescience"],
            mBarImgHilite:imagesHilite["Lifescience"],
            menuItems:[ {text:"Particle Counters", href:"particlecounters.htm"},
                        {text:"Heaters", href:"heaters.htm"},
                        {text:"Cleanroom Hoists", href:"cranes.htm"},
                        {text:"Cleanroom Cranes", href:"cranes.htm"},
                        {text:"Portable lifting Devices", href:"portable.htm"},
                        {text:"Platform Lift", href:"VRC.htm"},
                        {text:"Mezzanine Lift", href:"VRC.htm"},
                        {text:"Safety Enclosures", href:"containment_transfer.htm"},
                        {text:"Barriers/ Isolators", href:"Barrier_isolators.htm"},
                        {text:"Granulators", href:"Barrier_isolators.htm"},
                        {text:"Bio-Decontamination", href:"decontamination.htm"},
                        {text:"Vial Filling, Inspecting", href:"vial_products.htm"},
                        {text:"Vial Transporting", href:"Barrier_isolators.htm"},
                        {text:"Hygienic Bins, IBC's", href:"hygienic_bins.htm"},
                        {text:"V-Shell Blenders", href:"blenders.htm"},
                        {text:"Conical Blenders", href:"blenders.htm"},
                        {text:"Mixing, Sieving", href:"blenders.htm"},
                        {text:"Fluid bed processors", href:"mills_processors.htm"},
                        {text:"Laboratory Mills", href:"mills_processors.htm"},
                        {text:"Sanitary Valves", href:"mills_processors.htm"},
                        {text:"Washing systems", href:"mills_processors.htm"},
                        {text:"Cleanroom Design/ Build", href:"cleanroom.htm"},
                        {text:"Bellows- PTFE/ Metal", href:"bellows.htm"},
                        {text:"Chemical Process", href:"heaters.htm"},
                        {text:"Metal Cabinets", href:"cabinets.htm"},
                        {text:"Contact Us", href:"contact.htm"},
                        {text:"Site Map", href:"sitemap.htm"}

                        
                      ],
            elemId:""
           };
menus[3] = {mBarImgId:"menuImg_4",
			mBarImgNormal:imagesNormal["Ultrasonic"],
            mBarImgHilite:imagesHilite["Ultrasonic"],
            menuItems:[{text:"Frequency Reference Chart", href:"UltrasonicPDF/Frequency_Spectrum.pdf"},
                        {text:"Solar Manufacturing", href:"UltrasonicPDF/Product_Catalog_0508.pdf"},
                        {text:"Semiconductor", href:"UltrasonicPDF/Product_Catalog_0508.pdf"},
                        {text:"Disk Drive", href:"UltrasonicPDF/Product_Catalog_0508.pdf"},
                        {text:"Optics", href:"UltrasonicPDF/Product_Catalog_0508.pdf"},
                        {text:"Medical Devices", href:"UltrasonicPDF/Pharmaceutical_A_0409.pdf"},
                        {text:"Pharmaceutical", href:"UltrasonicPDF/Pharmaceutical_A_0409.pdf"},
                        {text:"GMP", href:"UltrasonicPDF/GMP-WashMaster_10.4.6.pdf"},
                        {text:"Injection Molding", href:"UltrasonicPDF/Mold_Cleaning_Brief.pdf"},
                        {text:"Solvent Cleaning", href:"UltrasonicPDF/HT-IPA_0808-1.pdf"},
                        {text:"Automated Systems", href:"UltrasonicPDF/Aquaris-Atlantis_Literature.pdf"},
                        {text:"Paper - Printing", href:"UltrasonicPDF/Printing_Industry.pdf"},
                        {text:"Other", href:"UltrasonicPDF/Product_Catalog_0508.pdf"}
                        
				      ],
            elemId:""
           };
menus[4] = {mBarImgId:"menuImg_5",
			mBarImgNormal:imagesNormal["Megasonic"],
            mBarImgHilite:imagesHilite["Megasonic"],
            menuItems:[{text:"Frequency Reference Chart", href:"UltrasonicPDF/Frequency_Spectrum.pdf"},
                        {text:"Solar Manufacturing", href:"UltrasonicPDF/megasonic_sheet.pdf"},
                        {text:"Semiconductor", href:"UltrasonicPDF/megasonic_sheet.pdf"},
                        {text:"Disk Drive", href:"UltrasonicPDF/megasonic_sheet.pdf"},
                        {text:"Optics", href:"UltrasonicPDF/megasonic_sheet.pdf"},
                        {text:"Super Critical Cleaning", href:"UltrasonicPDF/megasonic_sheet.pdf"}
					  
					  ],
            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;
    }
}
