// saffmenu.js
// dependencies: browser_ver.js, layer_util.js
var sx = 0;
var sy = 0;
var mouseX = 0, mouseY = 0;					// current mouse position
var currentMenu = "";						// current visible menu
var popleft, poptop, popright, popbottom; 	//  popup layer position
var menuleft, menutop, menuright, menubottom;	// menu position

// setup capturing mouse position
if (ns4)
	document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = getMousePos;

function getMousePos(e) {
	scrollTop = scrollLeft = 0;
	
	// need to use documentElement.scrollTop if IE is in standards complance mode...
	if (document.documentElement && document.documentElement.scrollTop) {
		scrollTop = document.documentElement.scrollTop;
		scrollLeft = document.documentElement.scrollLeft;
	}
	else if (document.body) {
		scrollTop = document.body.scrollTop;
		scrollLeft = document.body.scrollLeft;
	}
	
	if (ie4) {
		mouseX = window.event.clientX + scrollLeft;
		mouseY = window.event.clientY + scrollTop;
		sx = window.event.screenX;
		sy = window.event.screenY;
	} else if (ns4) {
		mouseX = e.pageX;
		mouseY = e.pageY;
		sx = e.screenX;
		sy = e.screenY;		
	}
	
	// if a popup is visible and the mouse moves outside of the popup, then hide the popup
	if (currentMenu != "") {
		// just looking at left, right, and bottom...
		if (mouseY < poptop - 2 || mouseY > popbottom + 2  || mouseX > popright + 2 || 
		   (mouseY > menubottom + 2 && mouseX < popleft - 2)) {
		   	hidemenu();
		}
	}
}

function showmenu(name) {
	divname = name + "div";
	menuname = name;
	var thislink = document.getElementById(menuname);
	var o;
    
	if(thislink.className!=null && thislink.className == "selected")
		return;
       
	hidemenu();
	
    thislink.style.background = '#c63 url("/img/saff/leftnav_rollover.gif")';	
	thislink.style.borderBottom = "1px solid #dedede";
	
	o = document.getElementById(menuname)
	menutop = getRealTopPos(o);
	menuleft = getRealLeftPos(o);
	menuright = menuleft + o.offsetWidth;
	menubottom = menutop + o.offsetHeight;
	
	o = document.getElementById(divname);
	
	o.style.top = (menutop - getRealTopPos(o.offsetParent)) + "px";
	o.style.left = menuright + "px";
	o.style.backgroundPosition = "0 " + (menubottom - menutop - 2) + "px";
	
	// hide the select box that can get overlapped by the people popup menus (this is such a kludge!)
	if (is_ie) {
		if (divname == "people_0div")
			s = document.getElementById("sectorlistdiv");
		else if (divname == "datasets_0div")
			s = document.getElementById("rm_select_state");
		else 
			s = null;
			
		if (s != null)
			s.style.visibility = "hidden";
	}
	
	show(divname);
	
	currentMenu = name;
	
	poptop = getRealTopPos(o);
	popleft = getRealLeftPos(o);
	popright = popleft + parseInt(o.offsetWidth);
	popbottom = poptop + parseInt(o.offsetHeight);	

}

function hidemenu() {
	if(currentMenu != "") {
		// make visible the select box that was overlapped by the popup menu
		if (is_ie) {
			if (divname == "people_0div")
				s = document.getElementById("sectorlistdiv");
			else if (divname == "datasets_0div")
				s = document.getElementById("rm_select_state");
			else 
				s = null;

			if (s != null)
				s.style.visibility = "visible";
		}
		
		hide(currentMenu + 'div');	
		var thislink = document.getElementById(currentMenu);
		thislink.style.background = '#366ea7 none';
		thislink.style.borderBottom = "1px solid #366ea6";
			
		currentMenu = "";
	}
}

function getRealTopPos(o) {
	iPos = 0
	while (o != null) {
	 	iPos += o.offsetTop
		o = o.offsetParent
	}
	return iPos
}

function getRealLeftPos(o) {
	iPos = 0
	while (o != null) {
	 	iPos += o.offsetLeft
		o = o.offsetParent
	}
	return iPos
}


