sfHover = function() {if(typeof(document.getElementById('nav')) != 'undefined'){
	
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			//this.className+=" sfhover";
			//alert('test');
			for(var j=0; j<sfEls.length; j++){
				if (sfEls[j].lastChild.nodeName == "UL" && !hasClass(sfEls[j].lastChild, 'subsubNav')) {
					sfEls[j].lastChild.style.left = '-9999px';
				}
			}
			
			if (this.lastChild.nodeName == "UL" && this.lastChild != document.getElementById("nav")) {
				if (hasClass(this.lastChild, 'subNav')) {
					this.lastChild.style.width = this.lastChild.offsetWidth - 11;
				}
				
				var randToMenu = findPos(document.getElementById('nav'));
				var randToItem = findPos(this);
				var itemWidth = this.offsetWidth;
				var subWidth = this.lastChild.offsetWidth;
				var itemToSub = (subWidth - itemWidth) / 2;
				var randToSub = randToItem - itemToSub + randToMenu;
				//var menuWidth = document.getElementById('nav').offsetWidth;
				var menuWidth = 951;
				
				if (hasClass(this.lastChild, 'subNav')) {
					
					this.lastChild.style.left = randToItem + 'px';
					//alert('rand->sub (' + randToSub + ') + subWidth (' + subWidth + ') = '+(randToSub+subWidth)+' //// randToMenu (' + randToMenu + ') + menuWidth (' + menuWidth + ') = '+(randToMenu+menuWidth));
					if (randToMenu > randToItem) {
						//align left
						this.lastChild.style.left = randToMenu + 'px';
					}
					else 
						if (randToSub + subWidth > randToMenu + menuWidth) {
							//align right
							this.lastChild.style.left = randToMenu + menuWidth - subWidth + 'px';
						}
						else {
							//align middle
							//this.lastChild.style.left = (findPos(this) - (this.lastChild.offsetWidth-this.offsetWidth)/2) + 'px';
							this.lastChild.style.left = findPos(this) + 'px';
						}
					
				//this.lastChild.style.left = (findPos(this) - (this.lastChild.offsetWidth-this.offsetWidth)/2) + 'px';
				}else if (hasClass(this.lastChild, 'subsubNav')){
					this.lastChild.style.left = 9999 + findPos(this) + 'px';
				}
			}
		}
		sfEls[i].onmouseout=function() {
			//this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			if(this.lastChild.nodeName == "UL" && hasClass(this.lastChild, 'subsubNav')){
				this.lastChild.style.left = '-9999px';
			}
		}
	}
}}
/*

if(this.lastChild.nodeName == "UL"){this.lastChild.style.left = '-9999px';}

*/
function findPos(obj){
	var currentleft = 0;
	if(obj.offsetParent){
		do{
			currentleft += obj.offsetLeft;
		}while(obj = obj.offsetParent);
		
		return currentleft;
	}
}

function hasClass (obj, className) {
	if (typeof obj == 'undefined' || obj==null || !RegExp) { return false; }
	var re = new RegExp("(^|\\s)" + className + "(\\s|$)");
	if (typeof(obj)=="string") {
		return re.test(obj);
	}
	else if (typeof(obj)=="object" && obj.className) {
		return re.test(obj.className);
	}
	return false;
}

sfHover();