/******************************************************************************
* Menu Bar                                                                    *
* Written by Tom Anderson, derived from BrainJar script				*
* Please see http://www.brainjar.com for original.                            *
******************************************************************************/

//----------------------------------------------------------------------------
// Code to determine the browser and version.
//----------------------------------------------------------------------------

function Browser() {

  var ua, s, i;

  this.isIE    = false;  // Internet Explorer
  this.isOP    = false;  // Opera
  this.isNS    = false;  // Netscape
  this.version = null;

  ua = navigator.userAgent;

  s = "Opera";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isOP = 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;
  }

  // Treat any other "Gecko" browser as Netscape 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }

  s = "MSIE";
  if ((i = ua.indexOf(s))) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
}

var browser = new Browser();

//if (browser.isIE) alert("using IE "+browser.version);
//if (browser.isNS) alert("using NS "+browser.version);
//if (browser.isOP) alert("using OP "+browser.version);

//----------------------------------------------------------------------------
// Code for handling the menu bar and active button.
//----------------------------------------------------------------------------

var activeButton = null;

// Capture mouse clicks on the page so any active button can be
// deactivated.

if (browser.isIE) { document.onmousedown = pageMousedown; } else { document.addEventListener("mousedown", pageMousedown, true); }

function pageMousedown(event)
{
        var el;

        // If there is no active button, exit.
        if (activeButton == null) { return; }

        // Find the element that was clicked on.
        el = browser.isIE ? window.event.srcElement : event.target.tagName ? event.target : event.target.parentNode;

        // If the active button was clicked on, exit.
        if (el == activeButton) { return; }

        // If the element is not part of a menu, reset and clear the active button.
        if (getContainerWith(el, "DIV", "menu") == null)
        {
                resetButton(activeButton);
                activeButton = null;
        }
}

function buttonClick(event, menuId)
{
        var button;

        // Get the target button element.
        button = browser.isIE? window.event.srcElement : event.currentTarget;

        //debug("button " + button.id + " clicked");

        // Blur focus from the link to remove that annoying outline.
        button.blur();

        // Associate the named menu to this button if not already done.
        // Additionally, initialize menu display.

        if (button.menu == null)
        {
                button.menu = document.getElementById(menuId);
                if (button.menu.isInitialized == null) { menuInit(button.menu, null); }
        }

        // Reset the currently active button, if any.
        if (activeButton != null) { resetButton(activeButton); }

        // Activate this button, unless it was the currently active one.
        if (button != activeButton)
        {
                depressButton(button);
                activeButton = button;
        }
        else { activeButton = null; }

        button.onmouseout = function()
        {
                menuout = setTimeout(function()
                {
                        //debug(menu.id + " menu timeout activated...");
                        if (!button.menu.activeItem && button.menu.style.visibility=="visible")
                        {
                                //debug("timing out " + menu.id);
                                closeMenu(button.menu);
                        }
                }, 200);
        };

        return false;
}

function buttonMouseover(event, menuId)
{
        var button;

        // Find the target button element.
        button = browser.isIE? window.event.srcElement : event.currentTarget;

        // If any other button menu is active, make this one active instead.
        if (activeButton != null && activeButton != button) { buttonClick(event, menuId); }
}

function depressButton(button)
{
        var x, y;

        //debug("depressing " + button.id);

        // Update the button's style class to make it look like it's depressed.
        button.className += " menuButtonActive";

        // Position the associated drop down menu under the button and show it.
        //  x = getPageOffsetLeft(button);
        x = getPageOffsetLeft(button) + button.offsetWidth/2 - button.menu.offsetWidth/2;

        var maxXY = getMaxXY();
        if (x+button.menu.offsetWidth > maxXY.x) { x = maxXY.x - button.menu.offsetWidth; }

        y = getPageOffsetTop(button) + button.offsetHeight;

        // For IE, adjust position.
        if (browser.isIE)
        {
                x += button.offsetParent.clientLeft;
                y += button.offsetParent.clientTop;
        }
	  else { y += 5; }

        button.menu.style.left = x + "px";
        button.menu.style.top  = y + "px";
        button.menu.style.visibility = "visible";

        //debug("showing " + button.menu.id);
}

