//*****************************************************************************
// Do not remove this notice.
//
// Copyright 2000-2004 by Mike Hall.
// See http://www.brainjar.com for terms of use.
//*****************************************************************************

//----------------------------------------------------------------------------
// Check for specific browsers.
//----------------------------------------------------------------------------

var isIE      = (document.all != null && window.opera == null);
var isOpera   = (window.opera != null);
var isMozilla = (window.scrollX != null);

//----------------------------------------------------------------------------
// Define a global variable for the currently active button.
//----------------------------------------------------------------------------

var activeButton = null;

//----------------------------------------------------------------------------
// Capture mouse clicks on the page.
//----------------------------------------------------------------------------

if (isIE)
  document.onmousedown = pageMousedown;
else
  document.addEventListener("mousedown", pageMousedown, true);

//----------------------------------------------------------------------------
// Event handler for mouse clicks on the page.
//----------------------------------------------------------------------------

function pageMousedown(event) {

  // If there is no active button, exit.

  if (activeButton == null)
    return;

  // Find the element that was clicked on.

  var el;
  if (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)
    return;

  // If the element is not part of a menu, clear the active button.

  if (getContainerWith(el, "DIV", "menu") == null)
    clearActiveButton();
}

//----------------------------------------------------------------------------
// Menu bar button functions.
//----------------------------------------------------------------------------

function setActiveButton(button) {

  // Clear the currently active button, if any.

  clearActiveButton();

  // Update the button's style to make it look like it's depressed.

  addClassName(button, "menuButtonActive");

  // Get a position for the button's menu just under it.

  var x = getPageOffsetLeft(button);
  var y = getPageOffsetTop(button) + button.offsetHeight;

  // For IE, adjust the position.

  if (isIE) {
    x += button.offsetParent.clientLeft;
    y += button.offsetParent.clientTop;
  }

  // Open the button's menu.

  menuOpen(button.menu, x, y);

  // Save the active button.

  activeButton = button;
}

function clearActiveButton() {

  // Exit if there is no button currently active.

  if (activeButton == null)
    return;

  // Restore the button's style.

  removeClassName(activeButton, "menuButtonActive");

  // Close the button's menu.

  if (activeButton.menu != null)
    menuClose(activeButton.menu)

  // Clear the active button.

  activeButton = null;
}

//----------------------------------------------------------------------------
// Event handlers for the menu bar buttons.
//----------------------------------------------------------------------------

function buttonClick(event, menuId) {

  // Get the target button element.

  var button;
  if (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. Also,
  // initialize the menu.

  if (button.menu == null) {
    button.menu = document.getElementById(menuId);
    menuInitialize(button.menu);
  }

  // If this button is currently active, clear it. Otherwise, make it the
  // active button.

  if (button == activeButton)
    clearActiveButton();
  else
    setActiveButton(button);

  return false;
}

function buttonMouseover(event, menuId) {

  // Find the target button element.

  var button;
  if (isIE)
    button = window.event.srcElement;
  else
    button = event.currentTarget;

  // If any other button menu is active, make this one active instead.

  if (activeButton != null && activeButton != button)
    buttonClick(event, menuId);
}

//----------------------------------------------------------------------------
// Menu functions.
//----------------------------------------------------------------------------

function menuInitialize(menu) {

  // Get a list of all the item link elements in the menu.

  var itemList = menu.getElementsByTagName("A");

  // Get the width of the first item.

  if (itemList.length > 0)
    itemWidth = itemList[0].offsetWidth;
  else
    return;

  // Handle IE glitches.

  if (isIE) {

    // Replace arrow characters.

    menu.style.lineHeight = "2.5ex";
    spanList = menu.getElementsByTagName("SPAN");
    for (var i = 0; i < spanList.length; i++)
      if (hasClassName(spanList[i], "menuItemArrow")) {
        spanList[i].style.fontFamily = "Webdings";
        spanList[i].firstChild.nodeValue = "4";
      }

    // Insert an IFRAME element before the menu element so we can prevent
    // SELECT elements from bleeding through.

    menu.iframeEl = menu.parentNode.insertBefore(document.createElement("IFRAME"), menu);
    menu.iframeEl.style.display = "none";
    menu.iframeEl.style.position = "absolute";
  }

  // For items with arrows, add padding to the item text to make the arrows
  // flush right.

  for (var i = 0; i < itemList.length; i++) {
    var spanList = itemList[i].getElementsByTagName("SPAN");
    var textEl  = null;
    var arrowEl = null;
    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 && arrowEl != null) {
      textEl.style.paddingRight = (itemWidth 
        - (textEl.offsetWidth + arrowEl.offsetWidth)) + "px";

      // For Opera, remove the negative right margin to fix a display bug.

      if (isOpera)
        arrowEl.style.marginRight = "0em";
    }
  }

  // Fix the IE hover problem by setting an explicit width on first item of the
  // menu.

  if (isIE) {
    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";
  }

	// Set event handler.
	menu.onmouseover = menuMouseover;
}

