// saffmenu.js
// dependencies: browser_ver.js, layer_util.js

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;
	} else if (ns4) {
		mouseX = e.pageX;
		mouseY = e.pageY;
	}
	
	// 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 < menutop - 2  || mouseY > popbottom + 20  || mouseX < popleft + 2 || 
			mouseX > popright + 2 || (mouseY < menubottom && mouseX > menuright)) {
		   	hidemenu();
		}
	}
}


function highlightSelectedMenu(name) {
	var thislink = document.getElementById(name);
	thislink.style.background = '#CCC6BD';
}


function initMenuPosition(name) {
	divname = name + "div";
	menuname = name + "menu"
	var thislink = document.getElementById(menuname);
	var o;
	
	thislink.style.background = '#D4D0BE';
	
	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 = (menubottom + 1) + "px";
	o.style.left = (menuleft - 1) + "px";
	
	show(divname);
}


function showmenu(name) {
	divname = name + "div";
	menuname = name + "menu"
	var thislink = document.getElementById(menuname);
	var o;
	
	if(thislink.className == "selected")
		return;

	hidemenu();
	
	thislink.style.background = '#D4D0BE';
	
	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 = (menubottom + 1) + "px";
	o.style.left = menuleft + "px";
	
	show(divname);
	
	currentMenu = name;
	
	poptop = getRealTopPos(o);
	popleft = getRealLeftPos(o);
	popright = popleft + parseInt(o.offsetWidth);
	popbottom = poptop + parseInt(o.offsetHeight);	
}

function hidemenu() {

	if(currentMenu != "") {
		// hide the select box that can get overlapped by the popup menus 
		
		hide(currentMenu + 'div');
			
		var thislink = document.getElementById(currentMenu + 'menu');
		thislink.style.background = '#fff';
			
		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
}


