var theTimer = null;
var prevDrop = null;

function DropDown(className) {
	els = document.getElementsByClassName(className, "feature_box");
	for(e in els) {
		els[e].onmouseover = function() {
			var dd = document.getElementById("dd" + this.rel);
			if(prevDrop) {
				clearTimeout(theTimer);
				prevDrop.style.display = "none";
			}
			prevDrop = dd;

			// ######## POSITIONING
			// Home page
			if(window.location.href.match(/\.(com|web)\/index/)) {
				if(navigator.appVersion.indexOf("Safari") > -1) { xoff = -1; yoff = 5; }
				else if(navigator.appVersion.indexOf("MSIE") > -1) { xoff = -34; yoff = 23; }
				else { xoff = 6; yoff = 6; } // Firefox

				dd.style.left = findPos(this)[0] + xoff + "px";
				dd.style.top = findPos(this)[1] + yoff + "px";
			} else { // Inner pages
				if(navigator.appVersion.indexOf("Safari") > -1) { xoff = 22; yoff = 10; } 
				else if(navigator.appVersion.indexOf("MSIE") > -1) { xoff = -15; yoff = 52; }
				else { xoff = 22; yoff = 12; } // Firefox

				dd.style.left = Position.positionedOffset(this)[0] + xoff + "px";
				dd.style.top = Position.positionedOffset(this)[1] + yoff + "px";
			}
			// ####################
			

			dd.style.display = "block"
			lis = dd.getElementsByTagName("li");
			for(l in lis) {
				lis[l].onmouseover = function() { clearTimeout(theTimer); prevDrop = this.parentNode; }
				lis[l].onmouseout = function() { theTimer = setTimeout("timeout()", 500); };
			}
		}
		
		els[e].onmouseout = function() { theTimer = setTimeout("timeout();", 500); };
	}
}

function timeout() {
	if(prevDrop) {
		prevDrop.style.display = "none";
		prevDrop = null;
	}
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

window.onload = function() {
	DropDown("homes");
	DropDown("life");
	DropDown("location");
	DropDown("vision");
	DropDown("next");
}