function resetButton(button)
{
        //debug("resetting " + button.id);

        // Restore the button's style class.
        removeClassName(button, "menuButtonActive");

        // Hide the button's menu, first closing any sub menus.
        if (button.menu != null)
        {
                closeSubMenu(button.menu);
                button.menu.style.visibility = "hidden";
        }

        // kill activeButton
        activeButton = null;
}

//----------------------------------------------------------------------------
// Code to handle the menus and sub menus.
//----------------------------------------------------------------------------

var itemout; // item timer
var menuout; // menu timer

function menuMouseover(event)
{
        var menu;

        // Find the target menu element.
        menu = browser.isIE? getContainerWith(window.event.srcElement, "DIV", "menu") : event.currentTarget;

        // cancel timeout
        //debug("clearing menu timeout on menu mouseover");
        clearTimeout(menuout);

        menu.active = 1;
        //debug("Setting " + menu.id + " active");

        // expire open menu
        //menu.onmouseout = function(){ menuout = setTimeout(function(){ if (!menu.activeItem) { menu.active = 0; } }, 200); };
        //menu.onmouseout = function(){ if (!menu.activeItem) { menu.active = 0; } };
        menu.onmouseout = function()
        {
                menuout = setTimeout(function()
                {
                        //debug(menu.id + " menu timeout activated...");
                        if (!menu.activeItem && menu.style.visibility=="visible" && (activeButton == null || menu.parent != activeButton))
                        {
                                //debug("timing out " + menu.id);
                                closeMenu(menu);
                        }
                }, 200);
        };

}

function menuItemMouseover(event, menuId)
{
        var item, menu, x, y;

        // cancel timeout
        //debug("clearing timeouts on item mouseover");
        clearTimeout(itemout);
        clearTimeout(menuout);

        // Find the target item element and its parent menu element.
        item = (browser.isIE)? getContainerWith(window.event.srcElement, "A", "menuItem") : event.currentTarget;
        menu = getContainerWith(item, "DIV", "menu");
        menu.active = 1;

        // exit if already selected
        if (menu.activeItem && menu.activeItem == item) { return; }

        // Close any active sub menu and mark this one as active.
        if (menu.activeItem) { closeSubMenu(menu); }
        menu.activeItem = item;
        //debug("Setting " + item.id + " on " + menu.id + " active");

        // Highlight the item element.
        item.className += " menuItemHighlight";
        //item.className = "menuItemHighlight";

        // show sub menu
        if (item.subMenu || document.getElementById(menuId))
        {
                // Initialize the sub menu, if not already done
                if (item.subMenu == null)
                {
                        item.subMenu = document.getElementById(menuId);
                        if (item.subMenu.isInitialized == null) { menuInit(item.subMenu,menu); }
                }

                // Get position for submenu based on the menu item.
                relX = item.offsetWidth;
                relY = item.offsetTop

                absX = getPageOffsetLeft(item) + item.offsetWidth;
                absY = getPageOffsetTop(item);

                // Adjust position to fit in view.
                var maxXY = getMaxXY();
                maxXY.absX = maxXY.x - item.subMenu.offsetWidth;
                maxXY.absY = maxXY.y - item.subMenu.offsetHeight;
                maxXY.relX = relX + maxXY.absX - absX;
                maxXY.relY = relY + maxXY.absY - absY;

                // switch sides on which submenu opens if x exceeds right edge, but don't let it go beyond the left edge
                if (relX > maxXY.relX) { relX = Math.max(0-item.subMenu.offsetWidth, 0-absX); }
                //if (x > maxXY.x) { x = Math.max(0, x - item.offsetWidth - item.subMenu.offsetWidth + (menu.offsetWidth - item.offsetWidth)); }

                // don't let y exceed the bottom of the page if possible, but definitely don't let it go above the top of the page
                relY = Math.max(0 - absY, Math.min(relY, maxXY.relY));
                //y = Math.max(0, Math.min(y, maxXY.y));

                //alert("relX = " + relX + "\nrelY = " + relY + "\nabsX = " + absX + "\nabsY = " + absY + "\nmaxXY.absX = " + maxXY.absX + "\nmaxXY.absY = " + maxXY.absY + "\nmaxXY.relX = " + maxXY.relX + "\nmaxXY.relY = " + maxXY.relY + "\nzIndex = " + (menu.style.zIndex+1));

                // Position and show it.
                item.subMenu.style.left = relX + "px";
                item.subMenu.style.top  = relY + "px";
                item.subMenu.style.zIndex = menu.style.zIndex + 1;
                item.subMenu.style.visibility = "visible";
        }

        // Stop the event from bubbling.
        //if (browser.isIE) { window.event.cancelBubble = true; } else { event.stopPropagation(); }

        // expire item
        item.onmouseout = function()
        {
                itemout = setTimeout(function()
                {
                        //debug(item.id + " item timeout activated...");
                        if (!(item.subMenu && (item.subMenu.active || item.subMenu.activeItem)))
                        {
                                if (menu.activeItem == item)
                                {
                                        //debug("timing out " + item.id);
                                        closeSubMenu(menu);
                                }

                                menuout = setTimeout(function()
                                {
                                        //debug(menu.id + " menu timeout activated from " + item.id + " timeout...");
                                        if (!menu.activeItem && !menu.active && menu.style.visibility=="visible")
                                        {
                                                //debug("timing out " + menu.id);
                                                closeMenu(menu);
                                        }
                                }, 200);
                        }
                }, 200);
        };

        //alert("set active " + item.subMenu.id + " from " + item.id);
}

