/* class Menu */
function dx_Menu(owner) 
{
	this.items = [];
	this.owner = owner;
	this.id = "empty";
	this.offset = new dx_Point(0,0);
	this.altOffset = new dx_Point(0,0);
	this.width = 0;
	this.zIndex = 1000;
	this.menuFrame = null;
	this.menuElement = null;
	
	this.Initialize = function() {
		for(var i = 0; i < this.items.length; i++) {
			this.items[i].ItemInitialize();
		}
	}
	this.HasChildren = function() {
		return this.items.length > 0;
	}
	this.GetMenuByPoint = function(pt) {
		if( this.Contains(pt) )
			return this;
		for(var i = 0; i < this.items.length; i++) {
			var menu = this.items[i].GetMenuByPoint(pt);
			if(menu != null) return menu;
		}
		return null;
	}
	this.Contains = function(pt) {
		var el = this.GetMenuElement();
		if( Exists(el) && this.IsElemVisible(el) ) {
			return dxMenu.Pos.ElementContainsPoint(el, pt);
		}
		return false;
	}
	this.GetElementById = function(id) {
		return dxMenu.Obj.GetElementById(id);
	}
	this.GetMenuElement = function() {
		if(this.HasChildren() && Exists(this.menuElement) == false) {
			this.menuElement = this.GetElementById(this.id + "_Menu");
		}
		return this.menuElement;
	}
	this.GetMenuFrame = function() {
		if(this.HasChildren() && dxMenu.Inf.ie && dxMenu.Inf.ie4 == false) {
			if(Exists(this.menuFrame) == false)
				this.menuFrame = this.GetElementById(this.id + "_MenuFrame");
		}
		return this.menuFrame;
	}
	this.GetAnchorElement = function() {
		if(Exists(this.anchorElement) == false) {
			this.anchorElement = this.GetElementById(this.id + "_Anchor");
		}
		return this.anchorElement;
	}
	this.GetAnchorPos = function() {
		var el = this.GetAnchorElement();
		return ( Exists(el) ) ? dxMenu.Pos.GetAbsolutePos(el) : null;
	}
	this.HideChildren = function(skipItem) {
		for(var i = 0; i < this.items.length; i++) {
			var item = this.items[i];
			if(item != skipItem) {
				item.HideChildren(skipItem);
				item.HideMenu();
				item.Highlight(false);
			}
		}
	}
	this.PopupMenu = function(x, y) {
		this.SetMenuPos(x, y);
		this.ShowMenu();
	}
	this.SetMenuPos = function(x, y) {
		var el = this.GetMenuElement();
		if(null == el)
			return;
			
		dxMenu.Pos.SetAbsolutePos(el, x, y);
		var frame = this.GetMenuFrame();
		if(null == frame)
			return;
		
		dxMenu.Pos.SetAbsolutePos(frame, x, y);;
		var size = dxMenu.Pos.GetElementSize(el);
		frame.style.width = size.x;
		frame.style.height = size.y;
	}
	this.HideMenu = function() {
		var el = this.GetMenuElement();
		if( Exists(el) ) this.SetMenuVisibility(el, false);
	}
	this.HideMenuExt = function() {
		this.HideChildren(null);
		this.HideMenu();
	}
	this.ShowMenu = function() {
		var el = this.GetMenuElement();
		if( Exists(el) ) this.SetMenuVisibility(el, true);
	}
	this.SetMenuVisibility = function(el, visible) {
		this.SetElementVisibility(el, visible);
		this.SetFrameVisibility(visible);
		//if(dxMenu.Inf.ns4)
		this.SetContentVisibility(visible);
	}
	this.SetFrameVisibility = function(visible) {
		var frame = this.GetMenuFrame();
		if(null != frame)
			frame.style.display = visible ? "block" : "none";
	}
	this.SetContentVisibility = function(visible) {
		if(visible) this.SetItemPos();
		for(var i = 0; i < this.items.length; i++)
			this.items[i].SetLinkVisibility(visible);
	}
	this.SetItemPos = function() {
		for(var i = 0; i < this.items.length; i++) {
			this.items[i].SetElementPos();
		}
	}
	this.SetElementVisibility = function(el, visible) {
		dxMenu.Obj.SetElementVisibility(el, visible);
//		dxMenu.Obj.SetElementVisibility(el, true);
	}
	this.IsElemVisible = function(el) {
		return dxMenu.Obj.IsElementVisible(el);
	}
	this.GetItemById = function(id) {
		for(var i = 0; i < this.items.length; i++) {
			var item = this.items[i];
			if(item.id == id) return item;
			item = item.GetItemById(id);
			if( Exists(item) ) return item;
		}
		return null;
	}
	this.AddItem = function(id) {
		var item = this.CreateItem();
		if( Exists(id) ) item.id = id;
		this.items.Add(item);
		return item;
	}
	this.CreateItem = function() {
		return dxMenu.Inf.ns4 ? new dx_MenuItemNS4(this, this.owner) :
			dxMenu.Inf.op /*&& !dxMenu.Inf.op7*/ ? new dx_MenuItemOP(this, this.owner) : 
			dxMenu.Inf.ie4 ? new dx_MenuItemIE4(this, this.owner) :
			new dx_MenuItem(this, this.owner);
	}
}

