var timeOutId = null;
var currentMenu = null;
var menuZIndex = 5101;

// User is hovering over a main menu button.
function mouseOverMainMenu(menuNumber)
	{
	if (timeOutId)
		{
		clearMenuTimeout();
		}

	if (!currentMenu || currentMenu != 'mainMenu' + menuNumber)
		{
		turnOffMenus();
		currentMenu = null;
		timeOutId = window.setTimeout('turnMenuOn(' + menuNumber + ');', 300);
		}
	else
		{
		clearMenuTimeout();
		}
	}

// User moved mouse off of main menu button.
function mouseOffMainMenu(menuNumber)
	{
	clearMenuTimeout();
	setMenuTimeout(10);
	}

// User clicked a main menu button.
function mouseClickMainMenu(menuNumber)
	{
	if (timeOutId)
		{
		turnOffMenus();
		clearMenuTimeout();
		turnMenuOn(menuNumber);
		return false;
		}
	else
		{
		turnMenuOff(menuNumber);
		return true;
		}
	}

// Show the menu items pane.
function turnMenuOn(menuNumber)
	{
	clearMenuTimeout();
	currentMenu = "mainMenu" + menuNumber;

	// Get the current menu and menu items pane.
	var mainMenu = document.getElementById(currentMenu);
	var menuItemsPane = document.getElementById("menuItemsPane" + menuNumber);

	// Turn on the menu and menu items pane display.
	if (mainMenu.className.indexOf("_light") !== -1)
		{
		mainMenu.className = "main_menu_button_on main_menu_button_light_text main_menu_button_link";
		}
	else
		{
		mainMenu.className = "main_menu_button_on main_menu_button_text main_menu_button_link";
		}

	if (enableMenus)
		{
		menuItemsPane.className = "menu_items_pane_on";

		// If the client browser is IE, adjust the left edge.
		if (navigator.appVersion.toLowerCase().indexOf('msie') != -1)
			{
			var mainMenuWrapper = document.getElementById("main_header_dropdown_menu");

			// Set the position and height of the menu items pane.
			var mainMenuDims = getMenuDimensions(mainMenu);
			if (mainMenuWrapper != null)
				{
				var mainMenuWrapperDims = getMenuDimensions(mainMenuWrapper);

				// Get the difference.
				mainMenuDims[0] = parseInt(mainMenuDims[0]) - parseInt(mainMenuWrapperDims[0]);
				}

			// Set the left of the menu items pane.
			menuItemsPane.style.left = parseInt(mainMenuDims[0]) + "px";
			}

		// Set the z-index so the menu sits on top of everything else.
		menuItemsPane.style.zIndex = menuZIndex;

		// Check to see if we need to hide any windowed objects (IE Only).
		toggleWindowedCollisions(menuItemsPane);
		}
	}

// Turn off the requested menu.
function turnMenuOff(menuNumber)
	{
	// Get the main menu and the menu items pane for this menu.
	var mainMenu = document.getElementById("mainMenu" + menuNumber);
	var menuItemsPane = document.getElementById("menuItemsPane" + menuNumber);

	// If this main menu has a menu items pane...
	if (menuItemsPane !== null)
		{
		// Turn off the main menu and the menu items pane.
		if (mainMenu.className.indexOf("_light") !== -1)
			{
			mainMenu.className = "main_menu_button main_menu_button_light_text main_menu_button_link";
			}
		else
			{
			mainMenu.className = "main_menu_button main_menu_button_text main_menu_button_link";
			}
		menuItemsPane.className = "menu_items_pane_off";
		}
	}

// Loop through the main menu buttons and turn them off.
function turnOffMenus()
	{
	// Loop through the main menus.
	for (var i = 1; i <= numMenus; i++)
		{
		// Check to see if there were any hidden windowed items that we need to switch back on.
		if (document.getElementById("menuItemsPane" + i) !== null)
			{
			toggleWindowedCollisions(document.getElementById("menuItemsPane" + i));
			}

		// Turn off this main menu and its' menu items pane.
		turnMenuOff(i);
		}

	currentMenu = null;
	}

function setMenuTimeout(delay)
	{
	timeOutId = window.setTimeout("turnOffMenus()", delay);
	}

function clearMenuTimeout()
	{
	window.clearTimeout(timeOutId);
	timeOutId = null;
	}

// Returns true if the requested object collides with a windowed object.
function collidesWithWindowedObject(obj, windowedObj)
	{
	// Get the position of the windowed object.
	var windowedDims = getMenuDimensions(windowedObj);
	var wx = parseInt(windowedDims[0]);
	var wy = parseInt(windowedDims[1]);
	var ww = windowedObj.offsetWidth;
	var wh = windowedObj.offsetHeight;

	// Get the position of the requested object.
	var objectDims = getMenuDimensions(obj);
	var ox = parseInt(objectDims[0]);
	var oy = parseInt(objectDims[1]);
	var ow = obj.offsetWidth;
	var oh = obj.offsetHeight;

	// If the requested object collides with the windowed object...
	if (ox > (wx + ww) || (ox + ow) < wx)
		{
		return false;
		}
	if (oy > (wy + wh) || (oy + oh) < wy)
		{
		return false;
		}

	// Collision Detected.
	return true;
	}

// Loop through all windowed objects and determine if there is a collision with the requested object. (IE Only)
function toggleWindowedCollisions(obj)
	{
	// If the client browser is IE...
	if (navigator.appVersion.toLowerCase().indexOf('msie') != -1)
		{
		// Get the list of windowed objects.
		var windowedObjects = document.getElementsByTagName("select");

		// Loop through the list of windowed objects.
		for (var i = 0; i < windowedObjects.length; i++)
			{
			// If the current windowed object collides with the requested object...
			if (collidesWithWindowedObject(obj, windowedObjects[i]))
				{
				// Turn off the windowed object.
				setVisibility(windowedObjects[i], false);
				}
			else
				{
				// If the current windowed object is not visible and there was no collision...
				if (!isVisible(windowedObjects[i]))
					{
					// Turn on the windowed object.
					setVisibility(windowedObjects[i], true);
					}
				}
			}
		}
	}

// Set the visibility of the requested object to the requested state (true of false).
function setVisibility(obj, visible)
	{
	obj.style.visibility = (visible ? 'visible' : 'hidden');
	}

// Return true if the requested object is visible.
function isVisible(obj)
	{
	return obj.style.visibility == 'visible';
	}

// Return the x and y dimensions of the requested object in an array.
function getMenuDimensions(obj)
	{
	var x = 0;
	var y = 0;

	do
		{
		x += obj.offsetLeft;
		y += obj.offsetTop;
		}
	while ((obj = obj.offsetParent));

	return new Array(x, y);
	}
