/*
** v2.0.20060106
**
** gMenu is a customizable collection of JavaScript routines which can be used
** to quickly create platform independent drop down menus.
**
** gMenu has been tested on IE 6.0, Netscape 7.1, FireFox 1.0, and Opera 8.5
**
** Copyright (C) 2004-2005  Brain Book Software LLC (formfields.com, info@formfields.com)
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
**
** See http://www.gnu.org/ for an up-to-date copy of the GPL.
*/

var IE_4 = document.all;
var NS_6 = document.getElementById && !document.all;

// The maximum depth of sub menus (not including the root menu)
var MAX_MENUS = 5;

// The number of milliseconds to delay before hiding a menu
var HIDE_MENU_DELAY = 1000;

// The number of pixels to offset the top-left corner of the top menu from a
// root menu item.
var TOP_MENU_LEFT_OFFSET = 0;

// This array keeps track of whether the mouse is over a given menu.
var menuFocus = new Array();
for (i = 1; i <= MAX_MENUS; i++)
  menuFocus["menu" + i] = false;

var activeRootMenuId;

var curItemHasChildren = false;

var hideMenuCounter = 0;

function getPosOffset(what, offsetType) {
  var totalOffset= (offsetType == "left") ? what.offsetLeft : what.offsetTop;
  var parentEl = what.offsetParent;
  while (parentEl != null) {
    totalOffset = (offsetType == "left") ? totalOffset + parentEl.offsetLeft : totalOffset + parentEl.offsetTop;
    parentEl = parentEl.offsetParent;
  }
  return totalOffset;
}

function stripPx(x) {
  if (x.length <= 2)
    return x;
  return eval(x.substr(0,x.length-2));
}

function addDimensions(x1, x2) {
  return (stripPx(x1) + stripPx(x2)) + 'px';
}

// This is needed so that a user doesn't cause a js error by doing a mouse over on the menu
// before the menu has completely loaded.
function enableRootMenus() {
  var i = 1;
  while ( (rootMenu = document.getElementById("rootMenu" + (i++))) ) {
    rootMenu.disabled = false;
  }
}

function focusMenu(menu) {
  menuFocus[menu.id] = true;
}

function initGMenu() {
  /*html = '';
  for (i = 1; i <= MAX_MENUS; i++) {
    //html += '<div id="menuBase' + i + '" class="subMenuBase" style="position:absolute;top:0px;left:0px;visibility:hidden;">'
    //  + '<iframe frameborder="0" width="100%"></iframe></div>';
    html += '<div id="menu' + i + '" class="subMenu" style="position:absolute;top:0px;left:0px;visibility:hidden;" onmouseout="delayHideMenu(this);" onmouseover="focusMenu(this)">'
      + '</div>';
  }
  document.body.innerHTML += html;*/
  //enableRootMenus();
}

function getHighlightedClass(curClass) {
  if (curClass.substr(0, 12) == 'subMenuItemL')
    return 'subMenuItemLeafHighlight';
  else
    return 'subMenuItemHighlight';
}

function getNotHighlightedClass(curClass) {
  if (curClass.substr(0, 12) == 'subMenuItemL')
    return 'subMenuItemLeaf';
  else
    return 'subMenuItem';
}

// Highlight current item and "unhighlight" other items in current menu
function setHighlighting(selectedNode) {
  for (i = 0; i < (selectedNode.parentNode).childNodes.length - 1; i++) {
    var curNode = (selectedNode.parentNode).childNodes[i];
    if (curNode.id == selectedNode.id) {
      if (curNode.className != getHighlightedClass(curNode.className))
        curNode.className = getHighlightedClass(curNode.className);
    } else {
      if (curNode.className != getNotHighlightedClass(curNode.className))
        curNode.className = getNotHighlightedClass(curNode.className);
    }
  }
}

function createLink(text, url, cssClass) {
  if (url == null)
    return text;
  //return '<a class="' + cssClass + '" href="' + url + '" style="height:100%;width:100%;">' + text + '</a>';
  return text;
}

// Fill in the sub menus. This is done every time a menu pops up.
function drawSubMenu(menuId, menuArray) {
  var menu = document.getElementById(menuId);
  var html = '';
  for (i = 0; i < menuArray.length; i++) {
    html += '<div id="' + menuId + "-" + i + '" class="' + (menuArray[i][2] == null ? 'subMenuItemLeaf' : 'subMenuItem"') + '"'
      + ' onmouseover="' + (menuArray[i][2] == null ? '' : 'showMenu(\'' + menuId + '\', ' + menuArray[i][2] + ', this);') + 'highlightIfNeeded(this);"'
      + (menuArray[i][1] != null ? ' style="cursor:pointer;" onclick="window.open(\'' + menuArray[i][1] + '\''
      + (menuArray[i][3] == null ? ', \'_top\'' : ', \'' + menuArray[i][3] + '\'') + ');"' : '')
      + '>'
      + createLink(menuArray[i][0], menuArray[i][1], 'subMenuItem')
      + '</div>';
  }
  html += '<div class="menuBottom"></div>';
  menu.innerHTML = html;
  return menu;
}