/// class MenuItem 
function dx_MenuItem(parent, owner) 
{
	this.inherit = dx_Menu;
	this.inherit(owner);
	this.parent = parent;
	this.onmouseover = null;
	this.onclick = null;
	
	this.ItemInitialize = function() {
		var el = this.GetLinkElement();
		this.UpdateItemSize();
		this.SetLinkSize(el);
		this.Initialize();
	}
	this.SetElementPos = function() {
		var pt = this.GetAnchorPos();
		if( Exists(pt) ) {
			var el = this.GetLinkElement();
			if( Exists(el) ) dxMenu.Pos.SetAbsolutePos(el, pt.x, pt.y);
			el = this.GetSelectedElement();
			if( Exists(el) ) dxMenu.Pos.SetAbsolutePos(el, pt.x, pt.y);
		}
	}
	this.SetLinkVisibility = function(visible) {
		var el = this.GetLinkElement();
		if(this.itemSize.x == 0 || this.itemSize.y == 0) {
			this.SetLinkSize(el);
		}
		this.SetElementVisibility(el, visible);
//		this.SetElementVisibility(el, true);
	}
	this.GetLinkElement = function() {
		if(Exists(this.linkElement) == false) {
			this.linkElement = this.CreateLink();
			dxMenu.Pos.ZIndex(this.linkElement, this.parent.zIndex + 10);
		}
		return this.linkElement;
	}
	this.CreateLink = function() {
		var sz = this.GetItemSize();
		var s = this.GetLinkString(sz.x, sz.y);
		document.write(s);
//		alert(this.GetElementById(this.id + "_Link"))
		return this.GetElementById(this.id + "_Link");
	}
	this.SetLinkSize = function(el) {
		var sz = this.GetItemSize();
		this.SetLinkSizeEx(el, sz);
	}
	this.SetLinkSizeEx = function(el, sz) {
		var img = el.firstChild.firstChild;
		img.width = sz.x;
		img.height = sz.y;
	}
	this.GetLinkString = function(w, h) {
		var link = "href='#'"; 
		if( Exists(this.href) ) link = "href='" + this.href + "'";
		if( Exists(this.target) ) link += " target='" + this.target + "'";
		var action = this.GetOnClick(); 
		var onclick = this.GetEvent(" onclick", action, false);
		var style = this.GetProperty(" style", "position:absolute;visibility:hidden;width:" + w + ";height:" + h);
		return "<div id='" + this.id + "_Link'" + style + ">" +
			"<a " + link + onclick + " onmouseover=\"SelectMenuItem('" + this.id + "');\">" +
			"<img src='" + this.owner.GetImagePath("1x1.gif") + "' height=" + h + " width=" + w + " border=0/></a></div>";
	}
	this.GetOnClick = function() {
		if( Exists(this.onclick) )
			return this.onclick + ";"
		else if( Exists(this.owner.events) )
			return "RaiseItemEvent('" + this.owner.uniqueID + "','" + this.id + "');";
		return "";
	}
	this.GetEvent = function(name, action, retVal) {
		if( Exists(action) ) {
			var ret = (arguments.length >= 3) ? " return " + retVal : "";
			return this.GetProperty(name, action + ret);
		}
		return "";
	}
	this.GetProperty = function(name, val) {
		return Exists(val) ? name + "=\"" + val + "\"" : "";
	}
	this.GetItemSize = function() {
		if(Exists(this.itemSize) == false || this.itemSize.x <= 0 || this.itemSize.y <= 0) {
			this.UpdateItemSize();
		}
//		alert(this.itemSize.x)
		return this.itemSize;
	}
	this.UpdateItemSize = function() {
		var el = this.GetSelectedElement();
//		alert(el)
		this.itemSize = dxMenu.Pos.GetElementSize(el);
	}
	this.GetSelectedElement = function() {
		if(Exists(this.selectedElement) == false) {
			this.selectedElement = this.GetElementById(this.id + "_Selected");
 			dxMenu.Pos.ZIndex(this.selectedElement, this.parent.zIndex + 5);
		}
		this.SetElementWidth(this.selectedElement)
		return this.selectedElement;
	}
	this.SetElementWidth = function(el) {
		if(this.parent == null)
			return;
		var baseEl = this.GetAnchorElement();
		if( !Exists(baseEl) ) 
			return;
		var size = dxMenu.Pos.GetElementSize(baseEl);
		if(size != null)
			dxMenu.Pos.SetElementWidth(el, size.x);
	}
	this.Highlight = function(visible) {
		var el = this.GetSelectedElement();

		this.SetElementVisibility(el, visible);
	}
	this.IsSelected = function() {
		return false;
		var el = this.GetSelectedElement();
		return this.IsElemVisible(el);
	}
	this.Select = function() {
		if(this.IsSelected() == false) {
			this.parent.HideChildren(this);
			this.Highlight(true);
			if( this.HasChildren() ) {
				var pt = this.GetMenuPos();
				pt = this.CorrectMenuPos(pt.x, pt.y)
				this.PopupMenu(pt.x, pt.y);
			}
			dxMenu.Mng.OnSelectMenuItem(this);
		}
		this.Run(this.onmouseover);
	}
	this.CorrectMenuPos = function(x, y) {
		var el = this.GetMenuElement();
		var size = dxMenu.Pos.GetElementSize(el);
		var rx = 0;
		var cw = document.body.clientWidth;
		if(cw < x + size.x)
			rx = x + size.x - cw;
		return new dx_Point(x - rx, y);
	}
	this.GetItemRect = function() {
		var el = this.GetLinkElement();
		return dxMenu.Pos.GetElementRect(el);
	}
	this.RemoveHighlighting = function(pt) {
		if(this.HasChildren() == false && this.owner.autoHideSelection > 0) {
			var r = this.GetItemRect();
			r.Inflate(5, 5);
			if(r.Contains(pt) == false) {
				this.Highlight(false);
				return true;
			}
		}
		return false;
	}
	this.GetMenuPos = function() {
		var pt = this.GetAnchorPos();
		if( Exists(pt) ) {
			if( this.HasHorizontalParent() ) {
				var sz = this.GetItemSize();
				pt.y += sz.y;
			}
			
//		alert(dxMenu.Pos.GetWindowRect)
			var rc = dxMenu.Pos.GetWindowRect();
			
			var offset = this.CorrectOffsetX(this.rightToLeft, this.offset);
			var altOffset = this.CorrectOffsetX(!this.rightToLeft, this.altOffset);
			var part1 = this.GetHiddenPart(rc, pt, offset);
			var part2 = this.GetHiddenPart(rc, pt, altOffset);
			if(part1 <= part2) {
				var offsetX = offset.x;
				var offsetY = offset.y;
			}
			else {
				var offsetX = altOffset.x;
				var offsetY = altOffset.y;
			}
			pt.Offset(offsetX, offsetY);
		}
		return pt;
	}
	
	this.CorrectOffsetX = function(rightToLeft, offset) {
		var newOffset = new dx_Point(offset.x,offset.y);
		if(rightToLeft) {
			if(offset.x < 0) {
				var el = this.GetMenuElement();
				var size = dxMenu.Pos.GetElementSize(el);
				newOffset.x = offset.x - (size.x - this.width)
			}
		}
		else {
			if(parent.toString() != "[object MainMenu]") {
				var parentEl = parent.GetMenuElement();
				var parentSize = dxMenu.Pos.GetElementSize(parentEl);
				newOffset.x = offset.x + (parentSize.x - this.width)
			}
		}
		return newOffset;
	}
	
	this.GetHiddenPart = function(rc, pos, offset) {
		var pt = pos.Clone();
		pt.Offset(offset.x, offset.y);
		var leftPiece = Math.max(0, rc.left - pt.x);
		var rightPiece = Math.max(0, pt.x + this.width - rc.right);
		return leftPiece + rightPiece;
	}
	this.HasHorizontalParent = function() {
		return (this.owner == this.parent && this.owner.horizontal > 0);
	}
	this.Run = function(s) {
		if( Exists(s) ) eval(s);
	}
	this.Index = function() {
		return this.parent.items.IndexOf(this);
	}
}

