//*****************************************************************************
// Do not remove this notice.
// Original code Copyright 2000 by Mike Hall. (Significant edits by Don Seeley, Abbott Labs)
// See http://www.brainjar.com for terms of use.
//*****************************************************************************

//----------------------------------------------------------------------------
// Code to determine the browser and version.
//----------------------------------------------------------------------------
var s, i, os, ver, timeoutId = 0, activeButton = null, ua = navigator.userAgent;

if (ua.indexOf("IE") >= 0) {   // set IE version variable for menu positioning later on.
    i = ua.indexOf("IE");
    s = "IE";
    ver = parseFloat(ua.substr(i + 2));
}

function killTimeout() {
    if (timeoutId != 0){
        clearTimeout(timeoutId);
        timeoutId=0;
    }
}
 
function Browser() {
    if (!Array.prototype.push) {   // This fixes the lack of support for the push() function in IE5.0
        Array.prototype.push = function() {
            for (var i=0; i<arguments.length; i++)
                this[this.length] = arguments[i];
        }
    }

    this.isIEMac = false;  // IE Mac
    this.isIE    = false;  // Internet Explorer
    this.isNS    = false;  // Netscape
    this.isOther = false;  // anthing esle
    this.version = null;

    s="Opera";
    if ((i=ua.indexOf(s)) >= 0){
        this.isOther = true;
        i += 1; // correct for variations in how "Opera" id displayed in userAgent string.
        this.version = parseFloat(ua.substr(i + s.length));
        ver = 5.01;
        return;
    }

    s = "IE";
    os = "Mac";
    if ( ((i = ua.indexOf(s)) >= 0) && (ua.indexOf(os) >= 0)) {
        this.isIEMac = true;
        this.version = parseFloat(ua.substr(i + s.length));
        return;
    }

    s = "MSIE"; // but only if greater than 4.x
    if ( ((i = ua.indexOf(s)) >= 0) && (parseFloat(ua.substr(i + s.length)) > 4.99) ) {
        this.isIE = true;
        this.version = parseFloat(ua.substr(i + s.length));
        return;
    }
    s = "Netscape6/";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isNS = true;
        this.version = parseFloat(ua.substr(i + s.length));
        return;
    }

    s = "Gecko";      // Treat any other "Gecko" browser as NS 6.1.
    if ((i = ua.indexOf(s)) >= 0) {
        this.isNS = true;
        this.version = 6.1;
        return;
    }

    this.isOther = true;
    return;
}

    var browser = new Browser();

//----------------------------------------------------------------------------
// Code for handling the menu bar and active button.
//----------------------------------------------------------------------------
//    var activeButton = null;

function buttonClick(event, menuId) {
    if (browser.isOther) return;

    killTimeout();
    var button;

    if (browser.isIE)      // Get the target button element.
        button = window.event.srcElement;
    else
        button = event.currentTarget;
                        /* button.blur(); // Blur focus from the link to remove that annoying outline.  */

    if (button.menu == null) {   // Associate the named menu to this button if not already done. Additionally, initialize menu display.
        button.menu = document.getElementById(menuId);
    if (button.menu.isInitialized == null)
        menuInit(button.menu);
      }

    if (button.onmouseout == null)      // Set mouseout event handler for the button, if not already done.
        button.onmouseout = buttonMouseout; //button.onmouseout = buttonOrMenuMouseout;
        
    if (button == activeButton)    // Exit if this button is the currently active one.
        return false;
    if (activeButton != null)      // Reset the currently active button, if any.
        resetButton(activeButton);

    if (button != activeButton) {      // Activate this button, unless it was the currently active one.
        depressButton(button);//
        activeButton = button;
    }
    else
        activeButton = null;

    return false;
    }

function buttonMouseover(event, menuId) {
    killTimeout();
    var button;
    if (activeButton == null) {    // Activates this button's menu if no other is currently active.
        buttonClick(event, menuId);
        return;
    }

    if (browser.isIE)    // Find the target button element.
        button = window.event.srcElement;
    else
        button = event.currentTarget;

    if (activeButton != null && activeButton != button)    // If any other button menu is active, make this one active instead.
    buttonClick(event, menuId);
}

