// JavaScript Document
// Copyright 2004
// Code written and owned by Click Logic Inc.  http://www.clicklogicinc.com
// All Rights Reserved
//
// Purpose: create and manipulate a set of dropdown menus.  
//
// Description: The menus are a series of tables that could be created by any means.
//   This code is specifically designed to create the tables from an XML document.  

var openMenus = new Array(10);
var shadows = new Array(10);
var numOpenMenus = 0;


var MENU_BORDER_COLOR		= '#ffffff'
var MENU_BORDER_WIDTH 		= 1
var MENU_BORDER_STYLE		= 'solid'
var MENU_BACKGROUND_COLOR	= '#4E88BA'
var MENU_CURRENTPAGE_COLOR	= '#ffffff'
var MENU_MOUSEOVER_COLOR	= '#03559E'
var MENU_MOUSEDOWN_COLOR	= '#03559E'
var MENU_SHADOW_COLOR		= '#666666'
var MENU_SHOW_DELAY = 50
var MENU_HIDE_DELAY = 100
var SHADOW_SIZE = 5;
var appVer = navigator.appVersion.toLowerCase();
var isIE  = (appVer.indexOf('msie') != -1);
var menuParent;
var menuParentCell;
var menuName;
var menuDirection;
var showMenuTimer = null;
var mouseX;
var mouseY;
var hideMenuTimer = null;


function mouseOver(cell, parentName, name, direction, evt)
{
	mouseX = evt.pageX;
	mouseY = evt.pageY;
	writeConsole("mouseOver Called");
	if ( isNaN(mouseX) )
	{
		//writeConsole("Calculating mouse position  for IE");
		mouseX = window.event.clientX + document.body.scrollLeft;
		mouseY = window.event.clientY + document.body.scrollTop;
	}
	if(	hideMenuTimer != null)
	{
		window.clearTimeout(hideMenuTimer);
	}
	  // no timer... close immediately.
	
	
	menuParent = document.getElementById(parentName);
	menuCell = cell;
	menuName = name;
	menuDirection = direction;
	
	if(	showMenuTimer != null)
	{
		window.clearTimeout(showMenuTimer);
	}
	showMenuTimer = window.setTimeout("showMenu()", MENU_SHOW_DELAY, "JScript")
}

function mouseOut(evt)
{
	writeConsole("mouseOut Called");

// if the showMenuTimer is about to fire, then it will call hideMenu, so 
	// this code does not need to.
	if(showMenuTimer != null)
	{
		mouseX = evt.pageX;
		mouseY = evt.pageY;
		
		if ( isNaN(mouseX) )
		{
			//writeConsole("Calculating mouse position  for IE");
			mouseX = window.event.clientX + document.body.scrollLeft;
			mouseY = window.event.clientY + document.body.scrollTop;
		}
		
		if(	hideMenuTimer != null)
		{
			window.clearTimeout(hideMenuTimer);
		}
		hideMenuTimer = window.setTimeout("hideMenu()", MENU_HIDE_DELAY, "JScript")
	}
}

function showMenu()
{
	writeConsole("ShowMenuCalled.... calling hideMenu");
	hideMenu();
		// Don't let any other hide events occur. 
	if(	hideMenuTimer != null)
	{
		window.clearTimeout(hideMenuTimer);
	}
	
		// If the hide just hid my parent, then don't show me!
	writeConsole("visibility " + menuParent.style.visibility);
	if( menuParent.style.visibility == "hidden" ) 
	{
		writeConsole("Parent is hidden... will not show child.");
		return;
	}
	
	showTimer = null;
	
		// There are a few cases where the user has moved of the menus, but the open event still gets called... so make sure
		// the mouse is over the menus before opening anything.
	if( ! mouseOverObject(menuParent))
	{
		writeConsole("&nbsp;&nbsp;&nbsp;&nbsp;Mouse is not over the menuParent of the menu that is supposed to open... don't open it");
		return;
	}
	
	writeConsole("Show Menu - " + menuName);
			// Open the menu by setting it to visible. Also, place the menu
		// on the screen using absolute positioning.
	if (numOpenMenus == 9)
	{
		alert("There can only be 10 levels of sub menus. No more can be opened");
		return;
	}
	
	menu= document.getElementById(menuName);
	writeConsole(" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;My Visibility" + menu.style.visibility);
	if( menu.style.visibility == "visible")
	{
		writeConsole(" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  Already Open! ");
		return;
	}
	
	menu.style.borderColor = MENU_BORDER_COLOR;
	menu.style.borderWidth = MENU_BORDER_WIDTH;
	menu.style.borderStyle = MENU_BORDER_STYLE;
	menu.style.backgroundColor = MENU_BACKGROUND_COLOR;
	menu.style.visibility = "visible";
	menu.style.position = "absolute";
	if( menuDirection == "below")
	{
		menu.style.top = calculateTop(menuCell) + menuCell.offsetHeight;
		menu.style.left = calculateLeft(menuCell);
		if( menu.offsetWidth < menuCell.offsetWidth) {menu.style.width = menuCell.offsetWidth;}
		menu.style.zIndex = 100;
	}
	else if (menuDirection == "above")
	{
		menu.style.top = calculateTop(menuCell) - menu.offsetHeight;
		menu.style.left = calculateLeft(menuCell);
		if( menu.offsetWidth < menuCell.offsetWidth) {menu.style.width = menuCell.offsetWidth;}
		menu.style.zIndex = 100;
	}
	else if (menuDirection == "left")
	{
		menu.style.top = menuCell.offsetHeight;
		menu.style.left = calculateLeft(menuCell) - menu.offsetWidth;
		if( menu.offsetWidth < menuCell.offsetWidth) {menu.style.width = menuCell.offsetWidth;}
		menu.style.zIndex = 100;
	}
	else
	{
		menu.style.top = calculateTop(menuCell) - 1;
		menu.style.left = calculateLeft(menuCell) + menuCell.offsetWidth - 1;
		if( menu.offsetWidth < menuCell.offsetWidth) {menu.style.width = menuCell.offsetWidth;}
		menu.style.zIndex = 100;
	}
	
	numOpenMenus++;
	openMenus[ numOpenMenus ] = menu;
	drawShadow(menu);
	writeConsole("Opened menu: " + menuName + " at level: " + numOpenMenus);
}
function hideMenu()
{
	writeConsole("Hide Menu... ");
	// The mouse just rolled off a menu... but did it roll onto another one?
	// Walk backward and close windows until the mouse is over one.  If the mouse
	// is not over any. Then they all should close.
	
	for(var i = numOpenMenus; i > 0; i --)
	{
		writeConsole("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In HideLoop: " + i);
		if ( ! mouseOverObject( openMenus[i]) )
		{
			writeConsole("    &nbsp;&nbsp;&nbsp;&nbsp;Mouse NOT over level " + i + " ... closing menu ");
			openMenus[i].style.visibility = "hidden";
			killShadow();
			numOpenMenus--;
		}
		else
		{
			writeConsole("    Mouse is over level " + i );
			break;
		}
	}
}