//// class MenuItemIE4 ****
function dx_MenuItemIE4(parent, owner) 
{
	this.inherit = dx_MenuItem;
	this.inherit(parent, owner);

	this.CreateLink = function() {
		var sz = this.GetItemSize();
		var s = this.GetLinkString(sz.x, sz.y);
		document.body.insertAdjacentHTML("BeforeEnd", s);
		return this.GetElementById(this.id + "_Link");
	}
	this.SetLinkSize = function(el) {
	}
}

/// class MenuItemOP *****
function dx_MenuItemOP(parent, owner) 
{
	this.inherit = dx_MenuItem;
	this.inherit(parent, owner);

	this.SetLinkSize = function(el) {
		var sz = this.GetItemSize();
		this.SetElemSize(el, sz.x, sz.y);
	}
	this.SetElemSize = function(el, w, h) {
		el.style.pixelWidth = w;
		el.style.pixelHeight = h;
	}
	this.GetLinkString = function(w, h) {
 		var s = " onmouseover=\"SelectMenuItem('" + this.id + "')\"";
		var action = this.GetOnClick();
		if( Exists(this.href) )
			action += "dxMenu.Utl.NavigateUrl('" + this.href + "','" + this.target + "');";
		s += this.GetEvent(" onclick", action);
//		w = "300px"; h = "30px";
		var style = this.GetProperty(" style", /*"background-color:red;" + */"position:absolute;" + "visibility:hidden;" + "width: " + w + "; height: " + h + "; z-index:" + (this.parent.zIndex + 10));
//		alert("<div id='" + this.id + "_Link'" + style + s + "></div>")
		return "<div id='" + this.id + "_Link'" + style + s + "></div>";
	}
}
// class MenuItemNS4 ************
function dx_MenuItemNS4(parent, owner) 
{
	this.inherit = dx_MenuItem;
	this.inherit(parent, owner);

	this.Clear = function() {
		if( Exists(this.linkElement) ) {
			delete this.linkElement;
			this.linkElement = null;
		}
		for(var i = 0; i < this.items.length; i++) {
			this.items[i].Clear();
		}
	}
	this.GetAnchorPos = function() {
		var el = this.GetAnchorElement();
		if( Exists(el) ) return dxMenu.Pos.GetAbsolutePos(el);
		var el = this.parent.GetMenuElement();
		if( Exists(el) ) {
			var sz = this.GetItemSize();
			var pt = dxMenu.Pos.GetAbsolutePos(el);
			pt.x += this.parent.borderWidth;
			pt.y += this.parent.borderWidth;
			pt.y += this.Index() * sz.y;
			return pt;
		}
		return null;
	}
	this.CreateLink = function() {
		var sz = this.GetItemSize();
		var lyr = dxMenu.Obj.CreateLayer(sz.x, window, null);
		with(lyr) {
			clip.width = sz.x;
			clip.height = sz.y;
		}
		this.SetElementVisibility(lyr, false);
		lyr.menuItem = this;
		lyr.onmouseover = function() { SelectMenuItem(this.menuItem.id); }
		if( Exists(this.href) || Exists(this.owner.events) ) {
			lyr.captureEvents(Event.CLICK);
			lyr.onclick = function(){ this.menuItem.OnClick() };
		}
		return lyr;
	}
	this.SetLinkSize = function(el) {
	}
	this.OnClick = function() {
		dxMenu.Utl.NavigateUrl(this.href, this.target);
		var action = this.GetOnClick(); 
		if( Exists(action) ) eval(action);
	}
	this.CorrectMenuPos = function(x, y) {
		var el = this.GetMenuElement();
		var size = dxMenu.Pos.GetElementSize(el);
		var rx = 0;
		var cw = window.innerWidth;
		if(cw < x + size.x)
			rx = x + size.x - cw;
		return new dx_Point(x - rx, y);
	}
}
// class RootMenu ***************
function dx_RootMenu() 
{
	this.inherit = dx_Menu;
	this.inherit(this);
	this.horizontal = 0;
	this.events = null;
	this.imageFilesPath = "/ASPxNavigation/Images/";
	this.autoHideSelection = 0;

	if(dxMenu.Inf.ns4) {
		this.ItemClear = function() {
			for(var i = 0; i < this.items.length; i++) {
				this.items[i].Clear();
			}
		}
	}
	this.OnLoad = function() {
		this.SetContentVisibility(true);
	}
	this.GetImagePath = function(name) {
		return this.imageFilesPath + name;
	}
}
// class MainMenu *********
function dx_MainMenu() 
{
	this.inherit = dx_RootMenu;
	this.inherit();
	this.HideMenu = function() {}
	this.toString = function() { return "[object MainMenu]"; } 
}

