//----------------------------------------------------------------------------
// DESCRIPTION ****************************************************************
// Name: deepmenus.js
// Creator: Bob Doyle <support@skybuilders.com> 
// and Derek Doyle <dtd@skybuilders.com>
// Copyright © 1999-2004 skyBuilders.com
// 
// skyBuilders deep menu functions
/*
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
*/
// 
// except functions used basically "as is" from Mike Hall's code with the 
// license notice following:
// Browser
// hasClassName
// fGetContainerWithClass
// getPageOffsetLeft
// getPageOffsetTop
//*****************************************************************************
// Do not remove this notice.
//
// Copyright 2000-2004 by Mike Hall.
// See http://www.brainjar.com for terms of use. 
// [It's all GPL, according to the site. -- dtd]
/*
"Use of Programming Code

"All programming code on the site may be used, redistributed and/or modified 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.

"No support, guarantee or warrantee is offered or implied. By using any code found on this site you assume full risk and responsibility for that use.

"See the GNU General Public License for details."

-- from http://www.brainjar.com/terms.asp on 20041229
*/
//*****************************************************************************
// END DESCRIPTION ************************************************************

//----------------------------------------------------------------------------
// Code to determine the browser and version.
//----------------------------------------------------------------------------

function Browser() {
	var ua, s, i;
	
	this.isIE    = false;  // Internet Explorer
	this.isOP    = false;  // Opera
	this.isNS    = false;  // Netscape
	this.version = null;
	
	ua = navigator.userAgent;
	
	s = "Opera";
	if ((i = ua.indexOf(s)) >= 0) {
		this.isOP = 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 as Netscape 6.1.
	
	s = "Gecko";
	if ((i = ua.indexOf(s)) >= 0) {
		this.isNS = true;
		this.version = 6.1;
		return;
	}
	
	s = "MSIE";
	if ((i = ua.indexOf(s))) {
		this.isIE = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}
}

var browser = new Browser();

function getPageOffsetLeft(el) {
	var x;
	
	// Return the x coordinate of an element relative to the page.
	
	x = el.offsetLeft;
	if (el.offsetParent != null)
		x += getPageOffsetLeft(el.offsetParent);
	
	return x;
}

function getPageOffsetTop(el) {
	var y;
	
	// Return the x coordinate of an element relative to the page.
	
	y = el.offsetTop;
	if (el.offsetParent != null)
		y += getPageOffsetTop(el.offsetParent);
	
	return y;
}

function fOpenSubMenu(oSubMenu, oOption, sDeep) {
	var x, y;
	
	// alert(oSubMenu);
	if (sDeep == 1) {
		// Position the associated drop down menu beside the button
		x = getPageOffsetLeft(oOption) + oOption.offsetWidth;
		y = getPageOffsetTop(oOption);
	} else {
		// Position the associated drop down menu under the button
		x = getPageOffsetLeft(oOption);
		y = getPageOffsetTop(oOption) + oOption.offsetHeight;
	}
	
	// For IE, adjust position.
	if (browser.isIE) {
		x += oOption.offsetParent.clientLeft;
		y += oOption.offsetParent.clientTop;
	}
	
	// restrict sub menu from opening too far to the right
	// widths are hardcoded here and in base.css
	sRightMargin = 760;
	// was: sSubmenuWidth = 197;
  sSubmenuWidth = 187;
	sSubmenuRightEdge = x + sSubmenuWidth;
	// alert("sSubmenuRightEdge: " + sSubmenuRightEdge);
  // alert("sDirectionToOpen: " + oOption.oMenu.sDirectionToOpen);
	if ((oOption.oMenu.sDirectionToOpen == "left") || (sSubmenuRightEdge > sRightMargin)) {
    oSubMenu.sDirectionToOpen = "left";
		if (sDeep == 1) {
      oOption.oMenu.sDirectionToOpen = "left";
			x = getPageOffsetLeft(oOption) - oOption.offsetWidth;
		} else {
			sOverage = sSubmenuRightEdge - sRightMargin;
			x -= sOverage;
		}
	} else {
    oOption.oMenu.sDirectionToOpen = "right";
    oSubMenu.sDirectionToOpen = "right";
  }
	
  // position the sub menu
	oSubMenu.style.left = x + "px";
	oSubMenu.style.top  = y + "px";
	
	// show the sub menu
	// oSubMenu.style.visibility = "visible";
  // delay so that the move can complete first (moz fails race condition)
  setTimeout('oSubMenu = document.getElementById("' + oSubMenu.id + '"); oSubMenu.style.visibility = "visible";', 1);
}

function fCloseSubmenuTree(oMenu) {
	// alert("fCloseSubmenuTree oMenu.id: " + oMenu.id);
	// was: closeSubMenu(oMenu);
	
	if (oMenu == null) {
			return;
	} else {
		// recursively close any submenus
		if (typeof oMenu.oOpenSubMenu != "undefined") {
			if (oMenu.oOpenSubMenu != null) {
        // alert("fCloseSubmenuTree");
				fCloseSubmenuTree(oMenu.oOpenSubMenu);
			}
		}
		// hide this menu
		// oMenu.style.visibility = "hidden";
    setTimeout('oMenu = document.getElementById("' + oMenu.id + '"); oMenu.style.visibility = "hidden";', 1);
		// set the open submenu for the super menu to null
		oMenu.oSuperMenu.oOpenSubMenu = null;
	}
		
	/*
	// function closeSubMenu(menu) {
		if (menu == null || menu.activeItem == null)
			return;
		
		// Recursively close any sub menus.
		
		if (menu.activeItem.subMenu != null) {
			closeSubMenu(menu.activeItem.subMenu);
			menu.activeItem.subMenu.style.visibility = "hidden";
			menu.activeItem.subMenu = null;
		}
		removeClassName(menu.activeItem, "menuItemHighlight");
		menu.activeItem = null;
	// }
	*/
}

function fMenuOptionMouseover(oEvent, sMenuID, sSubMenuID) {
	var oOption, oMenu, oSubMenu;
	var sDeep;
	
  if (browser.isIE) {
    oOption = window.event.srcElement;
  } else {
    oOption = oEvent.currentTarget;
	}
	
	// alert("fMenuOptionMouseover");
	
	// get menu handle
	oMenu = document.getElementById(sMenuID);
	// alert(oSubMenu);
	
	// set the menu for this option
	oOption.oMenu = oMenu;
	
	// check for any open submenu tree for this menu
	// alert("oOpenSubMenu: " + oMenu.oOpenSubMenu);
	if (typeof oMenu.oOpenSubMenu != "undefined") {
		if (oMenu.oOpenSubMenu != null) {
			// close the open submenu tree
      // alert("fMenuOptionMouseover");
			fCloseSubmenuTree(oMenu.oOpenSubMenu);
		}
	}
	
	if (sSubMenuID != '') {
		// get submenu handle
		oSubMenu = document.getElementById(sSubMenuID);
		
		// set whether this is a top submenu or a deep submenu
		// set tree top submenu for this submenu
		if ((sMenuID == 'main-menu') || (sMenuID == 'auxiliary-menu')) {
			sDeep = 0;
			// for top submenus, tree top submenu is self
			oSubMenu.oTopSubMenu = oSubMenu;
		} else {
			sDeep = 1;
			// for deeper submenus, tree top submenu is same property from menu
			oSubMenu.oTopSubMenu = oMenu.oTopSubMenu;
		}
		
		// set super menu for sub menu
		oSubMenu.oSuperMenu = oMenu;
		
		// set super menu option for sub menu
		oSubMenu.oSuperMenuOption = oOption;
		
		// open this submenu
		// alert("oSubMenu: " + oSubMenu);
		fOpenSubMenu(oSubMenu, oOption, sDeep);
		
		// set the open submenu for this menu to this submenu
		oMenu.oOpenSubMenu = oSubMenu;
	}
}

function hasClassName(el, name) {
	var i, list;
	
	// Return true if the given element currently has the given class
	// name.
	if ((name == "menu") || (name == "menuItem")) {
		// alert("name: " + name + "\nel.id: " + el.id + "\nel.tagName: " + el.tagName + "\nel.className: " + el.className);
	}
	list = el.className.split(" ");
	for (i = 0; i < list.length; i++) {
		// alert("list[i]: " + list[i]);
		if (list[i] == name)
			return true;
	}
	
	return false;
}

function fGetContainerWithClass(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 ((tagName == "LI") || (tagName == "DIV")) {
			// alert("node.tagName: " + node.tagName);
		}
		if (node.tagName != null && node.tagName == tagName &&
				hasClassName(node, className))
			return node;
		node = node.parentNode;
	}
	
	return node;
}