function menuOpen(menu, x, y) {

	// Position the menu and make it visible.

  menu.style.left = x + "px";
  menu.style.top  = y + "px";
  menu.style.visibility = "visible";

	// For IE; size, position and display the IFRAME.

  if (menu.iframeEl != null) {
    menu.iframeEl.style.left = menu.style.left;
    menu.iframeEl.style.top  = menu.style.top;
    menu.iframeEl.style.width  = menu.offsetWidth  + "px";
    menu.iframeEl.style.height = menu.offsetHeight + "px";
    menu.iframeEl.style.display = "";
  }
}

function menuClose(menu) {

  // Close any open sub menu.

  menuClearActiveItem(menu);

  // Make the menu invisible.

  menu.style.visibility = "hidden";

	// For IE, hide the IFRAME as well.

  if (menu.iframeEl != null)
    menu.iframeEl.style.display = "none";
}

function menuSetActiveItem(item) {

  // Clear the currently active item on the parent menu, if any.

  menuClearActiveItem(item.parentMenu);

  // Highlight the menu item.

  addClassName(item, "menuItemHighlight");

  // Get a position for the sub menu based on the item's position.

  var x = getPageOffsetLeft(item) + item.offsetWidth;
  var y = getPageOffsetTop(item);

  // If necessary, adjust the sub menu's position to fit in within browser's
	// viewport.

  var maxX, maxY;
  if (isIE) {
    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);
  }
  if (isOpera) {
    maxX = document.documentElement.scrollLeft + window.innerWidth;
    maxY = document.documentElement.scrollTop  + window.innerHeight;
  }
  if (isMozilla) {
    maxX = window.scrollX + window.innerWidth;
    maxY = window.scrollY + window.innerHeight;
  }
  maxX -= item.subMenu.offsetWidth;
  maxY -= item.subMenu.offsetHeight;

  if (x > maxX)
    x = Math.max(0, x - item.offsetWidth - item.subMenu.offsetWidth + (item.parentMenu.offsetWidth - item.offsetWidth));
  y = Math.max(0, Math.min(y, maxY));

  // Open the item's sub menu.

  menuOpen(item.subMenu, x, y);

  // Mark this item as the active one for the parent menu.

  item.parentMenu.activeItem = item;
}

function menuClearActiveItem(menu) {

  // Exit if the menu currently has no item active.

	if (menu.activeItem == null)
    return;

  // Restore the item's style and close its sub menu.

  removeClassName(menu.activeItem, "menuItemHighlight");
  menuClose(menu.activeItem.subMenu);

  // Clear the active item for the menu.

  menu.activeItem = null;
}

//----------------------------------------------------------------------------
// Event handlers for the menus and items.
//----------------------------------------------------------------------------

function menuMouseover(event) {

  // Find the menu element.

  var menu;
  if (isIE)
    menu = getContainerWith(window.event.srcElement, "DIV", "menu");
  else
    menu = event.currentTarget;

  // Clear any currently active item.

  menuClearActiveItem(menu);
}

function menuItemMouseover(event, menuId) {

  // Find the target item element.

  var item;
  if (isIE)
    item = getContainerWith(window.event.srcElement, "A", "menuItem");
  else
    item = event.currentTarget;

  // If the item and its sub menu have not been initialized yet, do so.

  if (item.subMenu == null) {
    item.subMenu = document.getElementById(menuId);
    menuInitialize(item.subMenu);
    item.parentMenu = getContainerWith(item, "DIV", "menu");
  }

  // Make this item the active one for its menu.

  menuSetActiveItem(item);

  // Stop the event from bubbling.

  if (isIE)
    window.event.cancelBubble = true;
  else
    event.stopPropagation();
}

//----------------------------------------------------------------------------
// 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) {

  // Return true if the given element currently has the given class name.

  var list = el.className.split(" ");
  for (var i = 0; i < list.length; i++)
    if (list[i] == name)
      return true;

  return false;
}

function addClassName(el, name) {

  if (!hasClassName(el, name))
    el.className += " " + name;
}

function removeClassName(el, name) {

  if (el.className == null)
    return;

  // Remove the given class name from the element's className property.

  var newList = new Array();
  var curList = el.className.split(" ");
  for (var i = 0; i < curList.length; i++)
    if (curList[i] != name)
      newList.push(curList[i]);
    el.className = newList.join(" ");
}

function getPageOffsetLeft(el) {

  // Return the x coordinate of an element relative to the page.

  var x = el.offsetLeft;
  if (el.offsetParent != null)
    x += getPageOffsetLeft(el.offsetParent);

  return x;
}

function getPageOffsetTop(el) {

  // Return the y coordinate of an element relative to the page.

  var y = el.offsetTop;
  if (el.offsetParent != null)
    y += getPageOffsetTop(el.offsetParent);

  return y;
}
