/*
** 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;
// max depth of sub menus (not including the root menu)
var MAX_MENUS = 5;
// ms to delay before hiding a menu
var HIDE_MENU_DELAY = 10;
// pixel offset of the top-left corner of the top menu from a
// root menu item
var TOP_MENU_LEFT_OFFSET = 0;
// keep 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';
}
function enableRootMenus() {
	var i = 1;
	while ( (rootMenu = document.getElementById("rootMenu" + (i++))) ) {
		rootMenu.disabled = false;
	}
}
function focusMenu(menu) {
	menuFocus[menu.id] = true;
}
function initGMenu() {}
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';
}
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 drawSubMenu(menuId, menuArray, menuWidth) {
	var menu = document.getElementById(menuId);
	var html = '';
	if (menuWidth!=0)
		html += '<div style="width:'+menuWidth+'px">';
	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.location=\'' + menuArray[i][1] + '\';"' : '')
			+ '>'
			+ menuArray[i][0]
			+ '</div>';
	}
	html += '<div class="menuBottom"></div>';
	if (menuWidth!=0)
		html += '</div>';
	menu.innerHTML = html;
	return menu;
}
function showTopMenu(curMenu, menuArray, menuWidth) {
	curMenu.className = curMenu.id + 'Highlight';
	if (menuFocus['menu1'])
		subMenu = document.getElementById('menu1');
	else
		subMenu = drawSubMenu('menu1', menuArray, menuWidth);
	subMenu.style.top = getPosOffset(curMenu, "top") + curMenu.offsetHeight + "px";
	subMenu.style.left = getPosOffset(curMenu, "left") + TOP_MENU_LEFT_OFFSET + "px";
	subMenu.style.visibility = 'visible';
	menuFocus[curMenu.id] = true;
	activeRootMenuId = curMenu.id;
	curItemHasChildren = true;
	hideMenus2();
}
function showTop(curMenu) {
	curMenu.className = curMenu.id + 'Highlight';
	menuFocus[curMenu.id] = true;
	activeRootMenuId = curMenu.id;
	curItemHasChildren = false;
}
function highlightIfNeeded(curItem) {
	if (curItem.className != "subMenuItemHighlight")
		setHighlighting(curItem);
	curItemHasChildren = !(curItem.className.substr(0, 15) == "subMenuItemLeaf");
}
function showMenu(curMenuId, menuArray, curItem) {
	if (curItem.className == "subMenuItemHighlight")
		return;
	var curMenu = document.getElementById(curMenuId);
	var subMenuId = 'menu' + (eval((curMenuId).substr(4,1)) + 1);
	var subMenu;
	if (menuFocus[subMenuId])
		subMenu = document.getElementById(subMenuId);
	else
		subMenu = drawSubMenu(subMenuId, menuArray, 0);
	subMenu.style.top = getPosOffset(curMenu, "top") + curItem.offsetTop + "px";
	subMenu.style.left = getPosOffset(curMenu, "left") + curMenu.offsetWidth + "px";
	subMenu.style.visibility = 'visible';
	var subSubMenuId = 'menu' + (eval((curMenuId).substr(4,1)) + 2);
	hideMenu(subSubMenuId, subMenuId);
}
function hideMenu(curMenuId, parentId) {
	var curMenu = document.getElementById(curMenuId);
	var hideChild = true;
	var childId = 'menu' + (eval((curMenu.id).substr(4,1)) + 1);

	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;
	}
	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);
}
