/*
** v1.0.20050304 ALT CITRUS
**
** 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.01
**
** Copyright (C) 2004-2005  Geoffrey Derek Cox (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.
*/

// 2005.09 Edited by Andrej Kudriavcev (CITRUS)
// * All graphics changed to styles
// * Opera adaptation
// * Menu hide with setTimeout
// * Menu activation with rootEnablded

/*
// Uncomment this if you need
var DOM = (document.getElementById ? true : false); 
var IE4 = ((document.all && !DOM) ? true : false);

function getObject(id) {
	if (DOM) return document.getElementById(id);
	if (IE4) return document.all[id];
	else return false;
}
function getOffset(id, offsetName) {
	var result = 0;
	var item = eval('id');
	while (item != null) {
		result += eval('item.'+offsetName);
		item = eval('item.offsetParent');
	}
	return result;
}
*/

var isIe = (document.all ? true : false);
var isOpera = (navigator.userAgent.toLowerCase().indexOf("opera") != -1);
var rootEnabled = false;
var checkIt = false;

// Needed because the mouse coordinates in IE and NN are not exact
var TOLERANCE_TOP = (isIe ? 2 : -5);
var TOLERANCE_RIGHT = (isIe ? 2 : 0);
var TOLERANCE_BOTTOM = (isIe ? 2 : 1);
var TOLERANCE_LEFT = (isIe ? 2 : 0);

// 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;
var MENU_LEFT_OFSET = 1;

// The maximum depth of sub menus (not including the root menu)
var MAX_MENUS = 6;

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

// Highlight current item and "unhighlight" other items in current menu
function setHighlighting(selectedNode) {
	for (i = 0; i < (selectedNode.parentNode.parentNode).childNodes.length; i++) {
		var curNode = (selectedNode.parentNode.parentNode).childNodes[i].childNodes[0];
		if (curNode.offsetHeight > 1) {
			if (curNode.id == selectedNode.id) curNode.className = "subMenuItemHighlight";
			else curNode.className = "subMenuItem";
		}
	}
}

function createLink(text, url, parseArr) {
	if (url == null) return '<div class="subMenuItemCell">' + text + '</div>';
	else return '<table border="0" cellpadding="0" cellspacing="0" class="simtas"><tr><td width="100%" class="subMenuItemCell"><a class="submenu_n " href="' + url + '" style="color: #000000; text-decoration: none">' + text + '</a></td>'+(parseArr?'':'')+'</tr></table>';
}

// 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() {
	rootEnabled = true;
}

// Prepare the menus
// Note: IE doesn't allow one to cover a form field with a div, however it does permit
//       an iframe to cover a form field. Therefore, we place an iframe under each menu div.
function initGMenu() {
	html = '';
	for (i = 1; i <= MAX_MENUS; i++) {
		if (isIe && !isOpera) {
			html += '<div id="menuBase' + i + '" class="dsubMenuBase" style="position:absolute;top:0px;left:0px;visibility:hidden;"><iframe frameborder="0" width="100%" height="100%"></iframe></div>';
		} else html += '<div id="menuBase' + i + '" class="dsubMenuBase" style="position:absolute;top:0px;left:0px;visibility:hidden;"></div>';
		html += '<div id="menu' + i + '" style="position:absolute; left: 0px; top:0px;visibility:hidden;" onMouseOut="hideMenus(event);"></div>';
	}
	document.write(html);
	enableRootMenus();
}

// Fill in the sub menus. This is done every time a menu pops up.
function drawSubMenu(menuId, menuArray) {
	var menuName = 'menu' + menuId;
	var menu = getObject(menuName);
	var html = '<table id="menuTable'+menuId+'" border="0" cellpadding="0" cellspacing="1" width="150" class="dsubMenu">';
	for (i = 0; i < menuArray.length; i++) {
		if (menuArray[i]) {
			if (menuId == MAX_MENUS - 1) menuArray[i][2] = null;
			if (menuArray[i][2] == null) {
				html += '<tr><td id="' + menuName + "-" + i + '" class="subMenuItem" onMouseOver="setHighlighting(this);"'+(menuArray[i][1] == null?'':' onclick="window.location.href=\''+menuArray[i][1]+'\'" style="cursor: pointer"')+'>'
					+ createLink(menuArray[i][0], menuArray[i][1], false) 
					+ '</td></tr>';
			} else {
				html += '<tr><td id="' + menuName + "-" + i + '" class="subMenuItem" onMouseOver="dshowMenu(\'' + menuName + '\', ' + menuArray[i][2] + ', this); setHighlighting(this);"'+(menuArray[i][1] == null?'':' onclick="window.location.href=\''+menuArray[i][1]+'\'" style="cursor: pointer"')+'>'
					+ createLink(menuArray[i][0], menuArray[i][1], true) 
					+ '</td></tr>';
			}
		}
	}
	html += '</table>';
	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, e) {
	if (!rootEnabled) return false;
	if (checkIt) clearTimeout(checkIt);
	hideMenu("menu1", 0, 0);
//	curMenu.className = curMenu.id + 'Highlight';
	var subMenu = drawSubMenu(1, menuArray);
	var subMenuTable = getObject('menuTable1');
	var subMenuBase = getObject('menuBase1');
	subMenu.style.top = getOffset(curMenu, 'offsetTop') + curMenu.offsetHeight - 2;
	subMenu.style.left = getOffset(curMenu, 'offsetLeft') + TOP_MENU_LEFT_OFFSET;
	if (isIe && !isOpera) subMenuBase.childNodes[0].style.height = subMenuTable.offsetHeight;
	subMenuBase.style.width = subMenuTable.offsetWidth;
	subMenuBase.style.height = subMenuTable.offsetHeight;
	subMenuBase.style.top = subMenu.style.top;
	subMenuBase.style.left = subMenu.style.left;
	subMenu.style.visibility = 'visible';
	subMenuBase.style.visibility = 'visible';
}

