/*
 * menuDropdown.js - implements an dropdown menu based on an HTML list
 * Author: Dave Lindquist (dave@gazingus.org)
 * Additions/changes: David Viner (post@davidviner.com)
 */

var currentMenu = null;
var killMenu = false;
var	tm = false;

if (!document.getElementById)
	document.getElementById = function() { return null; }

function timer ()
{
	if (killMenu && currentMenu)
	{
		currentMenu.style.visibility = "hidden";
		currentMenu = null;
		clearTimeout (tm);
		tm = false;
	}
}

function initializeMenu (menuId, actuatorId)
{
	var menu = document.getElementById (menuId);
	var actuator = document.getElementById (actuatorId);

	if (menu == null && actuator == null) return;

	actuator.onmouseover = function()
	{
		killMenu = false;
		if (tm) clearTimeout (tm);
		tm = false;

		if (currentMenu != this)
		{
			if (currentMenu)
			{
				currentMenu.style.visibility = "hidden";
			}

			this.showMenu();
		}
		else
		if (currentMenu)
		{
			currentMenu.style.visibility = "hidden";
			currentMenu = null;
		}

		return false;
	}

	actuator.onmouseout = function()
	{
		killMenu = true;
		tm = setTimeout ("timer()", 500);
	}

	actuator.showMenu = function ()
	{
		if (menu)
		{
			var xy = findPos (this);

			menu.style.left = xy [0] + "px";
			menu.style.top = (xy [1] + 26) + "px";
			menu.style.visibility = "visible";
			currentMenu = menu;
		}
	}

	if (menu == null) return;

	menu.onmouseout = function()
	{
		killMenu = true;
		tm = setTimeout ("timer()", 500);
	}

	menu.onmouseover = function()
	{
		killMenu = false;

		if (tm) clearTimeout (tm);
	}
}

// findPosX/Y from: http://www.quirksmode.org/js/findpos.html

function findPos (obj)
{
	var	left = 0;
	var top = 0;

	if (obj.offsetParent)
	{
		left = obj.offsetLeft;
		top = obj.offsetTop;

		// WARNING: this loop will fail if it encounters a "position: relative" box

		while (obj = obj.offsetParent)
		{
			left += obj.offsetLeft;
			top += obj.offsetTop;
		}
	}

	return [left, top];
}


function findPosY (obj)
{
	var curtop = 0;

	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else
	if (obj.y)
	{
		curtop += obj.y;
	}

	return curtop;
}

function findPosX (obj)
{
	var curleft = 0;

	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else
	if (obj.x)
	{
		curleft += obj.x;
	}

	return curleft;
}

