//模仿VS.net菜单的Javascript菜单
//一个简单的菜单类

function Menu(){
	//菜单条<TR>
	this.MenuBar;
	//内容面板，所有的菜单内容都在这个上面<div>
	this.ContentPane;
	//当前的菜单，在菜单条上显示的菜单项目<TD>
	this.CurrentMenu;
	//当前菜单面板，显示菜单项目列表的容器<TABLE>
	this.CurrentMenuBoard;
	//当前的菜单项目<TR>
	this.CurrentMenuItem;
	this.cellIndex=0;

	//增加菜单条
	//同时设置MenuBar和ContentPane
	this.addMenuBar=function(){
		this.ContentPane = document.createElement("div");
		document.body.insertAdjacentElement("beforeEnd",this.ContentPane);
		this.ContentPane.style.cssText="top:;left:20;width:100%;height:22;border:1 outset;background-color:#6699CC;z-index:1000";
		
		var _menubar = document.createElement("table");
		_menubar.style.cssText="font:9pt";
		_menubar.border = 0;
		_menubar.width = 70;
		_menubar.cellPadding = 2;
		_menubar.cellSpacing = 2;
		
		this.ContentPane.insertAdjacentElement("beforeEnd",_menubar);
		this.MenuBar = _menubar.insertRow();
	}
	
	//在菜单条上增加菜单
	//同时设置当前菜单
	//参数：菜单的文本内容
	this.addMenu=function(_mStr){
		_menu = this.MenuBar.insertCell();
		_menu.style.cssText = cssText = "border:1px solid #6699CC;z-index:1000";
		_menu.noWrap = true;
		_menu.innerHTML = "&nbsp"+_mStr+"&nbsp";
		_menu.onmouseover = function(){
			_menu.parentElement.cells(this.cellIndex).style.cssText = "background-color:#B5BED6;border:1px solid #08246B";
			showMenuBoard(_menu.parentElement.cells(this.cellIndex));
		}
		_menu.onmouseout = function(){
			_menu.parentElement.cells(this.cellIndex).style.cssText = "border:1px solid #6699CC";
			hiddenMenuBoard(_menu.parentElement.cells(this.cellIndex));
		}
		this.CurrentMenu = _menu;
		this.cellIndex += 1;
	}
	
	//增加菜单项目列表容器
	//同时设置当前的容器
	//参数：容器的所有者（或者parentElement），是否为顶级容器
	this.addMenuBoard=function(_owner, _isTop){
		var _panel = document.createElement("div");
		_panel.style.cssText="position:absolute;border:1px outset;background-color:#6699CC;visibility:hidden;z-index:1000";
		
		if(_isTop){
			_panel.style.left = this.ContentPane.offsetLeft + _owner.offsetLeft + 3;
			_panel.style.top = this.ContentPane.offsetTop + this.ContentPane.offsetHeight - 3;
		}else{
			_panel.style.left = _owner.offsetLeft + _owner.offsetWidth;
			_panel.style.top = _owner.offsetTop - 1;
			
			_owner.parentElement.cells(0).onmouseover = function(){
				setMenuItemBgColor(_owner.parentElement, true)
				showMenuBoard(_owner);
			}
			_owner.parentElement.cells(0).onmouseout = function(){
				setMenuItemBgColor(_owner.parentElement, false)
				hiddenMenuBoard(_owner);
			}
			_owner.parentElement.cells(1).onmouseover = function(){
				setMenuItemBgColor(_owner.parentElement, true)
				showMenuBoard(_owner);
			}
			_owner.parentElement.cells(1).onmouseout = function(){
				setMenuItemBgColor(_owner.parentElement, false)
				hiddenMenuBoard(_owner);
			}
			_owner.parentElement.cells(2).onmouseover = function(){
				setMenuItemBgColor(_owner.parentElement, true)
				showMenuBoard(_owner);
			}
			_owner.parentElement.cells(2).onmouseout = function(){
				setMenuItemBgColor(_owner.parentElement, false)
				hiddenMenuBoard(_owner);
			}
		}
		
		this.CurrentMenuBoard = document.createElement("table");
		this.CurrentMenuBoard.style.cssText="font:9pt;background-color:#6699CC;";
		this.CurrentMenuBoard.border = 0;
		this.CurrentMenuBoard.width = 160;
		this.CurrentMenuBoard.cellPadding = 0;
		this.CurrentMenuBoard.cellSpacing = 0;
		
		_panel.insertAdjacentElement("beforeEnd", this.CurrentMenuBoard);
		_owner.insertAdjacentElement("beforeEnd", _panel);
	}
	
	//增加菜单项
	//同时设置当前菜单项
	//参数：菜单项目列表容器，菜单项文本，图表(16 x 16)，链接位置，窗口目标，是否有下级菜单
	this.addMenuItem=function(_board, _mStr, _mIcon, _mHref, _mTrg, _hasSubMenu){
		var _itemBar = _board.insertRow();
		_itemBar.style.cssText = "background-color:#FFFFFF";
		
		var _iconCell = _itemBar.insertCell();
		_iconCell.align = "center"; _iconCell.width = 20;
		_iconCell.noWrap = true;
		_iconCell.style.cssText = "border-bottom:1px solid #6699CC;border-left:1px solid #6699CC;border-top:1px solid #6699CC;background-color:#6699CC;padding:3px;z-index:1000";
		
		var _strCell = _itemBar.insertCell();
		_strCell.align = "left"; _strCell.width = 130;
		_strCell.noWrap = true;
		_strCell.style.cssText = "border-bottom:1px solid #FFFFFF;border-top:1px solid #FFFFFF;background-color:#FFFFFF;padding:3px;z-index:1000";
				
		if(_mStr != "-"){
			var _subCell = _itemBar.insertCell();
			_subCell.align = "right";
			_subCell.noWrap = true;
			_subCell.style.cssText = "border-bottom:1px soli #FFFFFF;border-right:1px solid #FFFFFF;border-top:1px solid #FFFFFF;background-color:#FFFFFF;padding:3px;z-index:1000";
			
			_iconCell.onmouseover = function(){
				setMenuItemBgColor(_iconCell.parentElement, true);
			}
			_iconCell.onmouseout = function(){
				setMenuItemBgColor(_iconCell.parentElement, false);
			}
			_strCell.onmouseover = function(){
				setMenuItemBgColor(_strCell.parentElement, true);
			}
			_strCell.onmouseout = function(){
				setMenuItemBgColor(_strCell.parentElement, false);
			}
			_subCell.onmouseover = function(){
				setMenuItemBgColor(_strCell.parentElement, true);
			}
			_subCell.onmouseout = function(){
				setMenuItemBgColor(_strCell.parentElement, false);
			}
			
			_iconCell.height = 22;
			_strCell.height = 22;
			_subCell.height = 22;
			if(_mIcon != ""){
				_iconCell.innerHTML = "<img src="+_mIcon+" border=0>";
			}else{
				_iconCell.innerHTML = "&nbsp;";
			}
			_strCell.innerHTML = "&nbsp;&nbsp;<a href="+ _mHref +" target="+ _mTrg +">"+ _mStr +"</a>";
			if(_hasSubMenu){
				_subCell.innerHTML = "<img src=images/right.gif>&nbsp;";
			}else{
				_subCell.innerHTML = "&nbsp;";
			}
		}else{
			_iconCell.height = 2;
			_strCell.height = 2;
			_strCell.colSpan = 2;
			_strCell.style.padding = 0;
			_strCell.innerHTML = "<TABLE width=95% height=2><TR><TD bgcolor=#6699CC></TD></TR></TABLE>";
		}
		this.CurrentMenuItem = _itemBar;
	}
	
	//显示_owner的菜单项目列表
	function showMenuBoard(_owner){
		var _obj = _owner.all.tags("DIV");
		_obj[0].style.visibility = "visible";
	}
	
	//隐藏_owner的菜单项目列表
	function hiddenMenuBoard(_owner){
		var _obj = _owner.all.tags("DIV");
		_obj[0].style.visibility = "hidden";
	}
	
	//设置菜单项目的背景颜色
	//参数：菜单项，是否选中
	function setMenuItemBgColor(_mItem, _isSelect){
		if(_isSelect){
			_mItem.cells(0).style.cssText = "border-bottom:1px solid #08246B;border-left:1px solid #08246B;border-top:1px solid #08246B;background-color:#B5BED6;padding:3px";
			_mItem.cells(1).style.cssText = "border-bottom:1px solid #08246B;border-top:1px solid #08246B;background-color:#B5BED6;padding:3px";
			_mItem.cells(2).style.cssText = "border-bottom:1px solid #08246B;border-right:1px solid #08246B;border-top:1px solid #08246B;background-color:#B5BED6;padding:3px";
		}else{
			_mItem.cells(0).style.cssText = "border-bottom:1px solid #6699CC;border-left:1px solid #6699CC;border-top:1px solid #6699CC;background-color:#6699CC;padding:3px";
			_mItem.cells(1).style.cssText = "border-bottom:1px solid #FFFFFF;border-top:1px solid #FFFFFF;background-color:#FFFFFF;padding:3px";
			_mItem.cells(2).style.cssText = "border-bottom:1px solid #FFFFFF;border-right:1px solid #FFFFFF;border-top:1px solid #FFFFFF;background-color:#FFFFFF;padding:3px";
		}
	}
	
	//递归_board的父菜单项列表容器
	this.getParentBoard=function(_board){
		if (_board.parentElement.tagName=="TABLE"){
			return _board.parentElement;
		}else{
			return this.getParentBoard(_board.parentElement);
		}
		
	}
}