﻿Type.registerNamespace("Explorica");

Explorica.MenuManager = function() {
	Explorica.MenuManager.initializeBase(this);
	this._menus = [];
	this._delMouseOver = null;
	this._delMouseOut = null;
	this._delMenuOut = null;
	this._active = null;
	this._liBounds = null;
	this._menuBounds = null;
}

Explorica.MenuManager.prototype = {
	initialize: function() {
		Explorica.MenuManager.callBaseMethod(this, "initialize");
		this._delMouseOver = Function.createDelegate(this, this._onOverItem);
		this._delMouseOut = Function.createDelegate(this, this._onOutItem);
		this._delMenuOut = Function.createDelegate(this, this._onOutMenu);
	},
	dispose: function() { 
		for (var i = 0; i < this._menus.length; i++) {
			var li = this._menus[i].parentNode;
			if (li) { $clearHandlers(li); }
		}
		Explorica.MenuManager.callBaseMethod(this, "dispose");
	},
	_clearActive: function() {
		if (this._active) {
			Sys.UI.DomElement.removeCssClass(this._active, "hover");
			this._active.menu.style.display = "none";
			this._active = this._liBounds = this._menuBounds = null;
		}
	},
	_setActive: function(li) {
		if (li && li.menu) {
			if (this._active != li) this._clearActive();
			this._active = li;
			li.menu.style.display = "";
			Sys.UI.DomElement.addCssClass(li, "hover");
			this._liBounds = Sys.UI.DomElement.getBounds(li);
			this._menuBounds = Sys.UI.DomElement.getBounds(li.menu);
		}
	},
	_onOverItem: function(e) {
		var li = Explorica.getEventTarget(e.rawEvent), m;
		if (li) {
			if (!Explorica.isTag(li, "li")) li = Explorica.getParentNode(li, "li");
			if (li && li.menu) { this._setActive(li); }
		}
	},
	_onOutItem: function(e) {
		//var ev = Explorica.getEvent(e.rawEvent), toElement;
		var ev = e.rawEvent, toElement;
		toElement = ev.relatedTarget || ev.toElement;
		if (Explorica.isSelfOrChildOf(toElement, this._active)) {
			//this._setActive(m.parentNode.menu);
		} else {
					var b = this._menuBounds, tar = Sys.UI.DomElement.getBounds(e.target), 
						p = new Sys.UI.Point(tar.x + e.offsetX, tar.y + e.offsetY);
						if (!(p.x > b.x && p.x < (b.x + b.width) && p.y > b.y && p.y < (b.y + b.height)))
							this._clearActive();
		}
		e.stopPropagation();
	},
	_onOutMenu: function(e) {
		var ev = Explorica.getEvent(e), m = Explorica.getEventTarget(ev), li;
		/*if (m) {
		li = ev.relatedTarget || ev.toElement;
		if (m.parentNode == li) {
		Sys.UI.DomElement.addCssClass(li, "hover");
		this._setActive(m);
		} else {
		Sys.UI.DomElement.removeCssClass(m.parentNode, "hover");
		this._hideMenu(m);
		}
		}*/
	},
	registerMenus: function(baseId, count) {
		var m, li;
		this._menus = new Array(count);
		for (var i = 0; i < count; i++) {
			this._menus[i] = m = $get(baseId + (i + 1));
			if (m) {
				m.onmouseout = this._delMenuOut;
				li = m.parentNode;
				if (li) {
					//li.onmouseover = this._delMouseOver;
					//li.onmouseout = this._delMouseOut;
					Sys.UI.DomEvent.addHandler(li, "mouseover", this._delMouseOver);
					Sys.UI.DomEvent.addHandler(li, "mouseout", this._delMouseOut);
					li.menu = m;
				}
			}
		}
	}
}

Explorica.MenuManager.registerClass("Explorica.MenuManager", Sys.Component);

if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
