var ie = window.navigator.appName == "Microsoft Internet Explorer";
var visible = -1;
var opene, subopen = 0;
var openid;
var xpos = 0, ypos = 0;
var leftX, leftY, rightX, rightY;                                     // position for menu
var mn_hide_timeout =2500;
var mn_timeout = false;

// opens mainmenu
function PauseMenu(){
	if (mn_timeout)
		clearTimeout(mn_timeout);
}

function RefreshMenu(){
	if (!ie) {
		if (mn_timeout)
			clearTimeout(mn_timeout);
		mn_timeout = setTimeout("CloseMenu();", mn_hide_timeout);
	}
}

function OpenMenu(x) {
	if (mn_timeout)
		clearTimeout(mn_timeout);

	CloseMenu();
		
	opene = document.getElementById("submenu" + x);
	if (opene == null)
		return;
	opene.style.display = "inline";
	openid = x;
	visible = 1;
  	
	if (ie) {
		// setup handle events
		xpos = window.event.clientX;
		ypos = window.event.clientY;

		document.body.onclick = CloseMenu;
		window.onscroll = CloseMenu;
		document.body.onmousemove = updateIt;
		tm = setInterval('test_it()', 300);
	}
}

// closes mainmenu
function CloseMenu() {
	if (mn_timeout)
		clearTimeout(mn_timeout);

	if (opene == null)
		return;

	opene.style.display = "none";
	opene = null;

	if (ie) {
		clearInterval(tm);
		//clear handled events
		document.body.onclick = null;
		window.onscroll = null;
		document.body.onmousemove = null;
  	}
	visible = -1;
	openid = 0;
}

function test_it() {
	var elem = document.elementFromPoint(xpos, ypos);
	if (!opene.contains(elem))
		if (!document.getElementById("user").contains(elem))
			if (document.getElementById("menu" + openid) == null || !document.getElementById("menu" + openid).contains(elem))
				CloseMenu();
}

function updateIt(e) {
	if (ie) {
		xpos = window.event.clientX;
		ypos = window.event.clientY;
	}
	else
		CloseMenu();
}

/**
 * Toggle box display
 * 
 * @param id string
 */
function toggleBox(id)
{
	window.document.getElementById(id).style.display = window.document.getElementById(id).style.display == 'block' ? 'none' : 'block';
}