function depressButton(button) {
    var x, y;
    button.className += " menuButtonActive"; // Update the button's style class to make it look like it's depressed.

    if (button.onmouseout == null)    // Set mouseout event handler for the button, if not already done.
        button.onmouseout = buttonMouseout; // button.onmouseout = buttonOrMenuMouseout;
    if (button.menu.onmouseout == null)
        button.menu.onmouseout = menuMouseout;
    
    x = getPageOffsetLeft(button);    // Position the associated drop down menu under the button and show it.
    y = getPageOffsetTop(button) + button.offsetHeight;
    
    if (browser.isIE && ver >= 5.5) {    // For IE 5.5, adjust position.
        x += button.offsetParent.clientLeft;
        y += button.offsetParent.clientTop;
    }
    
    if (browser.isIE && ( (ver > 4.99) && (ver < 5.1) )) {    // For IE 5.0, adjust position.
        y += 8;
        x += button.offsetParent.clientLeft;
        y += button.offsetParent.clientTop;
    }
    
    button.menu.style.left = x - 2 + "px"; // - 2 added by J.Benton for v12.2 menu modification
    button.menu.style.top  = y - 2 + "px"; // - 2 added by J.Benton for v12.2 menu modification
    button.menu.style.visibility = "visible";
}



function resetButton(button) {
    removeClassName(button, "menuButtonActive");    // Restore the button's style class.    
    if (button.menu != null) {    // Hide the button's menu, first closing any sub menus.    
        closeSubMenu(button.menu);
        button.menu.style.visibility = "hidden";
    }
}

//----------------------------------------------------------------------------
// Code to handle the menus and sub menus.
//----------------------------------------------------------------------------    
function menuMouseover(event) {
killTimeout();
    var menu;
    
    if (browser.isIE)    // Find the target menu element.
        menu = getContainerWith(window.event.srcElement, "DIV", "menu");
    else
        menu = event.currentTarget;

    if (menu.activeItem != null)    // Close any active sub menu.
        closeSubMenu(menu);
}

function menuItemMouseover(event, menuId) {
killTimeout();
    var item, menu, x, y;
    
    if (browser.isIE)    // Find the target item element and its parent menu element.
        item = getContainerWith(window.event.srcElement, "A", "menuItem");
    else
        item = event.currentTarget;
    menu = getContainerWith(item, "DIV", "menu");
    
    if (menu.activeItem != null)    // Close any active sub menu and mark this one as active.
        closeSubMenu(menu);
    menu.activeItem = item;
    
    item.className += " menuItemHighlight";    // Highlight the item element.

    if (item.subMenu == null) {    // Initialize the sub menu, if not already done.
        item.subMenu = document.getElementById(menuId);
    if (item.subMenu.isInitialized == null)
        menuInit(item.subMenu);
    }
    
    if (item.subMenu.onmouseout == null)// Set mouseout event handler for the sub menu, if not already done.
        item.subMenu.onmouseout = menuMouseout;

      x = getPageOffsetLeft(item) + item.offsetWidth;      // Get position for submenu based on the menu item.
      y = getPageOffsetTop(item);
    
      var maxX, maxY;      // Adjust position to fit in view.
    
      if (browser.isNS) {
        maxX = window.scrollX + window.innerWidth;
        maxY = window.scrollY + window.innerHeight;
      }
      
      if (browser.isIE) { //no beta yet
        maxX = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft) + (document.documentElement.clientWidth != 0 ? document.documentElement.clientWidth : document.body.clientWidth);
        maxY = Math.max(document.documentElement.scrollTop, document.body.scrollTop) +  (document.documentElement.clientHeight != 0 ? document.documentElement.clientHeight : document.body.clientHeight);
      }
      
      maxX -= item.subMenu.offsetWidth;
      maxY -= item.subMenu.offsetHeight;
    
      if (x > maxX)
        x = Math.max(0, x - item.offsetWidth - item.subMenu.offsetWidth + (menu.offsetWidth - item.offsetWidth));
        y = Math.max(0, Math.min(y, maxY));
    
      item.subMenu.style.left = x + "px";      // Position and show the sub menu.
      item.subMenu.style.top  = y + "px";
      item.subMenu.style.visibility = "visible";
    
      if (browser.isIE)      // Stop the event from bubbling.
        window.event.cancelBubble = true;
      else
        event.stopPropagation();
}
    
function closeSubMenu(menu) {
    if (menu == null || menu.activeItem == null)
        return;

    if (menu.activeItem.subMenu != null) {    // Recursively close any sub menus.
        closeSubMenu(menu.activeItem.subMenu);
        menu.activeItem.subMenu.style.visibility = "hidden";
        menu.activeItem.subMenu = null;
    }
        removeClassName(menu.activeItem, "menuItemHighlight");
        menu.activeItem = null;
}

function buttonMouseout(event) {//  Handler for mouseout event on buttons.
        killTimeout();    
        var el;

        if (activeButton == null)        // If there is no active button, exit.
            return;

        if (browser.isIE)        // Find the element the mouse is moving to.
            el = window.event.toElement;
        else if (event.relatedTarget != null)
            el = (event.relatedTarget.tagName ? event.relatedTarget : event.relatedTarget.parentNode);

        if (getContainerWith(el, "DIV", "menu") == null) {        // If the element is not part of a menu, reset the active button.
            resetButton(activeButton);
            activeButton = null;
        }
    }

