var globalMenuTimer = null;
var timeout         = 200;
var active_li_element = null;

var isIE = false;
var isIE6 = false;
var temp_element_pointer = null; // used only with IE

//Prototype.Browser.IE6=Prototype.Browser.IE && parseInt(navigator.userAgent.substring (navigator.userAgent.indexOf("MSIE")+5))==6;
//Prototype.Browser.IE7=Prototype.Browser.IE && !Prototype.Browser.IE6;

function ddHitTest(event){
	// -- This function should only be needed with IE6
	if (active_li_element) {
		var m = {};
		m.x = event.pointerX();
		m.y = event.pointerY();
		//console.log("ddHitTest -> mx: "+m.x+", m.y: "+m.y);
		
		// get offset and dimensions of inner-most dropshadow container div
		var ds = active_li_element.down('div.ds4');
		var d = ds.getDimensions();
		var o = ds.cumulativeOffset();
		var sub_menu_hit = hitTest(m, o, d);
		//console.log('sub_menu_hit: '+(sub_menu_hit ? 'true' : 'false'));
		var a_tag = active_li_element.down('a');
		d = a_tag.getDimensions();
		o = a_tag.cumulativeOffset();
		var active_li_element_hit = hitTest(m,o,d);
		//console.log('active_li_element_hit: '+(active_li_element_hit ? 'true' : 'false'));
		return (sub_menu_hit || active_li_element_hit);
	}
}
function hitTest(m, o, d){
	// m -> mouse, o -> offset, d -> dimensions
	return (
		(m.x - 2) > o.left &&            // test left
		(m.x + 1) < o.left + d.width &&  // test right
		(m.y - 1) > o.top &&             // test top
		(m.y + 1) < o.top + d.height     // test bottom
	);
}

function removeSFHover(event){
	var hit = true;
	if(event) hit = ddHitTest(event);

	if (globalMenuTimer != null || event == undefined) {
		globalMenuTimer = window.clearTimeout(globalMenuTimer);
		if(active_li_element){
			// -- if removeSFHover was called without event that means it was called by a timeout and we need to remove the sfhover class
			if (event == undefined || hit === false) {
				active_li_element.removeClassName('sfhover');
				
				// -- IE 6 and 7 are special browsers with special needs so we need to treat it like the idiotic browsers that they are
				if(isIE){
					temp_element_pointer = active_li_element.down('div.ds1');
					temp_element_pointer.setStyle({left: -9999});
					window.setTimeout(function(){temp_element_pointer.setStyle({left: null});}, 200);
				}
				active_li_element = null;
			}
			else if (hit === false) {
				globalMenuTimer = window.setTimeout("removeSFHover()", timeout);
			}
		}
	}
	else if(active_li_element && hit === false) {
		globalMenuTimer = window.setTimeout("removeSFHover()", timeout);
	}
}
function addSFHover(element){
	if (active_li_element != element) {
		if (active_li_element)
			removeSFHover();
		if (globalMenuTimer)
			clearTimeout(globalMenuTimer);
		element.addClassName('sfhover');
		active_li_element = element;
		if (isIE6) {
			var div = element.down('div.ds1');
			var offset = element.positionedOffset();
			div.setStyle({
				left: offset.left,
				marginTop: offset.top
			});
		}
	}
}
document.observe('dom:loaded', function(){
	// set global for IE6 test
	isIE = Prototype.Browser.IE;
	isIE6 = isIE && navigator.appVersion.indexOf('MSIE 6') >= 0;
	// Get all top level menu items
	$$('#main-nav li.top-level').each(function(li){
		var a = li.down('a');
		li.observe('mouseout', function(event){ removeSFHover(event);});
		li.observe('mouseover', function(event){ addSFHover(li);});
	});
});
