/*
* menuDropdown.js - implements an dropdown menu based on a HTML list
* Author: Dave Lindquist (http://www.gazingus.org)
* Modified by: Donnie Tolbert (http://www.polarisindustries.com)
*/

var currentActuator = null;
var currentMenu = null;
var menuTimeoutID = null;

if (!document.getElementById)
	document.getElementById = function() { return null; }

function hideMenu() {
	if(currentMenu) {
		currentMenu.style.visibility = "hidden";
		currentMenu = null;
	}
	if(currentActuator) {
		/* currentActuator.style.backgroundColor = currentActuator.oldBackColor; */
		currentActuator.style.color = currentActuator.oldColor;
		currentActuator.actuatorImage.src = currentActuator.inactiveImage;
		currentActuator.menuBack.style.backgroundColor = currentActuator.originalBackColor;
		currentActuator = null;
	}
}

function getPageXY(elm)
{
	var point = { x: 0, y: 0 };
	while (elm)
	{
		point.x += elm.offsetLeft;
		point.y += elm.offsetTop;
		elm = elm.offsetParent;
	}
	return point;
}

function setPageXY(elm, x, y)
{
  var parentXY = {x: 0, y: 0 };

  if (elm.offsetParent)
  {
    parentXY = getPageXY(elm.offsetParent);
  }

  elm.style.left = (x - parentXY.x) + 'px';
  elm.style.top  = (y - parentXY.y) + 'px';
}

function initializeMenu(menuId, menuBackId, actuatorId, actuatorImageID, xoffset, yoffset, activatedColor, activatedBackColor, inactiveImage, activeImage) {
	var menu = document.getElementById(menuId);
	var menuBack = document.getElementById(menuBackId);
	var actuator = document.getElementById(actuatorId);
	var actuatorImage = document.getElementById(actuatorImageID);
	
	// set original background color
	/* actuator.oldBackColor = actuator.style.backgroundColor; */
	actuator.oldColor = actuator.style.color;
	actuator.xoffset = xoffset;
	actuator.yoffset = yoffset;
	actuator.activatedColor = activatedColor;
	actuator.inactiveImage = inactiveImage;
	actuator.actuatorImage = actuatorImage;
	actuator.activeImage = activeImage;
	actuator.activatedBackColor = activatedBackColor;
	actuator.originalBackColor = actuator.style.backgroundColor;
	actuator.menuBack = menuBack;
	
	if (menu == null || actuator == null) return;

	actuator.onmouseover = function() {
		if (currentMenu) {
			if(currentMenu != this) {
				hideMenu();
				this.showMenu(false);
			}
		}
		else {
			this.showMenu(false);
		}
		
		if(menuTimeoutID) clearTimeout(menuTimeoutID);
		
	}
	
	actuator.onclick = function() {
		
		if (currentMenu == null) {
			this.showMenu(false);
		}
		else {
			hideMenu();
		}

		return false;
	}

	actuator.showMenu = function(setTimer) {
		var p = getPageXY(this);
		//setPageXY(menu, p.x + this.xoffset, p.y + this.yoffset);
		
		menu.style.left = this.xoffset + 'px';
		menu.style.top = this.yoffset + 'px';	

		if(menu.filters && menu.filters[0]) menu.filters[0].apply();

		menu.style.visibility = 'visible';
		currentMenu = menu;
		currentActuator = this;
		
		if(menu.filters && menu.filters[0]) menu.filters[0].play();
		
		if(menuTimeoutID) clearTimeout(menuTimeoutID);
		if(setTimer)	menuTimeoutID = setTimeout('hideMenu()', 500);
		
		/* this.style.backgroundColor = "#EEEEEE"; */
		this.style.color = this.activatedColor;
		this.actuatorImage.src = this.activeImage;
		this.menuBack.style.backgroundColor = this.activatedBackColor;
	}
	
	actuator.onmouseout = function() {
		if(menuTimeoutID) clearTimeout(menuTimeoutID);
		menuTimeoutID = setTimeout('hideMenu()', 500);		
		
	}
	
	menu.onmouseout = actuator.onmouseout;
	
	menu.onmouseover = function() {
		if(menuTimeoutID) clearTimeout(menuTimeoutID);
	}
}