// Shows a level 1 menu, right below the root menus.
// Had to adjust the top location for netscape?????????
function showTopMenu(curMenu, menuArray) {
  curMenu.className = curMenu.id + 'Highlight';
  if (menuFocus['menu1'])
    subMenu = document.getElementById('menu1');
  else
    subMenu = drawSubMenu('menu1', menuArray);
  //subMenu.style.top = curMenu.offsetParent.offsetTop + curMenu.offsetParent.offsetHeight;
  //subMenu.style.top = curMenu.offsetParent.offsetTop + curMenu.offsetTop + curMenu.offsetHeight;
  subMenu.style.top = getPosOffset(curMenu, "top") + curMenu.offsetHeight + "px";
  subMenu.style.left = getPosOffset(curMenu, "left") + TOP_MENU_LEFT_OFFSET + "px";
  //subMenu.style.left = curMenu.offsetParent.offsetLeft + curMenu.offsetLeft;
  //var subMenuBase = document.getElementById('menuBase1');
  //subMenuBase.childNodes[0].style.height = subMenu.offsetHeight;
  //subMenuBase.style.top = subMenu.style.top;
  //subMenuBase.style.left = subMenu.style.left;
  subMenu.style.visibility = 'visible';
  //subMenuBase.style.visibility = 'visible';
  menuFocus[curMenu.id] = true;
  activeRootMenuId = curMenu.id;
  curItemHasChildren = true;
  hideMenus2();
}

function highlightIfNeeded(curItem) {
  if (curItem.className != "subMenuItemHighlight")
    setHighlighting(curItem);
  curItemHasChildren = !(curItem.className.substr(0, 15) == "subMenuItemLeaf");
}

// Shows a level2..leveln menu, directly to the right of the selected item.
function showMenu(curMenuId, menuArray, curItem) {
  if (curItem.className == "subMenuItemHighlight")
    return;
  var curMenu = document.getElementById(curMenuId);
  var subMenuId = 'menu' + (eval((curMenuId).substr(4,1)) + 1);
  //setHighlighting(curItem);
  //var subMenuBaseId = 'menuBase' + (eval((curMenuId).substr(4,1)) + 1);
  //var subMenuBase = document.getElementById(subMenuBaseId);
  var subMenu;
  if (menuFocus[subMenuId])
    subMenu = document.getElementById(subMenuId);
  else
    subMenu = drawSubMenu(subMenuId, menuArray);
  //subMenu.style.top = curItem.offsetParent.offsetTop + curItem.offsetTop;
  //subMenu.style.left = stripPx(curMenu.style.left) + curMenu.offsetWidth;
  subMenu.style.top = getPosOffset(curMenu, "top") + curItem.offsetTop + "px";
  subMenu.style.left = getPosOffset(curMenu, "left") + curMenu.offsetWidth + "px";
  //subMenuBase.style.top = subMenu.style.top;
  //subMenuBase.childNodes[0].style.height = subMenu.offsetHeight;
  //subMenuBase.style.left = subMenu.style.left;
  subMenu.style.visibility = 'visible';
  //subMenuBase.style.visibility = 'visible';
  var subSubMenuId = 'menu' + (eval((curMenuId).substr(4,1)) + 2);
  hideMenu(subSubMenuId, subMenuId);
}

// Recursively check all sub menus of the current menu.
// If the mouse has left a menu and all its sub menus then hide that menu.
function hideMenu(curMenuId, parentId) {
  var curMenu = document.getElementById(curMenuId);
  var hideChild = true;
  var childId = 'menu' + (eval((curMenu.id).substr(4,1)) + 1);

  //var curMenuBaseId = 'menuBase' + (eval((curMenu.id).substr(4,1)));
  //var curMenuBase = document.getElementById(curMenuBaseId);

  var child = document.getElementById(childId);
  if (child != null) {
    hideChild = hideMenu(child.id, curMenu.id);
  }

  if (!menuFocus[curMenuId] && hideChild && (!menuFocus[parentId] || !curItemHasChildren)) {
    curMenu.style.visibility = 'hidden';
    return true;
  } else {
    return false;
  }

}

function hideMenus() {
  if (--hideMenuCounter > 0)
    return;
  hideMenus2();
}

function hideMenus2() {
  var i = 1;
  var menusHidden = hideMenu('menu1', activeRootMenuId);
  while ( (rootMenu = document.getElementById("rootMenu" + (i++))) ) {
    if ( (!menuFocus[rootMenu.id] && activeRootMenuId == rootMenu.id && menusHidden)
         || (activeRootMenuId != rootMenu.id && rootMenu.className == rootMenu.id + "Highlight") ) {
      rootMenu.className = rootMenu.id;
    }
  }
}

function delayHideMenu(menu) {
  menuFocus[menu.id] = false;
  hideMenuCounter++;
  setTimeout('hideMenus()', HIDE_MENU_DELAY);
}

