dojo.addOnLoad(function(){
	var nodes = dojo.query("> ul > li", "nav");
	var timer = null;
	var onTimeoutFunction = null;
	var marginTop = "-19px";
	if ( dojo.isIE == 6 ) {
		marginTop = "-26px";
	} else if ( dojo.isIE == 7 ) {
		marginTop = "-19px";
	}
	var visible = {
		visibility: "visible",
		marginTop: marginTop
	};
	var hidden = {
		marginTop: "0px",
		visibility: "hidden"
	};
	function hideSubNav(subNav) {
		nodes.forEach(function(node){
			dojo.query("> ul", node).forEach(function(sn){
				if ( sn !== subNav ) {
					dojo.style(sn, hidden);
				}
			});
		});
	}
	function callOnTimeoutFunction() {
		if ( onTimeoutFunction ) {
			onTimeoutFunction();
		}
	}
	dojo.connect("page", "onmouseout", hideSubNav);
	nodes.style("position", "relative");
	nodes.forEach(function(node){
		var subNav = dojo.query("> ul", node)[0];
		if ( !subNav ) {
			return;
		}
		dojo.style(subNav, hidden);
		dojo.connect(node, "onmouseover", this, function(evt) {
			dojo.stopEvent(evt);
			if ( null !== timer ) {
				clearTimeout(timer);
				delete onTimeoutFunction;
				onTimeoutFunction = null;
				hideSubNav(subNav);
				delete timer;
				timer = null;
			}
			dojo.style(subNav, visible);
			dojo.style(node, "height", "19px");
		});

		dojo.connect(subNav, "onmouseout", this, function(evt) {
			dojo.stopEvent(evt);
			dojo.style(subNav, visible);
			onTimeoutNode = subNav;
			onTimeoutFunction = dojo.hitch(this, hideSubNav, null);
			if ( timer ) {
				clearTimeout(timer);
			}
			timer = setTimeout(function(){
				if ( onTimeoutFunction ) {
					onTimeoutFunction();
				}
			}, 500);
		});
	}, this);
});

dojo.addOnLoad(function(){
	var timer = null;
	var nodes = dojo.query("ul ul li", "nav");
	nodes.forEach(function(node) {
		dojo.connect(node, "onmouseover", function(evt){
			if ( timer !== null ) {
				clearTimeout(timer);
				delete timer;
				timer = null;
			}
			if ( true == changingBackground ) {
				var header = dojo.attr(node.parentNode, "header");
				var teaser = dojo.attr(node.parentNode, "teaser");
				dojo.style("header", "backgroundImage", "url('" + header + "')");
				dojo.style("teaser", "backgroundImage", "url('" + teaser + "')");
			}
		});
	});
	function resetBackground() {
		if ( true == changingBackground ) {
			dojo.style("header", "backgroundImage", initHeader);
			dojo.style("teaser", "backgroundImage", initTeaser);
		}
		delete timer;
		timer = null;
	}
	nodes.connect("onmouseout", function(evt){
		if ( timer !== null ) {
			clearTimeout(timer);
		}
		timer = setTimeout(resetBackground, 500);
	});
});