function menuMouseout(event) {    //  Handler for mouseout event on buttons and menus.
        var el;

        if (activeButton == null) // If there is no active button, exit.
            return;

        if (browser.isIE)         // Find the element the mouse is moving to.
            el = window.event.toElement;
        else if (event.relatedTarget != null)
            el = (event.relatedTarget.tagName ? event.relatedTarget : event.relatedTarget.parentNode);
            
        killTimeout();    
        if (getContainerWith(el, "DIV", "menu") == null) {   // If the element is not part of a menu, reset the active button.
            timeoutId = 0
            timeoutId = setTimeout("resetButton(activeButton);activeButton = null;",750);
//          window.status = timeoutId
        }
    }


//----------------------------------------------------------------------------
// Code to initialize menus.
//----------------------------------------------------------------------------
function menuInit(menu) {
      var itemList, spanList;
      var textEl, arrowEl;
      var itemWidth;
      var w, dw;
      var i, j;
    
      // For IE, replace arrow characters.
      if (browser.isIE) {
        menu.style.lineHeight = "1.5ex";
        spanList = menu.getElementsByTagName("SPAN");
        for (i = 0; i < spanList.length; i++)
          if (hasClassName(spanList[i], "menuItemArrow")) {
            spanList[i].style.fontFamily = "Webdings";
            spanList[i].firstChild.nodeValue = "4";
          }
      }
    
      itemList = menu.getElementsByTagName("A");      // Find the width of a menu item.
      if (itemList.length > 0) itemWidth = itemList[0].offsetWidth;
      else return;
    
    for (i = 0; i < itemList.length; i++) {      // For items with arrows, add padding to item text to make the arrows flush right.
        spanList = itemList[i].getElementsByTagName("SPAN");
        textEl  = null;
        arrowEl = null;
        for (j = 0; j < spanList.length; j++) {
            if (hasClassName(spanList[j], "menuItemText"))
                textEl = spanList[j];
            if (hasClassName(spanList[j], "menuItemArrow"))
                arrowEl = spanList[j];
    }

    if (textEl != null && arrowEl != null)
        textEl.style.paddingRight = (itemWidth - (textEl.offsetWidth + arrowEl.offsetWidth)) + "px";
    }
    
    if (browser.isIE && (ver >= 5.5)) {      // Fix IE hover problem in 5.5 by setting an explicit width on first item of the menu.
        w = itemList[0].offsetWidth;
        itemList[0].style.width = w + "px";
        dw = itemList[0].offsetWidth - w;
        w -= dw;
        itemList[0].style.width = w + "px";
    }
    
    if (browser.isIE && ( (ver > 4.99) && (ver < 5.1) )) {      // Fix IE hover problem in 5.0 by setting an explicit width on all items of the menu.
        var i;
        for (i = 0; i < itemList.length; i++){
            w = itemList[i].offsetWidth;
            itemList[i].style.width = w + "px";
            dw = itemList[i].offsetWidth - w;
            w -= dw;
            itemList[i].style.width = w + "px";
        }
    }
    
      menu.isInitialized = true;      // Mark menu as initialized.
}
    
//----------------------------------------------------------------------------
// General utility functions.
//----------------------------------------------------------------------------
function getContainerWith(node, tagName, className) {
      while (node != null) {      // Starting with the given node, find the nearest containing element with the specified tag name and style class.
        if (node.tagName != null && node.tagName == tagName && hasClassName(node, className))
          return node;
        node = node.parentNode;
      }
      return node;
    }
    
function hasClassName(el, name) {
      var i, list;
      list = el.className.split(" ");      // Return true if the given element currently has the given class name.
      for (i = 0; i < list.length; i++)
        if (list[i] == name)
          return true;
      return false;
    }
    
function removeClassName(el, name) {
      var i, curList, newList;
      if (el.className == null)
        return;
      newList = new Array();      // Remove the given class name from the element's className property.
      curList = el.className.split(" ");
      for (i = 0; i < curList.length; i++)
        if (curList[i] != name)
          newList.push(curList[i]); // this is the offending EI5.0 line. removing it, the menus collapse, but lose css formatting. push() is not supported in IE4.0. perhaps not here either.
      el.className = newList.join(" ");
    }

function getPageOffsetLeft(el) {
      var x;
      x = el.offsetLeft;      // Return the x coordinate of an element relative to the page.
      if (el.offsetParent != null)
        x += getPageOffsetLeft(el.offsetParent);
      return x;
    }
    
function getPageOffsetTop(el) {
      var y;
      y = el.offsetTop;      // Return the x coordinate of an element relative to the page.
      if (el.offsetParent != null)
        y += getPageOffsetTop(el.offsetParent);
      return y;
    }

function goOtherPage(page){
	window.location.href = page;
}
