/******************************************************************************
* Menu Bar                                                                    *
*                                                                             *
* Do not remove this notice.                                                  *
*                                                                             *
* Copyright 2000-2002 by Mike Hall                                            *
* Please see http://www.brainjar.com for terms of use.                        *
******************************************************************************/

//----------------------------------------------------------------------------
// Code to determine the Browser_sup and version.
//----------------------------------------------------------------------------

function Browser_sup() {

  var ua, s, i;

  this.isIE    = false;  // Internet Explorer
  this.isNS    = false;  // Netscape
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    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;
  }

  // Treat any other "Gecko" Browser_sup as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var Browser_sup = new Browser_sup();

//----------------------------------------------------------------------------
// Code for handling the menu bar and active button.
//----------------------------------------------------------------------------

var activeButton_sup = null;
// Capture mouse clicks on the page so any active button can be
// deactivated.

if (Browser_sup.isIE){
  document.onclick = pageMousedown_sup;
}else{
  document.addEventListener("mousedown", pageMousedown_sup, true);
}
function pageMousedown_sup(event) {

  var el;
  // If there is no active button, exit.

  if (activeButton_sup == null)
    return;

  // Find the element that was clicked on.

  if (Browser_sup.isIE)
    el = window.event.srcElement;
  else
    el = (event.target.tagName ? event.target : event.target.parentNode);

  // If the active button was clicked on, exit.

  if (el == activeButton_sup)
    return;

  // If the element is not part of a menu, reset and clear the active
  // button.

  if (getContainerWith_sup(el, "DIV", "menu_sup") == null) {
    resetButton_sup(activeButton_sup);
    activeButton_sup = null;
  }
}

function buttonClick_sup(event, menuId) {
  var button;

  // Get the target button element.

  if (Browser_sup.isIE)
    button = window.event.srcElement;
  else
    button = event.currentTarget;

  // 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_sup(button.menu);
  }

  // Reset the currently active button, if any.

  if (activeButton_sup != null)
    resetButton_sup(activeButton_sup);

  // Activate this button, unless it was the currently active one.

  if (button != activeButton_sup) {
    depressButton_sup(button);
    activeButton_sup = button;
  }
  else
    activeButton_sup = null;

  return false;
}

function buttonMouseover_sup(event, menuId) {

  var button;

  // Find the target button element.

  if (Browser_sup.isIE)
    button = window.event.srcElement;
  else
    button = event.currentTarget;

  // If any other button menu is active, make this one active instead.

  if (activeButton_sup != null && activeButton_sup != button)
    buttonClick_sup(event, menuId);
}

function depressButton_sup(button) {

  var x, y;

  // 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_sup(button);
   y = getPageOffsetTop_sup(button) + button.offsetHeight;

  button.menu.style.left = x + "px";
  button.menu.style.top  = y + "px";
  button.menu.style.visibility = "visible";
}

function resetButton_sup(button) {

  // Restore the button's style class.

  removeClassName_sup(button, "menuButtonActive");

  // Hide the button's menu, first closing any sub menus.

  if (button.menu != null) {
    closeSubMenu_sup(button.menu);
    button.menu.style.visibility = "hidden";
  }
}

//----------------------------------------------------------------------------
// Code to handle the menus and sub menus.
//----------------------------------------------------------------------------

function menuMouseover_sup(event) {

  var menu;

  // Find the target menu element.

  if (Browser_sup.isIE)
    menu = getContainerWith_sup(window.event.srcElement, "DIV", "menu_sup");
  else
    menu = event.currentTarget;

  // Close any active sub menu.

  if (menu.activeItem_sup != null)
    closeSubMenu_sup(menu);
}



function submenuItemMouseover_sup(event, menuId) {

  var item, menu;

  // Find the target item element and its parent menu element.

  if (Browser_sup.isIE)
    item = getContainerWith_sup(window.event.srcElement, "A", "menuItem_sup");
  else
    item = event.currentTarget;
  menu = getContainerWith_sup(item, "DIV", "menu_sup");

  // Close any active sub menu and mark this one as active.
  if (menu.activeItem_sup != null)
    closeSubMenu_sup(menu);
  menu.activeItem_sup = item;

  // Highlight the item element.

  item.className += " menuItemHighlight";

  // Initialize the sub menu, if not already done.

  if (item.subMenu_sup == null) {
    item.subMenu_sup = document.getElementById(menuId);
    if (item.subMenu_sup.isInitialized == null)
      menuInit_sup(item.subMenu_sup);
  }

  item.subMenu_sup.style.visibility = "visible";

  // Stop the event from bubbling.

  if (Browser_sup.isIE)
    window.event.cancelBubble = true;
  else
    event.stopPropagation();
}





