// 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 = "#222222";
var MENU_BORDER_WIDTH = 1;
var MENU_BORDER_STYLE = "solid";
var MENU_BACKGROUND_COLOR = "#030E44";
var MENU_CURRENTPAGE_COLOR = "#030E44";
var MENU_MOUSEOVER_COLOR = "#cccccc";
var MENU_MOUSEDOWN_COLOR = "#999999";
var MENU_SHADOW_COLOR = "#666666";
var MENU_SHOW_DELAY = 100;
var MENU_HIDE_DELAY = 1000;
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;



// The submenus do not always show up because the 

// browser does not correctly calculated the position of the

// mouse relative to the menu item.
var ieXOffset = 2;
var ieYOffset = 2;
var XOffset = 0;
var YOffset = 0;
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.className = "SubMenu SubMenuBorder";
	menu.style.position = "absolute";
	if (menuDirection == "below") 
	{
		leftOffset = ieXOffset;
		topOffset = ieYOffset;
		if (isIE) 
		{
			menu.style.top = calculateTop(menuCell) + menuCell.offsetHeight;
			menu.style.left = calculateLeft(menuCell);
		}
		else
		{
			menu.style.top = calculateTop(menuCell) + menuCell.offsetHeight + "px";
			menu.style.left = calculateLeft(menuCell) + "px";
		}			
		
		if (menu.offsetWidth < menuCell.offsetWidth) {
			menu.style.width = menuCell.offsetWidth;
		}
		menu.style.zIndex = 100;
		writeConsole("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Menu Top: " + menu.style.top + " Menu Left: " + menu.style.left);
	} 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 = calculateTop(menuCell) - 2;
				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) - 2;
				menu.style.left = calculateLeft(menuCell) + menuCell.offsetWidth - 2;
				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 + " at " + menu.style.left + ":" + menu.style.top + " (left:top)");
}
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 mouseOverObject(object) {
	var leftOffset = XOffset;
	var topOffset = YOffset;
	if (isIE) {
		leftOffset = ieXOffset;
		topOffset = ieYOffset;
	}
	writeConsole("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mousex: " + mouseX + " mousey:" + mouseY + "  -- Element:  " + (calculateLeft(object) + leftOffset) + "x" + (calculateLeft(object) + object.offsetWidth) + "x" + (calculateTop(object) + topOffset) + "x" + (calculateTop(object) + object.offsetHeight) + " (left:right:top:bottom)");
	if (mouseX >= calculateLeft(object) + leftOffset && mouseX < (calculateLeft(object) + object.offsetWidth + leftOffset) && mouseY >= calculateTop(object) + topOffset && mouseY < (calculateTop(object) + object.offsetHeight) + topOffset) {
		return true;
	} else {
		return false;
	}
}
function calculateTop(element) {
	var top = element.offsetTop;
	var parent = element.offsetParent;
	while (parent != null) {
		top += parent.offsetTop;

//		writeConsole("calculate top... OffsetParent = " + parent.nodeName + " : " + top);
		parent = parent.offsetParent;
	}
	return top;
}
function calculateLeft(element) {
	var left = element.offsetLeft;
	var parent = element.offsetParent;
	while (parent != null) {
		left += parent.offsetLeft;
//		writeConsole("calculate left... OffsetParent = " + parent.nodeName + " : " + left);
		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);
		}
	}
}


