var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;

var mouseX = 0;
var mouseY = 0;

var openddmenu;
var ddmenutopmargin = 0;
var ddmenuleftmargin = 0;

var hideddmenucount = 0;
var hideddmenutimeout = 5;
var watchddmenut = 0;

function showDDMenu(divid, callerid, type) {
	if (openddmenu && openddmenu != divid) hideDDMenu(openddmenu);
	
	cx = parseInt(callerid.offsetLeft);
	cy = parseInt(callerid.offsetTop);
	cw = parseInt(callerid.offsetWidth);
	ch = parseInt(callerid.offsetHeight);
	
	if (!cx) cx = parseInt(callerid.parentNode.offsetLeft);
	if (!cy) cy = parseInt(callerid.parentNode.offsetTop);
	
	/* It seems IE isn't reading the right offsetWidht if alligned to center */
	if (IE) {
		cx = cx + (document.body.offsetWidth - 20 - callerid.parentNode.offsetWidth) / 2;
	}
	
	/* Open dropdown menu on the right of the caller id */
	if (type == 1) {
		document.getElementById(divid).style.left = cx + cw;
		document.getElementById(divid).style.top = cy;
		ddmenutopmargin = 0;
		ddmenuleftmargin = cw;
	
	/* Open dropdown menu below the caller id */
	} else {
		document.getElementById(divid).style.left = cx;
		document.getElementById(divid).style.top = cy + ch;
		ddmenutopmargin = ch;
		ddmenuleftmargin = 0;
	}
	
	openddmenu = divid;
	document.getElementById(divid).style.visibility = 'visible';
	watchDDMenu();
}

function hideDDMenu(divid) {
	if (watchddmenut) clearTimeout(watchddmenut);
	hideddmenucount = 0;
	document.getElementById(divid).style.visibility = 'hidden';
	openddmenu = false;
}

function watchDDMenu() {
	if (!openddmenu) return false;
	
	divx = parseInt(document.getElementById(openddmenu).style.left)-ddmenuleftmargin;
	divy = parseInt(document.getElementById(openddmenu).style.top)-ddmenutopmargin;
	divw = parseInt(document.getElementById(openddmenu).offsetWidth)+ddmenuleftmargin;
	divh = parseInt(document.getElementById(openddmenu).offsetHeight)+ddmenutopmargin;
	
	if (mouseX < divx ||
		mouseY < divy ||
		mouseX > divx+divw ||
		mouseY > divy+divh)
	{
		hideddmenucount++;
		
		if (hideddmenucount > hideddmenutimeout) {
			hideDDMenu(openddmenu);
		} else {
			if (watchddmenut) clearTimeout(watchddmenut);
			watchddmenut = setTimeout("watchDDMenu();",100);
		}
		
	} else {
		hideddmenucount = 0;
		if (watchddmenut) clearTimeout(watchddmenut);
		watchddmenut = setTimeout("watchDDMenu();",100);
	}
}

function getMouseXY(e) {
	if (IE) {
		mouseX = event.clientX + document.body.scrollLeft;
		mouseY = event.clientY + document.body.scrollTop;
	} else {
		mouseX = e.pageX;
		mouseY = e.pageY;
	}  
	
	if (mouseX < 0) mouseX = 0;
	if (mouseY < 0) mouseY = 0;
}
