function mouseX(evt) {
	if (evt.pageX) return evt.pageX;
	else if (evt.clientX)
	   return evt.clientX + (document.documentElement.scrollLeft ?
	   document.documentElement.scrollLeft :
	   document.body.scrollLeft);
	else return null;
}
function mouseY(evt) {
	if (evt.pageY) return evt.pageY;
	else if (evt.clientY)
	   return evt.clientY + (document.documentElement.scrollTop ?
	   document.documentElement.scrollTop :
	   document.body.scrollTop);
	else return null;
}

hitTest = function(o, l){
	function getOffset(o){
		for(var r = {l: o.offsetLeft, t: o.offsetTop, r: o.offsetWidth, b: o.offsetHeight};
			o = o.offsetParent; r.l += o.offsetLeft, r.t += o.offsetTop);
		return r.r += r.l, r.b += r.t, r;
	}
	for(var b, s, r = [], a = getOffset(o), j = isNaN(l.length), i = (j ? l = [l] : l).length; i;
		b = getOffset(l[--i]), (a.l == b.l || (a.l > b.l ? a.l <= b.r : b.l <= a.r))
		&& (a.t == b.t || (a.t > b.t ? a.t <= b.b : b.t <= a.b)) && (r[r.length] = l[i]));
	return j ? !!r.length : r;
};

$(document).ready(function(){
	var currid = 0;
	$("#subNavBar").mouseout(function(e	){
		if(currid != 0){
			var hit= hitTest({offsetLeft:mouseX(e), offsetTop:mouseY(e), offsetWidth:1, offsetHeight:1}, document.getElementById("dept_"+currid+"_chan"));
			if(!hit){
				$(".subNav").hide();
				currid = 0;
				if(deptid != "")
					$("#dept_"+deptid+"_chan").show();
			}
		}
	});
	
	$("#navBar li").each(function(){
		var i = this.id.substring(3); 
		$(this).mouseover(function(){ 
			currid = i;
			$(".subNav").hide();
			$("#dept_"+i+"_chan").show();
		});
		$(this).mouseout(function(e){
			var hit= hitTest({offsetLeft:mouseX(e), offsetTop:mouseY(e), offsetWidth:1, offsetHeight:1}, document.getElementById("subNavBar"));
			if(!hit){
				$("#dept_"+i+"_chan").hide();
				currid = 0;
				if(deptid != "")
					$("#dept_"+deptid+"_chan").show();
			}
		});
		$("#dept_"+i+"_chan").mouseout(function(e){
			var hit= hitTest({offsetLeft:mouseX(e), offsetTop:mouseY(e)+5, offsetWidth:1, offsetHeight:1}, document.getElementById("subNavBar"));
			if(!hit){
				$(this).hide();
				currid = 0;
				if(deptid != "")
					$("#dept_"+deptid+"_chan").show();
			}
		});
	});
});

