/**
 *
 * The MenuManager class implements the browser based dropdown menus
 *
 * depends on /3rd-party/jquery/dimensions.js
 *
 **/
var MenuManager =
{
	menuHideTimeout: null,
	menuShowTimeout: null,

	WAIT_TIME_MENU_HIDE_AFTER_MOUSEOUT: 500,
	WAIT_TIME_MENU_SHOW_AFTER_BUTTON_MOUSEOVER: 250,
	
	FADE_TIME_MENU_SHOW: 350,
	FADE_TIME_MENU_HIDE: 100,

	onPageLoad: function() {
		$(".ul_menu_container").each(function() {
			var htmlDropDown = this;
			var jqDropDown = $(this);

			var dropdownID = jqDropDown.attr("ID");
			var buttonID = jqDropDown.attr("buttonID");

			var jqButton = $("#" + buttonID);

			jqButton.mouseover(function() {
				if (MenuManager.getCurrentButtonID() == this.id) {
					//if we're already showing the current menu, don't reshow it
					MenuManager.clearHideMenuTimeout();
				} else {
					//moused over a new or different button, hide the current menu and showh the new one
					MenuManager.hideMenu();

					//show the dropdown, but wait a tick
					MenuManager.menuShowTimeout = window.setTimeout(function() {
						MenuManager.menuShowTimeout = null;

						MenuManager.currentMenu = {button: $("#" + buttonID), dropdown: $("#" + dropdownID)};

						MenuManager.showMenu();
					}, MenuManager.WAIT_TIME_MENU_SHOW_AFTER_BUTTON_MOUSEOVER);
				}
			});

			jqButton.mouseout(function() {
				if (MenuManager.currentMenu == null) {
					//if the user mouses off a button before the menu is shown, don't show the menu
					window.clearTimeout(MenuManager.menuShowTimeout);
				}else{
					//if the dropdown is showing, set the hide timeout
					MenuManager.setHideMenuTimeout();
				}
			});

			jqDropDown.mouseover(MenuManager.clearHideMenuTimeout);

			jqDropDown.mouseout(MenuManager.setHideMenuTimeout);
		});

		$(".ul_menu_container").click(MenuManager.hideMenu);

		$(".ul_menu_item").mouseover(function() {
			$(this).css("background-color", "#ffcc80");
		});

		$(".ul_menu_item").mouseout(function() {
			$(this).css("background-color", "#ffffff");
		});
	},

	showMenu: function() {
		MenuManager.clearHideMenuTimeout();

		var jqButton = MenuManager.currentMenu.button;
		var jqDropDown = MenuManager.currentMenu.dropdown;

		jqDropDown.top((jqButton.offset().top + 19) + "px");
		jqDropDown.left(jqButton.offset().left + "px");

		jqButton.css("background", "url(http://www.pandora.com/images/nav_button_menu_hover.gif)");

		if ($.browser.safari) {
			//HACK!  fade doesn't work with safari.  simply show the menu.
			MenuManager.currentMenu.dropdown.show();
		} else {
			MenuManager.currentMenu.dropdown.fadeIn(MenuManager.FADE_TIME_MENU_SHOW);
		}
	},

	hideMenu: function() {
		if (MenuManager.currentMenu) {
			var jqButton = MenuManager.currentMenu.button;
			jqButton.css("background", "url(http://www.pandora.com/images/nav_button_menu.gif)");

			if ($.browser.safari) {
				//HACK!  fade doesn't work with safari.  simply hide the menu.
				MenuManager.currentMenu.dropdown.hide();
			} else {
				MenuManager.currentMenu.dropdown.fadeOut(MenuManager.FADE_TIME_MENU_HIDE);
			}

			MenuManager.currentMenu = null;
		}
	},

	setHideMenuTimeout: function() {
		MenuManager.menuHideTimeout = window.setTimeout(MenuManager.hideMenu, MenuManager.WAIT_TIME_MENU_HIDE_AFTER_MOUSEOUT);
	},

	clearHideMenuTimeout: function() {
		if (MenuManager.menuHideTimeout != null) {
			window.clearTimeout(MenuManager.menuHideTimeout);
		}
	},

	getCurrentButtonID: function() {
		if (MenuManager.currentMenu != null) {
			return MenuManager.currentMenu.button.get(0).id;
		} else {
			return null;
		}
	}
}
$(document).ready(MenuManager.onPageLoad);