function getMaxXY()
{
        var maxXY = new Object;

        if (browser.isIE)
        {
                maxXY.x = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft) + (document.documentElement.clientWidth != 0 ? document.documentElement.clientWidth : document.body.clientWidth);
                maxXY.y = Math.max(document.documentElement.scrollTop, document.body.scrollTop) + (document.documentElement.clientHeight != 0 ? document.documentElement.clientHeight : document.body.clientHeight);
        }

        if (browser.isOP)
        {
                maxXY.x = document.documentElement.scrollLeft + window.innerWidth;
                maxXY.y = document.documentElement.scrollTop  + window.innerHeight;
        }

        if (browser.isNS)
        {
                maxXY.x = window.scrollX + window.innerWidth - 19;
                maxXY.y = window.scrollY + window.innerHeight - 19;
        }

        return maxXY;
}

function closeSubMenu(menu)
{
        if (!menu || !menu.activeItem) { return; }

        // Recursively close any sub menus.
        if (menu.activeItem.subMenu)
        {
                //debug("Killing " + menu.activeItem.subMenu.id + " activeItem with subMenu.");
                closeSubMenu(menu.activeItem.subMenu);
                menu.activeItem.subMenu.style.visibility = "hidden";
                menu.activeItem.subMenu = null;
        }

        clearTimeout(itemout);
        removeClassName(menu.activeItem, "menuItemHighlight");
        menu.activeItem = null;
}

function closeMenu(menu)
{
        if (!menu || menu.activeItem) { return; }
        //debug("closing " + menu.id + " because it has no active items.");
        closeSubMenu(menu);
        menu.active = 0;
        menu.style.visibility = "hidden";

        itemout = setTimeout(function()
        {
                if (menu.parent_menu)
                {
                        //debug("timing out " + menu.parent_menu.id + " submenus, setting inactive");
                        closeSubMenu(menu.parent_menu);
                        menu.parent_menu.active = 0;
                }
        }, 200);

        menuout = setTimeout(function()
        {
                //if (menu.parent_menu) { debug("timing out " + menu.parent_menu.id + "... activeItem=" + menu.parent_menu.activeItem + ", active=" + menu.parent_menu.active); }
                if (menu.parent_menu && !menu.parent_menu.activeItem && !menu.parent_menu.active)
                {
                        closeMenu(menu.parent_menu);
                }
        }, 400);
}

//----------------------------------------------------------------------------
// Code to initialize menus.
//----------------------------------------------------------------------------

