if (typeof WebControlNET == "undefined") var WebControlNET = {};
if (typeof WebControlNET.Menu == "undefined") {
	WebControlNET.Menu = {
		InstancesOnPage : [],
		Build : function(a) {
			WebControlNET.Menu.InstancesOnPage.push(new WebControlNET.Menu.MenuObject(a));
			var n = WebControlNET.Menu.InstancesOnPage.last();
			return n;
		},
		MenuObject : function(a) {
			this.id = ("WMenuID" + a);
			var o = $(this.id);
			this.orientation = o.classNames().include("-Horizontal-") ? "horizontal" : (o.classNames().include("-Vertical-") ? "vertical" : false);
			this.displaytype = o.classNames().include("-Text-") ? "text" : (o.classNames().include("-Image-") ? "image" : (o.classNames().include("-TextAndImage-") ? "textandimage" : false));
			this.expandable = o.classNames().include("-Expandable");
			this.iecompatibilitymode = false;
			if (Prototype.Browser.IE) {
				o.addClassName("WMenuIECompatibilityMode");
				this.iecompatibilitymode = true;
			}
			o.observe("click", this.OnClick.bindAsEventListener(this));
			o.observe("mouseover", this.OnMouseOver.bindAsEventListener(this));
			o.observe("mouseout", this.OnMouseOut.bindAsEventListener(this));
			var itm = [];
			var t = this;
			o.firstDescendant().childElements().each(function(oc) {
				var mio = new WebControlNET.Menu.MenuItemObject(oc.identify(), null, t);
				itm.push(mio);
			});
			this.Items = itm;
			o = ai = null;
		},
		MenuItemObject : function(a,b,c) {
			this.id = a;
			this.parent = (typeof b != "undefined" ? b : null);
			this.menu = (typeof c != "undefined" ? c : null);
			var o = $(this.id); var p = (this.parent != null ? $(this.parent) : null);
			this.state = this.originalstate = o.hasClassName("WMenuItemStateActive") ? "active" : (o.hasClassName("WMenuItemStateDisabled") ? "disabled" : "default");
			this.hoverstate = o.hasClassName("WMenuItemStateHover");
			this.previousstate = null;
			this.returntimeout = null;
			o.observe("click", this.OnClick.bindAsEventListener(this));
			o.observe("mouseover", this.OnMouseOver.bindAsEventListener(this));
			o.observe("mouseout", this.OnMouseOut.bindAsEventListener(this));
			this.hassubitems = false;
			var la = o.childElements().last();
			if (la != null && la.nodeName.toLowerCase() == "div") {
				this.hassubitems = true;				
				if (this.menu != null && this.menu.IsIECompatibilityMode() && this.menu.GetOrientation() == "horizontal" && this.menu.IsExpandable()) {
					var nmlt = la.getStyle("margin-left"), aml = 0;
					var nmtt = la.getStyle("margin-top"), amt = 0;
					if (nmlt != null && nmlt.indexOf("px") > -1) { aml = parseInt(nmlt.substr(0, nmlt.length-2)); }
					if (nmtt != null && nmtt.indexOf("px") > -1) { amt = parseInt(nmtt.substr(0, nmtt.length-2));}	
					var wml = (this.GetElement().getWidth() * -1 - 2), wmt = (this.GetElement().getHeight() - 2);
					if (aml != 0) { wml+=aml; }
					if (amt != 0) {	wmt+=amt; }
					la.setStyle({
						marginLeft: wml + "px",
						marginTop: wmt + "px"
					});
				}
				
				var itm = [];
				var t = this;
				o.firstDescendant().childElements().each(function(oc) {
					var mio = new WebControlNET.Menu.MenuItemObject(oc.identify(), t, t.menu);
					itm.push(mio);
				});
				this.Items = itm;
			}
			la = o = p = null;
		}
	};
	WebControlNET.Menu.MenuObject.prototype = {
		GetElement : function() { return $(this.id); },
		GetOrientation : function() { return this.orientation; },
		GetDisplayType: function() { return this.displaytype; },
		IsExpandable : function() { return this.expandable; },
		IsIECompatibilityMode : function() { return this.iecompatibilitymode; },
		OnClick : function(e) { },
		OnMouseOver : function(e) { },
		OnMouseOut : function(e) { }
	};
	WebControlNET.Menu.MenuItemObject.prototype = {
		GetElement : function() { return $(this.id); },
		IsRoot : function() { return (this.parent == null); },
		GetParent : function() { return this.parent; },
		GetMenu : function() { return this.menu; },
		GetLevel : function() { var l = 0; var p = this; do { p = p.GetParent(); l++; } while(p != null); return l-1; },
		GetState : function() { return this.state; },
		IsHoverState : function() { return this.hoverstate; },
		SetHoverState : function(a) {

		},
		HasSubItems : function() { return this.hassubitems; },
		GetOriginalState : function() { return this.originalstate; },
		GetPreviousState : function() { return this.previousstate; },
		SetState : function(a,b) {
			if (typeof b != "undefined" && b && this.state != a) { this.previousstate = this.state; }
			var o = this.GetElement();
			switch(this.GetState()) {
				case "active":
					o.removeClassName("WMenuItemStateActive");
					break;
				case "disabled":
					o.removeClassName("WMenuItemStateDisabled");
					break;
				default:
					break;
			}
			switch(a) {
				case "active":
					o.addClassName("WMenuItemStateActive");
					break;
				case "disabled":
					o.addClassName("WMenuItemStateDisabled");
					break;
				default:
					break;
			}
			this.state = a;
			o = null;
		},
		ReturnToState : function() {
			if (this.GetPreviousState() == null) throw "An attempt to return MenuItemObject with ID " + this.id + " to the previous state was made where no previous state exists";
			this.SetState(this.GetPreviousState());
		},
		SetHoverStateDelayed : function(a) {
			if (this.returntimeout) { clearTimeout(this.returntimeout); this.returntimeout = null; }
			var t = this;
			this.returntimeout = setTimeout(function() { t.SetHoverState(a); }, this.GetMenu().IsIECompatibilityMode() ? 50 : 10);
		},
		OnClick : function(e) { },
		OnMouseOver : function(e) { this.SetHoverState(true); },
		OnMouseOut : function(e) { this.SetHoverStateDelayed(false); }
	};
};