// class ContextMenu ************
function dx_ContextMenu() 
{
	this.inherit = dx_RootMenu;
	this.inherit();
	this.OnLoad = function() {}
	this.toString = function() { return "[object ContextMenu]"; } 
}
// class MenuManager *********
function dx_MenuManager() 
{
	this.hideDelay = 3000;
	this.timer = null;
	this.activeMenu = null;
	this.activeMenuItem = null;
	this.menus = [];
	
	this.OnLoad = function() {
 		this.Initialize();
		for(var i = 0; i < this.menus.length; i++) {
			this.menus[i].OnLoad();
		}
	}
//	this.OnLoading = function() {
//		if(dxMenu.Inf.dom) this.Initialize();
//	}
	this.Initialize = function() {

		for(var i = 0; i < this.menus.length; i++) {
			this.menus[i].Initialize();
		}
	}
	this.OnResize = function() {
		this.HideAll();
		this.OnLoad();
	}
	this.HideChildren = function(skipItem) {
		for(var i = 0; i < this.menus.length; i++) {
			var item = this.menus[i];
			if(item != skipItem) item.HideMenuExt();
		}
	}
	this.HideAll = function() {
		this.HideChildren(null);
		this.ClearActiveMenu();
	}
	this.CreateMainMenu = function() {
		var menu = new dx_MainMenu();
		this.menus.Add(menu);
		return menu;
	}
	this.CreateContextMenu = function() {
		var menu = new dx_ContextMenu();
		this.menus.Add(menu);
		return menu;
	}
	this.GetMenuById = function(id) {
		var menu = this.GetRootMenu(id);
		if( Exists(menu) ) {
			if(menu.id == id) return menu;
			return menu.GetItemById(id);
		}
		return null;
	}
	this.GetRootMenu = function(id) {
		for(var i = 0; i < this.menus.length; i++)
			if(id.indexOf(this.menus[i].id) == 0) return this.menus[i];
		return null;
	}
	this.SelectItemId = function(id) {
		var menu = this.GetRootMenu(id);
		if( Exists(menu) ) {
			var item = menu.GetItemById(id);
			if( Exists(item) ) {
				this.HideChildren(menu);
				if(this.IsMenuVisible(this.activeMenu) == false) 
					this.ClearActiveMenu();
				item.Select();
			}
		}
	}
	this.IsMenuVisible = function(menu) {
		if( Exists(menu) ) {
			var el = menu.GetMenuElement();
			return dxMenu.Obj.IsElementVisible(el);
		}
		return false;
	}
	this.PopupMenu = function(id, x, y) {
		var menu = this.GetMenuById(id);
		if( Exists(menu) ) {
			this.HideAll();
			menu.PopupMenu(x,y);
			this.activeMenu = menu;
		}
	}
	this.HideMenu = function(id) {
		var menu = this.GetMenuById(id);
		if( Exists(menu) ) menu.HideMenuExt();
	}
	this.OnMouseDown = function(e) {
		var el = dxMenu.EvtInf.GetSrcElement(e);
		el = dxMenu.Obj.GetParentById(el, "_MenuItem");
		if(Exists(el) == false) this.HideAll();
	}
	this.OnMouseMove = function(e) {
		var pt = dxMenu.Pos.EventPoint(e, window);
		if(this.activeMenu != null && this.activeMenu.owner.autoHideSelection) {
			if( Exists(pt) ) {
				var menu = this.activeMenu.GetMenuByPoint(pt);
				if( Exists(menu) ) this.ClearTimeout();
				else this.SetTimeout(this.activeMenu.owner.autoHidePopupDelay/*this.hideDelay*/);
			}
		}
		if(this.activeMenuItem != null && this.activeMenuItem.RemoveHighlighting(pt) ) {
			this.activeMenuItem = null;
		}
	}
	this.OnSelectMenuItem = function(menuItem) {
		this.activeMenuItem = menuItem;
		this.activeMenu = menuItem.parent;
	}
	this.ClearTimeout = function() {
		if( Exists(this.timer) ) {
			clearTimeout(this.timer);
			this.timer = null;
		}
	}
	this.SetTimeout = function(delay) {
		if(Exists(this.timer) == false) {
			this.timer = setTimeout("dxMenu.Mng.HideActiveMenu()",delay);
		}
	}
	this.HideActiveMenu = function() {
/*		if( Exists(this.activeMenu) ) {
			this.activeMenu.HideMenuExt();
			this.ClearActiveMenu();
		}*/
		this.HideAll();
		this.ClearActiveMenu();
	}
	this.ClearActiveMenu = function() {
		this.activeMenu = null;
		this.ClearTimeout();
	}
}