function menuInit(menu, parent_menu)
{
        var itemWidth = 0;

        var itemList = menu.getElementsByTagName("A");
        if (itemList.length > 0) { itemWidth = itemList[0].offsetWidth; } else { return; }

        for (var i = 0; i < itemList.length; i++)
        {
                if (itemList[i].parentNode == menu)
                {
                        var textEl  = null;
                        var arrowEl = null;
                        var spanList = itemList[i].getElementsByTagName("SPAN");

                        for (var 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)
                        {
                                var this_width = textEl.offsetWidth;
                                if (arrowEl != null) { this_width += arrowEl.offsetWidth; }

                                if (this_width > itemWidth)
                                {
                                        itemWidth = this_width;
                                        debug("" + itemList[i].id + " is the new max width at " + itemWidth + ".");
                                        // i = 0;
                                }
                        }
                }
        }

        var IEmod = (browser.isIE)? 16 : 0;

        for (var i = 0; i < itemList.length; i++)
        {
                if (itemList[i].parentNode == menu)
                {
                        var textEl  = null;
                        var arrowEl = null;
                        var spanList = itemList[i].getElementsByTagName("SPAN");

                        for (var 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)
                        {
                                var this_width = textEl.offsetWidth;
                                if (arrowEl != null) { this_width += arrowEl.offsetWidth - (1.5 * IEmod); }

                                var padding = (itemWidth - this_width - (2 * IEmod));
                                padding = (padding<0)? 0 : padding;
                                textEl.style.paddingRight = padding + "px";

                                debug("" + itemList[i].id + ".menuItemText.style.paddingRight = " + itemWidth + " - " + this_width + " = " + (itemWidth-this_width) + ".");

                                // For Opera, remove the negative right margin to fix a display bug.
                                if (browser.isOP && arrowEl != null) { arrowEl.style.marginRight = "0px"; }

                                //alert("name="+itemList[i].id+"\nitemWidth="+itemWidth+"\ntextEl.offsetWidth="+textEl.offsetWidth+"\narrowEl.offsetWidth="+arrowEl.offsetWidth+"\ntextEl.style.paddingRight="+textEl.style.paddingRight);
                        }
                }
        }

        if (browser.isIE)
        {
                // Fix IE hover problem by setting an explicit width on first item of the menu.
                //var w = itemList[0].offsetWidth;
                //itemList[0].style.width = w + "px";
                //var dw = itemList[0].offsetWidth - w;
                //w -= dw;
                //itemList[0].style.width = w + "px";

                // replace arrow characters.
                menu.style.lineHeight = "2.5ex";
                var 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";
                        }
                }

                itemWidth -= 16;

                //menu.style.overflow = "hidden";
        }
        else { itemWidth += 16; }

        // set the menu to the width of the largest item
        menu.style.width = itemWidth + "px";
        menu.style.clip.left = itemWidth + "px";

        // set separators to the menu width
        /*
        var sepList = menu.getElementsByTagName("DIV");
        for (i = 0; i < sepList.length; i++)
        {
                if (hasClassName(sepList[i], "menuItemSep"))
                {
                        sepList[i].style.width = itemWidth + "px";
                        sepList[i].style.height = "0px";
                }
        }
        */

        var sepList2 = menu.getElementsByTagName("HR");
        for (i = 0; i < sepList2.length; i++)
        {
                var sepwidth = (browser.isIE)? (menu.offsetWidth+3) : (menu.offsetWidth-3);
                sepList2[i].style.width =  sepwidth + "px";
                sepList2[i].style.height = "2px";
        }

        // identify parent
        menu.parent_menu = parent_menu;

        // Mark menu as initialized.
        menu.isInitialized = true;
}

//----------------------------------------------------------------------------
// General utility functions.
//----------------------------------------------------------------------------

function getContainerWith(node, tagName, className)
{
        // Starting with the given node, find the nearest containing element
        // with the specified tag name and style class.

        while (node != null)
        {
                if (node.tagName != null && node.tagName == tagName && hasClassName(node, className)) { return node; }
                node = node.parentNode;
        }

        return node;
}

function hasClassName(el, name) {

  var i, list;

  // Return true if the given element currently has the given class
  // name.

  list = el.className.split(" ");
  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;

  // Remove the given class name from the element's className property.

  newList = new Array();
  curList = el.className.split(" ");
  for (i = 0; i < curList.length; i++)
    if (curList[i] != name)
      newList.push(curList[i]);
  el.className = newList.join(" ");
}

function getPageOffsetLeft(el) {

  var x;

  // Return the x coordinate of an element relative to the page.

  x = el.offsetLeft;
  if (el.offsetParent != null)
    x += getPageOffsetLeft(el.offsetParent);

  return x;
}

function getPageOffsetTop(el) {

  var y;

  // Return the x coordinate of an element relative to the page.

  y = el.offsetTop;
  if (el.offsetParent != null)
    y += getPageOffsetTop(el.offsetParent);

  return y;
}