function highlightItem( inItem )
{

	inItem.style.borderStyle = MENU_BORDER_STYLE;
	inItem.style.borderColor = MENU_BORDER_COLOR;
	inItem.style.borderWidth = MENU_BORDER_WIDTH;
	inItem.style.backgroundColor = MENU_MOUSEOVER_COLOR;
	inItem.style.cursor="pointer";

}

function unHighlightItem( inItem)
{
	inItem.style.borderColor = MENU_BACKGROUND_COLOR;
	inItem.style.backgroundColor = MENU_BACKGROUND_COLOR;
	inItem.style.cursor="auto";

}

function mouseOverObject( object )
{
	writeConsole("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + mouseX + "x" + mouseY + "  --  " + (calculateLeft(object) +2)  + "x" + (calculateLeft(object) + object.offsetWidth)  + "x" + (calculateTop(object) + 2)  + "x" + (calculateTop(object) + object.offsetHeight));

	if(    mouseX >= calculateLeft(object) + 1 
		&& mouseX < (calculateLeft(object) + object.offsetWidth)
		&& mouseY >= calculateTop(object) + 1
		&& mouseY < (calculateTop(object) + object.offsetHeight)  )
		{ return true; }
	else 
		{ return false; }	
}

function calculateTop(element)
{

	var top = element.offsetTop;
	var parent = element.offsetParent;

	while( parent != null)
	{
		top += parent.offsetTop;
		parent = parent.offsetParent;
	}

	return top;
}

function calculateLeft(element)
{
	var left = element.offsetLeft;
	var parent = element.offsetParent;
	while( parent != null)
	{
		left += parent.offsetLeft;
		parent = parent.offsetParent;
	}
	return left;
}
function getParentName(name)
{
	index = name.lastIndexOf(".");
	if( index == -1 )
		return name;
	else
		return name.substring( 0, index);
}

function drawShadow( object )
{
	writeConsole("&nbsp;&nbsp;&nbsp;&nbsp;drawShadow");
	
	if (isIE)
	{
		var i;
		for (i=SHADOW_SIZE; i>0; i--)
		{
			var rect = document.createElement('div');
				
			rect.style.position = 'absolute';
			rect.style.left = (calculateLeft(object) + i) + 'px';
			rect.style.top = (calculateTop(object) + i) + 'px';
			rect.style.width = object.offsetWidth + 'px';
			rect.style.height = object.offsetHeight + 'px';
			rect.style.zIndex = object.style.zIndex - i;
			rect.style.backgroundColor = "#999999";
			var opacity = 1/(2*i);
			rect.style.filter = 'alpha(opacity=' + (100 * opacity) + ')';
			object.insertAdjacentElement('afterEnd', rect);
			shadows[ (SHADOW_SIZE * numOpenMenus) + i] = rect;
		}
	}
}

function killShadow()
{
	if(isIE)
	{
		var i;
		for (i=SHADOW_SIZE; i>0; i--)
		{
			shadows[(SHADOW_SIZE * numOpenMenus) + i].removeNode(true);
		}
	}
}