
var currentId = "";
var isObserving = false;

function showMenu(id, offset)
{
	if (currentId != id)
	{
		// Hide other active menus
		if (currentId != "")
		{
			hideMenu(currentId);
		}
		
		// 
		currentId = id;

		// Where to place
		var position = Position.cumulativeOffset($('menu'));		

		// Show and position
		$(id).style.display = "block";
		$(id).style.top = position[1] + "px";
		$(id).style.left = (position[0] + offset + 10) + "px";
		
		// Observe
		if (!isObserving)
		{
			Event.observe(document, 'mousemove', hideMenuEvent);
			isObserving = true;
		}
	}
}
function hideMenuEvent(evt)
{
    var child = Event.element(evt);
    if (currentId != "")
    {    
		var position = Position.cumulativeOffset($(currentId));
		var dimensions = Element.getDimensions($(currentId));	
		if (((Event.pointerX(evt) < position[0] + 1) || 
			(Event.pointerX(evt) > position[0] + dimensions["width"]) || 
			(Event.pointerY(evt) < position[1] + 1) || 
			(Event.pointerY(evt) > position[1] + dimensions["height"])))
		{
			hideMenu(currentId);
			currentId = "";
		}
	}
}
function hideMenu(id)
{
	$(id).style.display = "none";
	currentId = "";

}