var MAX_MENUS = 5;

var lastMenu = new Array(MAX_MENUS);
var activeMenus = 0;
var cancelTimeout = 0;

function DOMBrowser() {
	// dont even bother with macs
	if (navigator.userAgent.toLowerCase().indexOf("mac") != -1)
		return false;
	return document.body && document.body.style && document.getElementById;
}

/* return the left-most position of an object */
function getXPos(a)
{
	// special work-around for image-maps in IE
	if (document.all && a.parentNode.tagName == "MAP")
	{
		a = document.getElementsByTagName("img")[0];
	}
		
	var x = a.offsetLeft;
	while (a.offsetParent)
	{
		a = a.offsetParent;
		x += a.offsetLeft;
	}

	return x;
}

/* return the top-most position of an object */
function getYPos(a)
{
	var y = a.offsetTop;
	while (a.offsetParent)
	{
		a = a.offsetParent;
		//alert(a.offsetTop);
		y += a.offsetTop;
	}
	
	return y;
}

/* get the height offset (extracting from coords if necessary) */
function getOffsetHeight(a)
{
	var y = a.coords;
	if (y)
	{
		y = y.substring(y.lastIndexOf(",")+1, y.length); 	// extract height coordinate
		y = parseInt(y) - a.offsetTop; 							// convert to integer and subtract top offset
		return y;
	}
	else
		return a.offsetHeight;
}

/* Image map support */
function getMapOffset(a)
{
	var x = a.coords;
	if (x)
		return parseInt(x.substring(0, x.indexOf(",")));
	else
		return 0;
}

/*********************************************************************
	ShowMenu(menuID, parentID)
		Shows a regular menu
 *********************************************************************/
function showMenu(a, b) {
	if (!DOMBrowser()) return;	
	
	cancelTimeout = 1;
	hideChildren(lastMenu[0]);
	activeMenus = 0;
	
	
	var menu = document.getElementById(a);
	var parent = document.getElementById(b);
   menu.style.left =	getXPos(parent) + getMapOffset(parent) - 5 + "px";
   menu.style.top = getYPos(parent) + getOffsetHeight(parent) + "px";
	menu.style.display = '';

	lastMenu[activeMenus++] = b;		// add parent
	lastMenu[activeMenus++] = a;		// add child
}

/*********************************************************************
	ShowSub(menuID, parentID)
		Shows a sub-menu or a right-aligned popup  
 *********************************************************************/
function showSub(a, b) {
	cancelTimeout = 1;
	var parentMenu = document.getElementById(b).parentNode;
	hideChildren(parentMenu.id);
	
	var menu = document.getElementById(a);
	var parent = document.getElementById(b);
	menu.style.left = getXPos(parent) + parent.offsetWidth + "px";
	menu.style.top = getYPos(parent) + "px";
	menu.style.display = '';

	lastMenu[activeMenus++] = a;
}

/*********************************************************************
	doHide()
		called from a timeout to actually hide the menu 
		* allows time for the user to move their mouse over a menu 
		  without losing it
 *********************************************************************/
function doHide()
{
	if (cancelTimeout == 1)
		return;
	
	var i;

	// the hidden menu may call this so check
	if (activeMenus == 0)
		return; 

	for (i=1; i<activeMenus; i++)
		document.getElementById(lastMenu[i]).style.display = 'none';
	activeMenus = 0;
}

/*********************************************************************
	hideMenu()
		called on mouseout 
 *********************************************************************/
function hideMenu() {
	if (!DOMBrowser()) return;
	
	var ms = 500;

	if (activeMenus > 1)
		ms = 100;
	
	cancelTimeout = 0;
	setTimeout("doHide()", ms);
}

/*********************************************************************
	overMenu
		cancels the actual hiding of the menu by doHide() and also hides
		children
 *********************************************************************/
function overMenu(a) {
	cancelTimeout = 1;
	if (a)
		hideChildren(a);
}

/*********************************************************************
	hideChildren(parentID)
		hides all children of a
 *********************************************************************/
function hideChildren(a) {
	var i=0;
	var newActive = 0;
	while (lastMenu[i] != a && i<activeMenus)
		i++;
	
	i++;
	newActive = i;
	while (i<activeMenus)
	{
		document.getElementById(lastMenu[i]).style.display = 'none';
		i++;
	}
	
	activeMenus = newActive;
}

/*******************************************************************
 OTHER FUNCTIONS (not associated w/ menu)
 *******************************************************************/

function openWindow(url, width, height, title)
{
	if (width == null) 
		width = 640;
	if (height == null)
		height = 480;
	if (title == null)
		title = "View Image";
	var hWin = window.open(url, "_blank", "menubar=no, resizeable=1, width=" + width + ", height=" + height);
	if (!hWin) {
		alert("Popup blocker software is preventing you from viewing the image, please disable this software to view the images.");
		return;
	}
	
	hWin.document.write('<html><head><title>' + title + '</title></head><body style="margin: 0;"><img src="' + url + '" alt="' + title + '" /></body></html>');
}