// Shows a level2..leveln menu, directly to the right of the selected item.
function dshowMenu(curMenuId, menuArray, curItem) {
	if (checkIt) clearTimeout(checkIt);
	var curMenu = getObject(curMenuId);
	var curMenuIDN = (eval((curMenuId).substr(4,1)) + 1);
	var subMenuIDN = (eval((curMenuId).substr(4,1)) + 2);
	var subMenuId = 'menu' + curMenuIDN;
	var subMenuBaseId = 'menuBase' + curMenuIDN;
	var subMenu = drawSubMenu(curMenuIDN, menuArray);
	var subMenuBase = getObject(subMenuBaseId);
	var subMenuTable = getObject('menuTable' + curMenuIDN);
	var newtop = getOffset(curItem, 'offsetTop'); //stripPx(curMenu.style.top);
	var newleft = stripPx(curMenu.style.left) + curMenu.offsetWidth - 4 - MENU_LEFT_OFSET;
	if (newleft + subMenuTable.offsetWidth > document.body.clientWidth) newleft = stripPx(curMenu.style.left) - subMenuTable.offsetWidth + 4;
	if ((newtop != subMenu.style.top) || (newleft != subMenu.style.left)) hideMenu('menu' + subMenuIDN, 0, 0);
	subMenu.style.top = newtop;
	subMenu.style.left = newleft;
	subMenuBase.style.top = subMenu.style.top;
	if (isIe && !isOpera) subMenuBase.childNodes[0].style.height = subMenuTable.offsetHeight;
	subMenuBase.style.width = subMenuTable.offsetWidth;
	subMenu.style.width = subMenuTable.offsetWidth;
	subMenuBase.style.left = subMenu.style.left;
	subMenu.style.visibility = 'visible';
	subMenuBase.style.visibility = 'visible';
	
}

function unhighlightRootMenus() {
	var i = 1;
	while ( (rootMenu = getObject("rootMenu" + (i++))) ) {
		rootMenu.className = rootMenu.id;
	}
}

// Recursively check and then hide all menus arropriately. This is done
// during a mouseOut event.
function hideMenus(e) {
	if (isIe) {
		xMousePos = event.clientX + document.body.scrollLeft;
		yMousePos = event.clientY + document.body.scrollTop;
	} else {
		xMousePos = e.clientX;
		yMousePos = e.clientY;
	}
	if (checkIt) clearTimeout(checkIt);
	checkIt = setTimeout('hideMenu("menu1", '+xMousePos+', '+yMousePos+')', 300);
}

// 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, xMousePos, yMousePos) {
	var curMenu = getObject(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 = getObject(curMenuBaseId);
	var child = getObject(childId);
	if (child != null) {
		hideChild = hideMenu(child.id, xMousePos, yMousePos);
	}
	if (xMousePos < stripPx(curMenu.style.left) + TOLERANCE_LEFT
		|| xMousePos > stripPx(curMenu.style.left) + curMenu.offsetWidth - TOLERANCE_RIGHT
		|| yMousePos < stripPx(curMenu.style.top) + TOLERANCE_TOP
		|| yMousePos > stripPx(curMenu.style.top) + curMenu.offsetHeight - TOLERANCE_BOTTOM
		|| curMenu.style.visibility == 'hidden') {
		if (hideChild == true) {
			curMenu.style.visibility = 'hidden';
			curMenuBase.style.visibility = 'hidden';
			return true;
		}
		return false;
	} else return false;
}
