// Horizontal Menu javascript
// built by http://www.iconico.com

var objTimer;
var objDrop;
var objMenuLast;

//shows menu
function appear(id) {
	var objMenu = document.getElementById(id.replace('X', ''));
	clearTimeout(objTimer);	//remove any old timers
	if (objMenu != objMenuLast)	//if we're on a new menu then hide the last immediatly
		doDisappear();
	//style
	objMenu.style.backgroundColor = '#0773DF'; //#0066CB';
	objMenuLast = objMenu;
	//dropdown
	//objDrop = objMenu.childNodes[1];	//get the menu node
	objDrop = document.getElementById(objMenu.id + 'X');
	if (objDrop) {
		objDrop.style.left = parseInt(findPosX(objMenu)) + 'px';	//get left position
		objDrop.style.top = "122px";
		objDrop.style.display = 'inline';
	}
	checkInter("SELECT", false, objDrop);
}

//hides a menu
function disappear(id) {
	var objMenu = document.getElementById(id.replace('X', ''));
	objMenuLast = objMenu;
	//objDrop = objMenu.childNodes[1];	//get the menu node
	objDrop = document.getElementById(objMenu.id + 'X');
	objTimer = setTimeout('doDisappear();', 400);
}

//internal function for hiding menu
function doDisappear() {
	if (objMenuLast)
		objMenuLast.style.backgroundColor = '';
	if (objDrop)
		objDrop.style.display = 'none';
	checkInter("SELECT", true, objDrop);
}

//checks if nav intersects select boxes
function checkInter(strTag, blnShow, objNav) {

	var sel = document.getElementsByTagName(strTag);
	if (objNav) {
		//Create the intersection array if needed
		if (!objNav.smartIntersect) {
			var arrSel = new Array;
			var objDropR = new Recto(objNav);	//dropdown list rectangle
			for (var i=0; i<sel.length; i++) {
				var objSelR = new Recto(sel[i]);
				arrSel[i] = RectoInter(objDropR, objSelR)	//check select is under the nav
			}
			objNav.smartIntersect = arrSel;	//asign to DOM object for later access
		}

		//apply the styles to select dropdowns as needed
		for (var i=0; i<sel.length; i++) {
			if (objNav.smartIntersect[i]) {
				sel[i].style.visibility = blnShow ? "visible" : "hidden"; 
			}
		}
	}
}

//Rectangle object constructor, takes any DOM object
function Recto(obj) {
	if (obj) {
		this.left = findPosX(obj);
		this.top = findPosY(obj);
		this.width = obj.offsetWidth;
		this.height = obj.offsetHeight;
		this.bottom = this.top + this.height;
		this.right = this.left + this.width;
	}
}

//checks to see if two Recto objects intersect
function RectoInter(r1, r2) {
	var inter = false;
	//Find the leftmost etc rectangle. 
	var leftMost = r1.left < r2.left ? r1 : r2;
	var nonLeftMost = r1.left >= r2.left ? r1 : r2;
	var topMost = r1.top < r2.top ? r1 : r2;
	var nonTopMost = r1.top >= r2.top ? r1 : r2;
/*  If the left x co-ordinate of the non-leftmost rectangle is between the 
left and the right of the leftmost rectangle, you have the left cordinate of 
your intersection. If it is to the right you have no intersection, and the 
rectangles don't overlap. */
	if (nonLeftMost.left >= leftMost.left && nonLeftMost.left <= leftMost.right && nonTopMost.top >= topMost.top && nonTopMost.top <= topMost.bottom) inter = true;
	return inter;
}


//return screen X position
function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}


//return screen Y position
function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return 235;
	//return curtop;
}



//Sub Menu
function appearSub(objSubMenu) {
	objSubMenu.style.width = '195px';
}

function disappearSub(objSubMenu) {
	objSubMenu.style.width = '99px';
}