function fGetContainerWithID(node, sID) {
	// Starting with the given node, find the nearest containing element
	// with the specified ID.
	
	while (node != null) {
		if (node.id == sID)
			return node;
		node = node.parentNode;
	}
	
	return node;
}

function fMenuOptionMouseout(oEvent, sMenuID) {
	// alert("fMenuOptionMouseout\nsMenuID: " + sMenuID);
	
  if (browser.isIE) {
    oOption = window.event.srcElement;
		oDestinationElement = window.event.toElement;
  } else {
    oOption = oEvent.currentTarget;
		if (oEvent.relatedTarget) {
			oDestinationElement = (oEvent.relatedTarget.tagName ? oEvent.relatedTarget : oEvent.relatedTarget.parentNode);
		} else {
			oDestinationElement = null;
		}
	}
	
	// getContainerWith could use id instead of class for this comparison
	if (oDestinationElement) {
		// alert(fGetContainerWithClass(oDestinationElement, "DIV", "menu"));
		if ((fGetContainerWithClass(oDestinationElement, "DIV", "menu")) || (fGetContainerWithID(oDestinationElement, "DIV", "main-menu")) || (fGetContainerWithClass(oDestinationElement, "DIV", "aux-menu")) || (fGetContainerWithID(oDestinationElement, "TABLE", "auxiliary-menu"))) {
			sDestinationElementIsInTree = 1;
		} else {
			sDestinationElementIsInTree = 0;
		}
	} else {
		sDestinationElementIsInTree = 1;
	}
	
	// alert("oDestinationElement: " + oDestinationElement + "\nsDestinationElementIsInTree: " + sDestinationElementIsInTree);
	if (sDestinationElementIsInTree) {
		// if this is an option in the super tree
		// do nothing?
		if (0) {
			oMenu = document.getElementById(sMenuID);
			alert(sMenuID);
			alert(oMenu);
			alert((oOption == oMenu.oSuperMenuOption));
			if (oOption.oMenu == oMenu) {
				if (oOption != oMenu.oSuperMenuOption) {
					fCloseSubmenuTree(oMenu);
				}
			} else {
				fCloseSubmenuTree(oMenu);
			}
		}
	} else {
		// close entire menu tree
		// alert("fMenuOptionMouseout\nsMenuID: " + sMenuID);
		
		oMenu = document.getElementById(sMenuID);
		// alert("fMenuOptionMouseout\noMenu: " + oMenu);
    // alert("fMenuOptionMouseout");
		fCloseSubmenuTree(oMenu.oTopSubMenu);
	}
}