function menuItemMouseover_sup(event, menuId) {

  var item, menu, x, y;

  // Find the target item element and its parent menu element.

  if (Browser_sup.isIE)
    item = getContainerWith_sup(window.event.srcElement, "A", "menuItem_sup");
  else
    item = event.currentTarget;
  menu = getContainerWith_sup(item, "DIV", "menu_sup");

  // Close any active sub menu and mark this one as active.
  if (menu.activeItem_sup != null)
    closeSubMenu_sup(menu);
  menu.activeItem_sup = item;

  // Highlight the item element.

  item.className += " menuItemHighlight";

  // Initialize the sub menu, if not already done.

  if (item.subMenu_sup == null) {
    item.subMenu_sup = document.getElementById(menuId);
    if (item.subMenu_sup.isInitialized == null)
      menuInit_sup(item.subMenu_sup);
  }

  // Get position for subMenu_sup based on the menu item.

   x = getContainerWith_sup(item) + item.offsetWidth - 2;
   y = getPageOffsetTop_sup(item) - getPageOffsetTop_sup(menu) - 1;

  // Adjust position to fit in view.

  var maxX, maxY;

  if (Browser_sup.isNS) {
    maxX = window.scrollX + window.innerWidth;
    maxY = window.scrollY + window.innerHeight;
  }
  if (Browser_sup.isIE) {
    maxX = (document.documentElement.scrollLeft   != 0 ? document.documentElement.scrollLeft   : document.body.scrollLeft)
         + (document.documentElement.clientWidth  != 0 ? document.documentElement.clientWidth  : document.body.clientWidth);
    maxY = (document.documentElement.scrollTop    != 0 ? document.documentElement.scrollTop    : document.body.scrollTop)
         + (document.documentElement.clientHeight != 0 ? document.documentElement.clientHeight : document.body.clientHeight);
  }
  maxX -= item.subMenu_sup.offsetWidth;
  maxY -= item.subMenu_sup.offsetHeight;

  if (x > maxX)
    x = Math.max(0, x - item.offsetWidth - item.subMenu_sup.offsetWidth
      + (menu.offsetWidth - item.offsetWidth));
  y = Math.max(0, Math.min(y, maxY));

  // Position and show it.

  item.subMenu_sup.style.left = x + "px";
  item.subMenu_sup.style.top  = y + "px";
  item.subMenu_sup.style.visibility = "visible";

  // Stop the event from bubbling.

  if (Browser_sup.isIE)
    window.event.cancelBubble = true;
  else
    event.stopPropagation();
}

function closeSubMenu_sup(menu) {

  if (menu == null || menu.activeItem_sup == null)
    return;

  // Recursively close any sub menus.

  if (menu.activeItem_sup.subMenu_sup != null) {
    closeSubMenu_sup(menu.activeItem_sup.subMenu_sup);
    menu.activeItem_sup.subMenu_sup.style.visibility = "hidden";
    menu.activeItem_sup.subMenu_sup = null;
  }
  removeClassName_sup(menu.activeItem_sup, "menuItemHighlight");
  menu.activeItem_sup = null;
}

//----------------------------------------------------------------------------
// Code to initialize menus.
//----------------------------------------------------------------------------

function menuInit_sup(menu) {

  var itemList, spanList;
  var textEl, arrowEl;
  var itemWidth;
  var w, dw;
  var i, j;

  // For IE, replace arrow characters.

  if (Browser_sup.isIE) {
    menu.style.lineHeight = "1ex";
    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";
      }
  }

  // Find the width of a menu item.

  itemList = menu.getElementsByTagName("A");
  if (itemList.length > 0)
    itemWidth = itemList[0].offsetWidth;
  else
    return;

  // For items with arrows, add padding to item text to make the
  // arrows flush right.

  for (i = 0; i < itemList.length; i++) {
    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";
  }

  // Fix IE hover problem by setting an explicit width on first item of
  // the menu.

  if (Browser_sup.isIE) {
    w = itemList[0].offsetWidth;
    itemList[0].style.width = w + "px";
    dw = itemList[0].offsetWidth - w;
    w -= dw;
    itemList[0].style.width = w + "px";
  }

  // Mark menu as initialized.

  menu.isInitialized = true;
}

//----------------------------------------------------------------------------
// General utility functions.
//----------------------------------------------------------------------------

function getContainerWith_sup(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_sup(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_sup(el) {

  var x;

  // Return the x coordinate of an element relative to the page.

  x = el.offsetLeft;
  if (el.offsetParent != null)
    x += getPageOffsetLeft_sup(el.offsetParent);

  return x;
}

function getPageOffsetTop_sup(el) {

  var y;

  // Return the x coordinate of an element relative to the page.

  y = el.offsetTop;
  if (el.offsetParent != null)
    y += getPageOffsetTop_sup(el.offsetParent);

  return y;
}
