/** hideSubOnMouseoverMain.js
  * Implements nav behavior that hides a visible subnav when the user mouses over another main nav item.
  * IMPORTANT: These functions are also used by hideSubOnMouseoverBody.js.
  */

/** public void itemOut : Differs from the default function in two ways:
  * a) it does not set the displayedSectionId to null; the displayedSectionId is correctly
  *    set to null only when the hideMenuListener executes and hides the subnav.
  * b) it doesn't roll out from main section items while their subnavs are displaying.
  * @param Event e Mouseout event triggering this function call
  * @param String itemId Id of nav item moused-out from
  */
itemOut = function(e, itemId) {
	// cancel bubbling
	if (e) {
		e.cancelBubble = true;
		if (e.stopPropagation) {
			e.stopPropagation();
			e.preventDefault();
		}
	}
	// hide status text
	window.status = "";
	var item = Navigation.getItem(itemId);
	// document has not finished loading or bogus itemId
	if (!item) return;
	// change the classes, images, etc. for items not already "on",
	// also section items should still be "over" while their subnavs are displaying
	if (!item.isOn() && (Navigation.getDisplayedSectionId() != item.getId())) item.out();
}

/** public void hideMenuListener : This hook is called from the itemOver
  * function, to hide the subnav when the appropriate element is moused over (e.g. body or item).
  * To find the itemId of section to hide, looks first for a displayedSectionId (item currently moused over);
  * if null (as when a subnav is displayed on page load), looks for a selected main section;
  * if null (as when a button is currently selected), gives up as no subnav is displayed.
  * @param Event e Mouseover event triggering this function call
  */
hideMenuListener = function(e) {
	// cancel bubbling
	if (e) {
		e.cancelBubble = true;
		if (e.stopPropagation) {
			e.stopPropagation();
			e.preventDefault();
		}
	}
	// clear the mouseover action on the body
	if (window.clearHideMenuListener) clearHideMenuListener();
	// get the currently displayed section id
	var displayedId = (Navigation.getDisplayedSectionId()) ? Navigation.getDisplayedSectionId() : ((Navigation.getSelectedIdAt(0) && Navigation.getItem(Navigation.getSelectedIdAt(0)).isSection()) ? Navigation.getSelectedIdAt(0) : null);
	var item = Navigation.getItem(displayedId);
	// document has not finished loading, bogus itemId, or item does not have a subnav to hide
	if (!item || !item.isSection()) return;
	// hide the currently displayed subnav
	item.hideMenu();
	// change the classes, images, etc. for items not already "on"
	if (!item.isOn()) item.out();
	// reset displayedSectionId, subnav is gone
	Navigation.setDisplayedSectionId(null);
}
