﻿Sureflix.UI.SimpleMenus = {
	menus: $H(),
	hideTimoutMillisecs: 5000
};


Sureflix.UI.SimpleMenus.setupMenus = function() {
	//only allow this to be run once
	if (this.menus.keys().length > 0) return;
	
	$$(".SureDropdownMenu").each(function(menuContainer) {
		var menu = new Sureflix.UI.SimpleMenu(menuContainer);
		Sureflix.UI.SimpleMenus.menus.set(menuContainer.identify(), menu);
	})
};


Sureflix.UI.SimpleMenus.findMenu = function(menuID) {
	return this.menus.get(menuID);
}


Sureflix.UI.SimpleMenus.collapseMenus = function(caller) {
	this.menus.each(function(pair) {
		var menu = pair.value;
		if (menu != caller || SxDt.isSmartphone()) menu.collapseMenu();
	});
}


Sureflix.UI.SimpleMenu = Class.create({

	initialize: function(menuContainer) {
		this.menuContainer = $(menuContainer);
		this.defaultCurrentMenu = null;
		this.visibleMenu = null;
		this.hideTimerID = null;

		this.fitMenusWithinPageContainer();
		this.findDefaultCurrentMenu();
		this.setupEvents();
	},


	fitMenusWithinPageContainer: function() {
		this.menuContainer.select("ul li div").each(function(subMenu) {
			var pageContainer = $("SurePageContainer");
			if (!pageContainer) pageContainer = $("SureSmphContainer");
			var pageContainerLeft = pageContainer.viewportOffset().left;
			var pageContainerWidth = pageContainer.getWidth();

			var subMenuLeft = subMenu.getOffsetParent().viewportOffset().left;
			var subMenuWidth = subMenu.getOutsideDimensions().width;

			//avoid the code breaking on IE
			if (subMenuWidth < 500) {
				if ((subMenuLeft + subMenuWidth) > (pageContainerLeft + pageContainerWidth)) {
					var subMenuLeft = (pageContainerLeft + pageContainerWidth) - (subMenuLeft + subMenuWidth);
					subMenu.setStyle({ left: subMenuLeft + 'px' });
				}
			}
		});
	},


	findDefaultCurrentMenu: function() {
		this.defaultCurrentMenu = this.menuContainer.down(".current");
		if (this.defaultCurrentMenu == null) {
			this.defaultCurrentMenu = this.menuContainer.down("li");
			this.setCurrentMenu(this.defaultCurrentMenu);
		}
	},


	setupEvents: function() {
		var menu = this;
		this.menuContainer.down("ul").childElements().each(function(menuElement) {
			if (SxDt.isSmartphone()) {
				menuElement.observe("click", menu.showMenu.bind(menu, menuElement));
			} else {
				menuElement.observe("mouseenter", menu.showMenu.bind(menu, menuElement));
				if (!menuElement.hasClassName("SureNoHover"))
					menuElement.observe("mouseleave", menu.collapseMenu.bind(menu));
			}
		});
		//window.observe("resize", this.fitMenusWithinPageContainer.bind(this));
	},


	collapseMenu: function() {
		if (this.hideTimerID) {
			clearTimeout(this.hideTimerID);
			this.hideTimerID = null;
		}

		var noHoverMenu = this.menuContainer.down('li.SureNoHover div');
		if (this.visibleMenu) {
			this.makeMenuHidden(this.visibleMenu);
			this.visibleMenu = null;
		} else if (noHoverMenu) {
			if (noHoverMenu.isVisible()) noHoverMenu.makeHidden();
		}

		this.setCurrentMenu(this.defaultCurrentMenu);
	},


	showMenu: function(menuElement) {
		SxUi.SimpleMenus.collapseMenus(this);

		if (menuElement) {
			this.setCurrentMenu(menuElement);
			this.visibleMenu = $(menuElement).down("div");
			if (this.visibleMenu == null) return;
			this.makeMenuVisible(this.visibleMenu);
			if (SxDt.isSmartphone()) {
				this.hideTimerID = setTimeout("SxUi.SimpleMenus.findMenu('" + this.menuContainer.identify() + "').collapseMenu()", Sureflix.UI.SimpleMenus.hideTimoutMillisecs);
			}
		}
	},

	makeMenuVisible: function(element) {
		if (typeof (jQuery) === "undefined" || typeof (jQuery.apycomMenu) === "undefined") {
			element.makeVisible();
		}
	},

	makeMenuHidden: function(element) {
		if (typeof (jQuery) === "undefined" || typeof (jQuery.apycomMenu) === "undefined") {
			element.makeHidden();
		}
	},


	setCurrentMenu: function(menuElement) {
		var lastCurrentMenu = this.menuContainer.down(".current");
		if (lastCurrentMenu) lastCurrentMenu.removeClassName("current");
		if (menuElement) menuElement.addClassName("current");
	}

});