var origsubmenu;
var cursubmenu;
var submenutimeout;

// wait until the dom is loaded
document.observe("dom:loaded", function() {
		// iterate through all the children of the submenu
		$('submenu').childElements().each(function(child) {
			// if this child is not hidden it is the original submenu
			if (!child.hasClassName('hidden')) {
				origsubmenu = child;
			} else {
				// if we keep the hidden class on the element then
				// the hide & show methods don't work
				child.hide();
				child.removeClassName('hidden');
			}
		});

		// highlight the form errors
		$$("form p.error").each(function(item) {
			new Effect.Highlight(item, { startcolor: '#00aeef' });
		});
});

function showsubmenu(menuname) {
	// if the timeout is still running restore our menus before we switch again
	if (submenutimeout) {
		_restoresubmenu();
	}

	// find the new menu
	cursubmenu = $(menuname);

	// hide the original
	if (origsubmenu) {
		origsubmenu.hide();
	}

	// show the new menu
	if (cursubmenu) {
		cursubmenu.show();

		// observe mouseover & mouseout
		// this is so the user can select something from the submenu
		cursubmenu.observe("mouseover", function() {
				clearTimeout(submenutimeout);
		});
		cursubmenu.observe("mouseout", function() {
				restoresubmenu();
		});
	}
}

function restoresubmenu() {
	clearTimeout(submenutimeout);
	submenutimeout = setTimeout("_restoresubmenu()", 250);
}

function _restoresubmenu() {
	clearTimeout(submenutimeout);
	if (cursubmenu) {
		cursubmenu.hide();
	}
	if (origsubmenu) {
		origsubmenu.show();
	}
}
