// This script makes the navigation menu work.

function showSubmenu() {
	$(this).children("a").addClass("expanded");
	$(this).siblings(":has(ul)").each(hideSubmenu);
	$(this).children("ul").each(function() {
		$(this).stop(true, true);
		if (!$(this).is(":visible"))
			if ($.browser.msie)
				$(this).show();
			else
				$(this).show(100);
	});
}

function hideSubmenu() {
	$(this).children("ul").each(function() {
		$(this).children("li:has(ul)").each(hideSubmenu);
		$(this).stop(true, true);
		if ($(this).is(":visible"))
			if ($.browser.msie)
				$(this).hide();
			else
				$(this).hide(100);
	});
	$(this).children("a").removeClass("expanded");
}

$(function () {
	// Set up the navigation menu.
	$("#navigation li:has(ul) > a").addClass("haschildren");
	$("#navigation li:has(ul) > a:not([href])").attr("href", "#").click(function() {return false;});
	$("#navigation li:has(ul)").hoverIntent({
		over: showSubmenu, out: hideSubmenu, timeout: 200
	}).focusin(function() {
		showSubmenu.call(this);
	}).focusout(function() {
		// Close the submenu UNLESS focus is staying within.
		// Wait until focus has moved to the next element.
		var parentItem = $(this); // Save the context for use in the setTimeout callback.
		setTimeout(function() {
			if (!$.contains(parentItem[0], document.activeElement))
				hideSubmenu.call(parentItem);
		}, 0);
	});
});

