var popupTimeoutArray = new Array(4);
var currentActiveMainMenuItem = null;

//findPos function is from http://www.quirksmode.org/js/findpos.html;
//where its workings are explained in more detail.
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

//Display a named menu, at the position of another object
function display_menu(parent, named, order)
{
	for(i=1;i<=popupTimeoutArray.length;i++){
		if(document.getElementById('menu'+i)){
			document.getElementById('menu'+i).style.display="none";
			clearTimeout(popupTimeoutArray[i]);
			if(document.getElementById("mainMenuItem"+i)){
				document.getElementById("mainMenuItem"+i).className="";
			}
		}
	}
	//get the named menu
	var menu_element = document.getElementById(named);
	//override the 'display:none;' style attribute
	menu_element.style.display = "";
	//get the placement of the element that invoked the menu...
	var placement = findPos(parent);
	//...and put the menu there
	menu_element.style.left = placement[0] + "px";
	menu_element.style.top = placement[1] + 36 + "px";
	document.getElementById("mainMenuItem"+order).className="secili";
}

//Hide a named menu
function hide_menu(named, itemOrder)
{
	//alert(itemOrder);
	//get the named menu
	var menu_element = document.getElementById(named);
	//hide it with a style attribute
	popupTimeoutArray[itemOrder] = setTimeout("document.getElementById('"+named+"').style.display='none';if("+itemOrder+" != "+currentActiveMainMenuItem+"){document.getElementById('mainMenuItem"+itemOrder+"').className='';}", 500);
	//menu_element.style.display = "none";
}

function getSelectedMenuItem(){
	var i = 1;
	while(document.getElementById("mainMenuItem"+i)){
		if(document.getElementById("mainMenuItem"+i).className == "secili"){
			currentActiveMainMenuItem = i;
		}
		i++;
	}
}