/* Start config */

var secondsBeforeOpeningSubmenu = 0.1;
var secondsBeforeClosingSubMenu = 0.1;

/* End Config */

var mouseX = null;
var mouseY = null;

if (document.layers) { // Netscape
    document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove = getMouseXY;
} else if (document.all) { // Internet Explorer
    document.onmousemove = getMouseXY;
} else if (document.getElementById) { // Netcsape 6
    document.onmousemove = getMouseXY;
}

/**************
*
* isMouseOver(target)
*
* Determines whether the mouse cursor is currently over a given object.
*
/**************/
function isMouseOver(target) {
	var targetLoc = findXY(target);

	if ( (mouseX >= targetLoc[0] && 
			mouseX <= (targetLoc[0] + target.offsetWidth)) &&
		 (mouseY >= targetLoc[1] && 
			mouseY <= (targetLoc[1] + target.offsetHeight)) ) {
		return true;
	} else {
		return false;
	}
}

/**************
*
* findXY(target)
*
* Finds the actual X and Y co-ordinates of target regardless of the browser and window
* size and returns them in an [X,Y] array. Script courtesy of QuirksMode:
* http://www.quirksmode.org/js/findpos.html
*
/**************/
function findXY(target) {
	var curleft = curtop = 0;
	if (target.offsetParent) {
		curleft = target.offsetLeft
		curtop = target.offsetTop
		while (target = target.offsetParent) {
			curleft += target.offsetLeft
			curtop += target.offsetTop
		}
	}
	return [curleft,curtop];
}

/**************
*
* getMouseXY(e)
*
* Gets the current mouse position from a mousemove event and stores it. This gets
* set as an mousemove event handler in init, gets called by the browser directly,
* and refers to the mainNav object instantiated in bodyMainNav.jsp.
*
/**************/
function getMouseXY(e) {
		var newX = 0;
		var newY = 0;
		if (!e) var e = window.event;
		if (e.pageX || e.pageY) 	{
			newX = e.pageX;
			newY = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			newX = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
			newY = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
		}
	// Make sure we got values - these are sometimes undefined
	if (newX) {mouseX = newX;}
	if (newY) {mouseY = newY;}

	return true;
}

/* positionIFrame used to handle IE bug with divs overlaying form controls. */
function positionIFrame(divid, frmid)
{
	var div = document.getElementById(divid);
	divXY = findXY(div);
	var frm = document.getElementById(frmid);

	frm.style.position = 'absolute';
	frm.style.left = divXY[0];
	frm.style.top = divXY[1];
	frm.style.height = div.offsetHeight + 10;
	frm.style.width = div.offsetWidth;
	frm.style.display = "block";	
	frm.style.zIndex = 0;
}

var subMenuVisible = 'false';

var triggerShowSubMenu = function(subMenuName) {
	openTimeout = secondsBeforeOpeningSubmenu * 1000;
	setTimeout("this.showSubMenu('"+subMenuName+"')", openTimeout);
}

var triggerHideSubMenu = function (e) {
	if (subMenuVisible != 'false') {
		closeTimeout = secondsBeforeClosingSubMenu * 1000;
		setTimeout("this.hideSubMenu('" + subMenuVisible + "')", closeTimeout);
	}
}

var hideSubMenu = function (subMenuName) {
	theMenu = document.getElementById('navigation');
	subMenu = document.getElementById(subMenuName);

	if (!isMouseOver(subMenu) && !isMouseOver(theMenu)) {
		var frm = document.getElementById('menuIframe');
		if(frm) {
			frm.style.display = 'none';
		}

		subMenu = document.getElementById(subMenuName);
		subMenu.style.display = 'none';
	
		subMenuVisible = 'false';

		var subMenuList = theMenu.getElementsByTagName("ul");
		for( var i= 0; i < subMenuList.length; i++ ){
			subMenuList[i].style.display = 'none';
		}

	}
}

function showSubMenu(menuName,hideBread) {
	if (subMenuVisible !=  menuName+'_submenu') {
		theMenu = document.getElementById('navigation');
		var subMenuList = theMenu.getElementsByTagName("ul");
		for( var i= 0; i < subMenuList.length; i++ ){
			subMenuList[i].style.display = 'none';
		}

		subMenu = document.getElementById(menuName + '_submenu');
		subMenu.style.opacity = 0;
		subMenu.style.display = "block";

		/* if IE less than 7, position an iframe to handle form controls covering. */
			browserVersion = 0;
			if (navigator.appVersion.indexOf("MSIE")!=-1){
				temp = navigator.appVersion.split("MSIE");
				browserVersion = parseFloat(temp[1]);
			}

			if (browserVersion > 0) {
				positionIFrame(menuName+'_submenu','menuIframe');
			}

		var attributes = { 
			height: {from:0, to:100, unit:'%'},
			opacity: { from:0, to: 100 }
		};
		var myAnim = new YAHOO.util.Anim(menuName+'_submenu', attributes,20); 
		myAnim.animate(); 

		subMenuVisible = menuName+'_submenu';
	}
}

var showSMPeople = function (e) { triggerShowSubMenu('nav_people'); }
var showSMPractice = function (e) { triggerShowSubMenu('nav_practice'); }
var showSMStudentprogram = function (e) { triggerShowSubMenu('nav_studentprogram'); }
var showSMTransactions = function (e) { triggerShowSubMenu('nav_transactions'); }

YAHOO.util.Event.addListener("nav_people", "mouseover", showSMPeople);
YAHOO.util.Event.addListener("nav_practice", "mouseover", showSMPractice);
YAHOO.util.Event.addListener("nav_studentprogram", "mouseover", showSMStudentprogram);

YAHOO.util.Event.addListener("navigation", "mouseout", triggerHideSubMenu);