// Create the variable used to associate with the timer.
var currentmenu; var menustoclose = new Array();

$('#menutop').hover(function() { 
	// Drop any clear all timer that may be set, in turn keeping the menu open if somebody accidentally moused off and then back on.
	clearTimeout(menustoclose['all']); 
},function() { 
// No menu currently open. Set time out to close all submenus.
menustoclose['all'] = setTimeout("clearMenu('all')",500); 
});

$('ul a').hover(function() {
	// When the mouse enters a top level link, stop the timer, close any sibling links' menus, then open this link's menu.
	var menuclass = this.className;
	clearTimeout(menustoclose[currentmenu]);
	clearTimeout(menustoclose[menuclass]);
	$(this).parent().parent().find('.submenu').hide(); // Hide all child submenus of a -> li -> ul
	openMenu(menuclass); 
}, function() { });

$('.submenu').hover(function() {
	// When the mouse enters a submenu, stop any potential close timer.
	var menuclass = this.id;
	clearTimeout(menustoclose[menuclass]);
}, function() {
	// When the mouse leaves a submenu, set a closure timer.
	var menuclass = this.id;
	menustoclose[menuclass] = setTimeout("clearMenu('"+menuclass+"')",500);
});

function openMenu(menuid) { $('#'+menuid).show(); currentmenu = menuid; clearTimeout(menustoclose[currentmenu]); }
function clearMenu(menuid) {  if (menuid == 'all') { $('.submenu').hide(); currentmenu = ''; } else { $('#'+menuid).hide(); } }
