// Floats a <ul> by its parent
// Author: Mike Holloway
// Version: 2.1
// Customised for mooresappliances 10/05/2006

// <a href="" id="example" onmouseover="listFocus(this.id,'ID OF <UL>');" onmouseout="setState('ID OF <UL>',0);">Example of floating menu</a>
var isOn; // Checks if floating nav is still in state
var wait;
function setState(c,state){
	isOn = state;
	listBlur(c,0);
}

// Remove all child menus first
function removeOthers(){
	var ul = document.getElementsByTagName('ul');
	if(ul.length){
		for(var i=0;i<ul.length;i++){
			if(ul[i] && ul[i].className.match(/childMenu/gi)){
				ul[i].style.display='none';
			}
		}
	}
}


// Find true position of an object
// Updated to DOM compliance
function findPosX(obj)
{
	var curleft = 0;
	if (obj.parentNode){
		while (obj.parentNode){
			curleft += obj.offsetLeft;
			obj = obj.parentNode;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}
function findPosY(obj){
	var curtop = 0;
	if (obj.parentNode){
		while (obj.parentNode){
			curtop += obj.offsetTop;
			obj = obj.parentNode;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}



// listFocus
// @param	p	The parent object, ie. <a>
// @param	c	The child object, ie. <ul>

function listFocus(p,c){

	removeOthers();

	var parent = p;
	var child = c;

	// Set x,y position of menu
	// Horizontal Navigation, this makes menu appear underneath
	// link
	//var posX = (findPosX(parent) +parent.offsetWidth);
	//var posY = (findPosY(parent) +parent.offsetHeight);

	// Vertical Navigation, this makes menu appear to right of
	// link
	var posX = (parent.offsetWidth);
	//var posY = (findPosY(parent));
	var posY = (parent.offsetTop);

	child.style.margin='0';
	child.style.padding='0';
	child.style.position='absolute';
	child.style.display='block';
	child.style.left=posX+'px';
	child.style.top=posY+'px';
	child.style.zIndex='100';

	// Remove bullets from children lists
	if(child){
		var childA = child.getElementsByTagName('a');
		for(a=0;a<childA.length;a++){

			var li = childA[a].parentNode;

			li.style.listStyle='none';
			li.style.margin='0';
			li.style.padding='0';

			//childA[a].style.border='1px solid red';
			childA[a].style.display='block';

			// All of the child links have to have their height set in
			// pc ie
			childA[a].style.height='1%';
		}
	}

	child.onmouseover = function(){
		setState(c,1);
	}
	child.onmouseout = function(){
		setState(c,0);
	}

	setState(c,1);
}

// listBlur
// @param	c	The child object, ie. <ul>
// @param	clear	Start Count
function listBlur(c,clear){
	if(!isOn){
		if(parseInt(clear) != 0){
			c.style.display='none';
			clearTimeout(wait);
		}
		else wait=setTimeout(function(){listBlur(c,1)},500); // How long the nav menu stays open after mouseout
	}

}
