/*
MenuItem.action:
0 - None
1 - Redirect to url
2 - Redirect to url in new window
3 - Line
4 - Cause Postback
*/

function Menu(index){
	this.index = index;
	this.items = new Array();
	this.parentItem = null;

	this.addItem = function(text, action, actionArgus){
		this.items[this.items.length] = new MenuItem(text, action, actionArgus);
	}
}

function MenuItem(text, icon, action, actionArgu, subMenu){
	this.text = text;
	this.icon = icon;
	this.action = action;
	this.actionArgu = actionArgu;
	
	this.subMenu = subMenu;
	if(subMenu)
		subMenu.parentItem = this;
}

function MenuController(id){
	this.id = id;
	
	this.menus = new Array();
	this.roots = new Array();
	this.containers = new Array();
	this.covers = new Array();
	
	this.hideMenuTimerId = null;
	
	this.menuContainerCss = "";
	this.menuItemCss = "";
	this.menuItemTextCss = "";
	this.menuItemOverCss = "";
	this.menuIconCss = "";
	
	this.itemCover = null;
	this.itemCoverOffsetX = 0;
	this.itemCoverOffsetY = 0;
	
	this.showIcon = false;
	this.blankImage = "";
	
	this.rootsRepeatDirection = "Horizontal";
	
	this.menuFixed = false;
	
	this.isMac = (window.navigator.userAgent.toLowerCase().indexOf("mac") != -1);
	
	this.initialize = function(){
	}
	
	this.showMenu = function(root, index){
		if(this.hideMenuTimerId){
			window.clearTimeout(this.hideMenuTimerId);
			this.hideMenuTimerId = null;
		}
		this.hideMenuImmed(0, true);
		var container = this.containers[0];
		if(!container){
			container = this.createContainer();
			this.containers[0] = container;
		}
		
		var s = "";
		s += "<table cellpadding=4 cellspacing=0 border=0 width=100%>";
		var menu = this.menus[index];
		for(var i = 0; i <= menu.items.length - 1; i++){
			var item = menu.items[i];
			s += this.generateItemRow(item, 0);
		}
		s += "</table>";
		
		container.innerHTML = "";
		container.style.width = "160";
		container.innerHTML = s;
		container.style.display = "";
		
		var rootPos = new Position(0, 0);
		rootPos.fromObject(root);

		if(this.rootsRepeatDirection == "Vertical"){
			container.style.left = rootPos.x + root.offsetWidth;
			container.style.top = rootPos.y;
		}
		else{
			container.style.left = rootPos.x;
			container.style.top = rootPos.y + root.offsetHeight;
		}
		
		if(!this.isMac){
			container.cover.style.width = container.offsetWidth;
			container.cover.style.height = container.offsetHeight;
			container.cover.style.top = container.offsetTop;
			container.cover.style.left = container.offsetLeft;
			container.cover.style.display = "";
		}
	}
	
	this.showSubMenu = function(parentCell, index, level){
		parentCell = parentCell.parentNode.cells[(this.showIcon ? 1 : 0)];
		
		var container = this.containers[level];
		if(!container){
			container = this.createContainer();
			this.containers[level] = container;
		}
		
		var s = "";
		s += "<table cellpadding=4 cellspacing=0 border=0 width=100%>";
		var menu = this.menus[index];
		for(var i = 0; i <= menu.items.length - 1; i++){
			var item = menu.items[i];
			s += this.generateItemRow(item, level);
		}
		s += "</table>";
		
		container.innerHTML = "";
		container.style.width = "160";
		container.innerHTML = s;
		container.style.display = "";
		
		var parentPos = new Position(0, 0);
		parentPos.fromObject(parentCell, true);
		
		var adjustTop=0;
		var adjustLeft=0;
		if(this.isMac){
			/*container.style.top=0;
			container.style.left=0;
			if(container.offsetTop!=0)
				adjustTop=container.offsetTop-2;
			if(container.offsetLeft!=0)
				adjustLeft=container.offsetLeft-2;*/
		}
		
		container.style.left = parentPos.x + parentCell.offsetWidth + 2 - adjustLeft;
		container.style.top = parentPos.y - adjustTop;
		
		if(container.offsetLeft + container.offsetWidth > document.body.scrollLeft + document.body.clientWidth){
			container.style.left = parentPos.x - container.offsetWidth;
			container.style.top = parentPos.y - adjustTop;
		}
		
		if(!this.isMac){
			container.cover.style.width = container.offsetWidth;
			container.cover.style.height = container.offsetHeight;
			container.cover.style.top = container.offsetTop;
			container.cover.style.left = container.offsetLeft;
			container.cover.style.display = "";
		}
	}
	
	this.hideMenuScheduled = function(fromLevel, hideItemCover){
		this.hideMenuTimerId = window.setTimeout("document.findMenuController('" + this.id + "').hideMenuImmed(" + fromLevel + ", " + hideItemCover + ");", 1000);
	}
	
	this.hideMenuImmed = function(fromLevel, hideItemCover){
		if(this.menuFixed){
			this.menuFixed = false;
			return;
		}
		if(!fromLevel)
			fromLevel = 0;
		if(!hideItemCover)
			hideItemCover = false;
		for(var i = fromLevel; i <= this.containers.length - 1; i++){
			this.containers[i].style.display = "none";
			this.containers[i].cover.style.display = "none";
		}
		if(hideItemCover && this.itemCover)
		{
			this.itemCover.style.display = "none";
			this.itemCover.itemCoverBg.style.display = "none";
		}
	}
	
	this.generateItemRow = function(item, currentLevel){
		var s = "";
		var click = "", mouseover = "", mouseout = "", cursor = "";
		
		if(item.action == 3){
			cursor = " style=\"cursor: default;\"";
		}
		else{
			cursor = " style=\"cursor: hand; cursor: pointer;\"";
			if(item.action == 1)
				click += " onclick=\"window.location.href='" + item.actionArgu + "';\"";
			else if(item.action == 2)
				click += " onclick=\"window.open('" + item.actionArgu + "');\"";
			else if(item.action == 4)
				click += " onclick=\"" + item.actionArgu + ";\"";
			else
				cursor = " style=\"cursor: default;\"";
			
			if(this.isMac){
				mouseover += " onmouseover=\"";
			}
			else{
				if(this.showIcon)
					mouseover += " onmouseover=\"document.findMenuController('" + this.id + "').showItemCover(this);";
				else{
				mouseover += " onmouseover=\"this.className='" + this.menuItemOverCss + "';";
				 //this.lastChild.className='" + this.menuItemOverCss + "';";
					//mouseover += " onmouseover=\"this.className='" + this.menuItemOverCss + "';";
					mouseout = " onmouseout=\"this.className='" + this.menuItemCss + "'\";";
				}
			}
			
			mouseover += "document.findMenuController('" + this.id + "').hideMenuImmed(" + (currentLevel + 1) + ");";
			if(item.subMenu)
				mouseover += "document.findMenuController('" + this.id + "').showSubMenu(this, " + item.subMenu.index + ", " + (currentLevel + 1) + ");";
			mouseover += "\"";
		}
		s += "<tr>";
		if(this.showIcon)
			s += "<td" + click + mouseover + mouseout + cursor + " class=\"" + this.menuIconCss + "\"><img src=\"" + ((item.icon == "") ? this.blankImage : item.icon) + "\" width=16 height=" + ((item.action == 3) ? "1" : "16") + "></td>";
		s += "<td" + click + mouseover + mouseout + cursor + " class=\"" + this.menuItemCss + "\">";
		if(item.action == 3){
			s += "<table cellpadding=0 cellspacing=0 border=0 style=\"width:100%;\">";
			s += "<tr>";
			s += "<td style=\"background-color:#000000;height:1px;\"></td>";
			s += "</tr>";
			s += "</table>";
		}
		else{
			s += "<table cellpadding=0 cellspacing=0 border=0 style=\"width:100%;\"";
			s += " onmouseover=\"this.rows[0].cells[0].className='"+"SubMenuOver"+"'\";";
			s += " onmouseout=\"this.rows[0].cells[0].className='" + "SubMenu" + "'\";";
			s +=">";
			s += "<tr>";
			//s += "<td class=\"" + this.menuItemTextCss + "\" style=\"width:100%;\" nowrap>" + item.text + "</td>";
			s +="<td";
			
			s += " class=\"" + this.menuItemTextCss + "\" style=\"width:100%;\" nowrap>" + item.text + "</td>";
		
			
			s += "<td align=\"right\"><img src=\"images/" + (item.subMenu ? "SubMenu_Icon.gif" : "blank.gif") + "\" alt=\"\" height=\"16\" width=\"16\" /></td>";
			s += "</tr>";
			s += "</table>";
		}
		s += "</td>";
		s += "</tr>";
		return s;
	}
	
	this.showItemCover = function(itemCell){
		if(!this.itemCover){
			this.itemCover = document.createElement("DIV");
			this.itemCover.style.position = "absolute";
			this.itemCover.style.display = "none";
			this.itemCover.style.zIndex = 4;
			this.itemCover.menuController = this;
			this.itemCover.onmouseover = function(){
				window.clearTimeout(this.menuController.hideMenuTimerId);
				this.menuController.hideMenuTimerId = null;
			}
			this.itemCover.onmouseout = function(){
				this.menuController.hideMenuTimerId = window.setTimeout("document.findMenuController('" + this.menuController.id + "').hideMenuImmed(0, true);", 1000);
			}
			document.body.appendChild(this.itemCover);
			
			var innerTable = document.createElement("TABLE");
			innerTable.cellPadding = "4";
			innerTable.cellSpacing = "0";
			innerTable.border = "0";
			innerTable.insertRow(0);
			this.itemCover.appendChild(innerTable);
			
			this.itemCover.itemCoverBg = document.createElement("DIV");
			this.itemCover.itemCoverBg.style.position = "absolute";
			this.itemCover.itemCoverBg.style.display = "none";
			this.itemCover.itemCoverBg.style.zIndex = 3;
			this.itemCover.itemCoverBg.className = this.menuItemOverCss;
			document.body.appendChild(this.itemCover.itemCoverBg);
		}
		
		var pos = new Position(0, 0);
		pos.fromObject(itemCell);
		this.itemCover.style.top = pos.y + this.itemCoverOffsetY;
		pos.fromObject(itemCell.offsetParent);
		this.itemCover.style.left = pos.x + this.itemCoverOffsetX;
		
		var row = this.itemCover.childNodes[0].rows[0];
		while(row.cells.length > 0)
			row.deleteCell(0);
		for(var cellIndex = 0; cellIndex <= itemCell.parentNode.cells.length - 1; cellIndex++){
			var cell = row.insertCell(row.cells.length);
			var srcCell = itemCell.parentNode.cells[cellIndex];
			cell.align = srcCell.align;
			cell.width = srcCell.offsetWidth - 8;
			cell.onclick = srcCell.onclick;
			cell.style.cursor = srcCell.style.cursor;
			cell.innerHTML = srcCell.innerHTML;
		}
		
		this.itemCover.style.display = "";
		this.itemCover.itemCoverBg.style.display = "";
		this.itemCover.itemCoverBg.style.top = this.itemCover.offsetTop;
		this.itemCover.itemCoverBg.style.left = this.itemCover.offsetLeft;
		this.itemCover.itemCoverBg.style.width = this.itemCover.offsetWidth;
		this.itemCover.itemCoverBg.style.height = this.itemCover.offsetHeight;
	}
	
	this.createContainer = function(){
		var container = document.createElement("DIV");
		container.menuController = this;
		container.style.position = "absolute";
		container.className = this.menuContainerCss;
		container.style.zIndex = 2;
		container.style.display = "none";
		container.onmouseout = function(){
			this.menuController.hideMenuTimerId = window.setTimeout("document.findMenuController('" + this.menuController.id + "').hideMenuImmed(0, true);", 1000);
		}
		container.onmouseover = function(){
			if(this.menuController.hideMenuTimerId){
				window.clearTimeout(this.menuController.hideMenuTimerId);
				this.menuController.hideMenuTimerId = null;
			}
		}
		document.body.appendChild(container);
		
		var cover = document.createElement("IFRAME");
		cover.frameborder = "0";
		cover.style.position = "absolute";
		cover.style.zIndex = 1;
		cover.style.display = "none";
		cover.style.border = "none";
		cover.src = "_blank";
		container.cover = cover;
		document.body.appendChild(cover);
		
		return container;
	}
}

function Position(x, y){
	this.x = x;
	this.y = y;
	
	this.isMac = (window.navigator.userAgent.toLowerCase().indexOf("mac") != -1);
	
	this.fromObject = function(obj, handleTD){
		if(!handleTD)
			handleTD = false;
		var left = 0, top = 0;
		var tmp = obj;
		while(tmp.offsetParent){
			left += tmp.offsetLeft;
			top += tmp.offsetTop;
			if(this.isMac && tmp.tagName == "TD" && handleTD){
				if(tmp.offsetTop == 0){
					var oTr = tmp.parentNode;
					oTbl = oTr;
					while(oTbl.tagName != "TABLE")
						oTbl = oTbl.parentNode;
					for(var rowIndex = 0; rowIndex <= oTr.rowIndex - 1; rowIndex++){
						if(oTbl.rows[rowIndex].cells.length > 0)
							top += oTbl.rows[rowIndex].cells[0].offsetHeight;
					}
				}
			}
			tmp = tmp.offsetParent;
		}
		if(tmp.tagName && tmp.tagName == "BODY")
		{
			left += tmp.offsetLeft;
			top += tmp.offsetTop;
		}
		this.x = left, this.y = top;
	}
}
