$package("js.ui")

js.ui.MenuSeparator = function (){
}

js.ui.MenuSeparator.prototype = new js.ui.Component();

js.ui.MenuSeparator.prototype.getNativeClass = function (){
	return "js.ui.MenuSeparator";
}

js.ui.MenuItem = function (){
	this.items = new js.lang.Array();
}

js.ui.MenuItem.prototype = new js.ui.Component();

js.ui.MenuItem.prototype.selected = false ;

js.ui.MenuItem.prototype.isParent = false ;


js.ui.MenuItem.prototype.getNativeClass = function (){
	return "js.ui.MenuItem";
}

js.ui.MenuItem.prototype.bindUI = function (){
	var tds = this.ui.getElementsByTagName("tr")[1].getElementsByTagName("td");
	this.iconCell = tds[1];
	this.text = tds[2];
	this.rightCell = tds[3];

	var __this = this;
	this.ui.onmouseover = function(){
		this.className = "MenuItem Panel Panel_over";
		__this.setSelected(true);
		if(__this.__isMenuBarItem && !__this.menu.__isPopuped) return;
		if(__this.hasSubMenu() && __this.subMenu.visible) return;
		if(__this.menu){
			if(__this.menu.hideSubMenu){
				__this.menu.hideSubMenu(__this.menu);
			}
		}
		__this.showSubMenu();
//		__this.setSelected(true);
	}
	this.ui.onmouseout = function(){
		this.className = "MenuItem Panel";
	}
	this.ui.onkeydown = function(event){
	}
	this.ui.onmousedown = function(event){
		return js.ui.Component.eventCancelBubble(event);
	}
	this.ui.onclick = function(event){
		if(!event)event=window.event;
		if(!__this.hasSubMenu()){
			if (__this.menu && !__this.__isMenuBarItem){
				__this.menu.hide();
			}
			if(!__this.root.isMenuBar)
				__this.root.hide();	
			else
				__this.root.hideSubMenu();	
		}
		if(__this.onclick){
			__this.onclick(event);
			browser.sendMessage(null, 201, event, window);//WM_ONMOUSEDOWN(WM_LBUTTONDOWN):201
		}
		return js.ui.Component.eventCancelBubble(event);
	}
}

js.ui.MenuItem.prototype.setIcon = function(url, w, h){
	if(!this.ui) this.getUI();
	if(!this.icon){
		this.icon = new js.ui.Img();
		this.iconCell.appendChild(this.icon.getUI());
	}
	this.icon.setSrc(url, w, h);
}
	
js.ui.MenuItem.prototype.setRightIcon = function(url, w, h){
	if(!this.ui) this.getUI();
	if(!this.rightIcon){
		this.rightIcon = new js.ui.Img();
		this.rightCell.appendChild(this.rightIcon.getUI());
	}
	this.rightIcon.setSrc(url, w, h);
}

js.ui.MenuItem.prototype.setText = function(lable){
	if(!this.ui) this.getUI();
	this.text.innerHTML = lable;						
}
/**
 * Add submenu to the item 
 * @param  {menuitem} 
 */
js.ui.MenuItem.prototype.setSubMenu = function(menu){
	document.body.appendChild(menu.getUI());
	this.subMenu = menu;
	this.subMenu.root = this.root;
	this.subMenu.menu = this.menu;
	this.subMenu.hide();
	menu.owner = this;
	if(!this.__isMenuBarItem){
		var url = imagePath+'menu/rightArrow.gif';
		this.setRightIcon(url);
	}
}
js.ui.MenuItem.prototype.subMenuClass = null;

js.ui.MenuItem.prototype.createSubMenu = function(){
	if(!this.subMenuClass)
		this.subMenuClass = js.ui.Menu;
	this.subMenu = new this.subMenuClass();
	this.setSubMenu(this.subMenu);
	return this.subMenu;
}

js.ui.MenuItem.prototype.showSubMenu = function (){
	if(!this.hasSubMenu()) return;
	this.subMenu.getUI().style.zIndex = this.getOffsetZIndex()+1;
	this.subMenu.ui.style.position = "absolute";
	this.subMenu.show();
    var top;
	if(this.__isMenuBarItem){
		top = this.getOffsetTop()+this.ui.offsetHeight;
		this.subMenu.getUI().style.left = this.getOffsetLeft();
	}
	else{
		this.subMenu.getUI().style.left = this.getOffsetLeft()+this.ui.offsetWidth;
		top = this.getOffsetTop();
		var maxbottom = this.menu.getOffsetTop()+this.menu.ui.offsetHeight;
		if(top>maxbottom-this.subMenu.getUI().offsetHeight){
			top = maxbottom-this.subMenu.getUI().offsetHeight;
		}
	}
	this.subMenu.getUI().style.top = top;
	this.subMenu.popup(this.subMenu.ui.style.left, top);
	this.menu.__isPopuped = true;
}

js.ui.MenuItem.prototype.hideSubMenu = function (except){
	if(this.hasSubMenu()){
		this.subMenu.hideSubMenu(except);
		this.subMenu.setVisible(false);
	}
	this.menu.__isPopuped = false;
}
js.ui.MenuItem.prototype.setSelected = function (selected){
	this.selected = selected;
	if(selected){
		this.className = "js_ui_Style_Selected js_ui_MenuItem js_ui_MenuItem_Selected";
	}
	else{
		this.className = "js_ui_MenuItem";
	}
}
/**
 * set menuitem enabled
 * @param {boolean} enabled
 */
js.ui.MenuItem.prototype.setEnabled = function (enabled){
	if(enabled) this.enabled = enabled;
	this.disabled = !enabled;
	if(browser.isMoz){
		if(!enabled){
			this.setOpacity(30);
			if(this.onclick && !this.__oldOnClick) 
				this.__oldOnClick = this.onclick;
			this.onmouseup = js.ui.Component.eventCancelBubble;
		}
		else{
			this.setOpacity(null);
			if(this.__oldOnClick)
				this.onclick = this.__oldOnClick;
			this.__oldOnClick = null;
			this.onmouseup = js.ui.MenuItem.prototype.__MenuItem_onclick;
		}
	}

}
/**
 * Checks whether this menu item is enabled
 * @return {bollen}
 */
js.ui.MenuItem.prototype.isEnabled = function (){
	return this.enabled;
}
/**
 * Checks whether this menu item is enabled
 * @return {bollen}
 */
js.ui.MenuItem.prototype.hasSubMenu = function (){
	if (this.subMenu) return true;
	else return false;
}
js.ui.MenuItem.prototype.hasPopupMenu = function (){
	if(this.popupMenu) return true;
	else return false;
}
js.ui.MenuItem.prototype.__MenuItem_onclick = function (event){
	if(!event)event=window.event;
	if(!this.hasSubMenu()){
		if (this.menu && !this.__isMenuBarItem){
			this.menu.Hide();
		}
		if(!this.root.isMenuBar)
			this.root.hide();	
		else
			this.root.hideSubMenu();	
	}
	js.ui.Component.eventCancelBubble(event);
	if(this.onclick){
		this.onclick(event);
		browser.sendMessage(null, 201, event, window);//WM_ONMOUSEDOWN(WM_LBUTTONDOWN):201
	}
}
function  MenuItem_onmouseover(event){
	this.setSelected(true);
	if(this.__isMenuBarItem && !this.menu.__isPopuped) return;
	if(this.hasSubMenu() && this.subMenu.visible) return;
	if(this.menu){
		if(this.menu.hideSubMenu){
			this.menu.hideSubMenu(this.menu);
		}
	}
	this.showSubMenu();
	this.setSelected(true);
	return true;
	
}

function MenuItem_onmouseout(){
	if(!this.hasSubMenu()) 
		this.setSelected(false);
	else{
		if(!this.subMenu.visible)
			this.setSelected(false);
	}
}

/**
 * Construct js.ui.Menu inherits from js.dhtml.Table.
 * @param  {js.ui.Container} parent container of the instance
 * @class js.ui.Menu is the menu class.  
 * @see Component js.dhtml.Table is the base class for this
 */
js.ui.Menu = function (){
	this.items = new js.lang.Array();
}

js.ui.Menu.prototype = new js.ui.Container();

js.ui.Menu.prototype.getNativeClass = function (){
	return "js.ui.Menu";
}

js.ui.Menu.prototype.bindUI = function (){
	this.container = this.ui.getElementsByTagName("table")[0]; 
}
js.ui.Menu.prototype.addSeparator = function (){
	var sep = new js.ui.MenuSeparator();
	this.getContainer().appendChild(sep.getUI());
	return sep;
}

js.ui.Menu.prototype.insert = function (menuitem, beforeitem){
	this.add(menuitem, beforeitem);
}

js.ui.Menu.prototype.add = js.ui.Menu.prototype.addItem = function (menuitem, beforeitem){
	if(beforeitem){
		this.getContainer().insertBefore(menuitem.getUI(), beforeitem.getUI());
//		this.items.InsertItem(menuitem, this.Items.IndexOf(beforeitem));
	}else{
		this.getContainer().appendChild(menuitem.getUI());
		this.items.addItem(menuitem);
	}
	menuitem.menu = this;
	menuitem.__isMenuBarItem = false;
//	menuitem.MenuBar = this.MenuBar;
	if(this.root)
		menuitem.root = this.root;
	else
		menuitem.root = this;
/*	if(this.autoPopup){
		menuitem.ui.onmousedown = menuitem.ui.onclick;
		menuitem.ui.onclick = null;
	}*/

}
/**
 * Removes the specified MenuItem from the menu item collection.
 * @param  {menuitem} 
 */
 /**
 * Removes the specified MenuItem from the menu item collection as index
 * @param  {int} 
 */
js.ui.Menu.prototype.remove = function (indexOrobj){
	if(typeof indexOrobj == "object"){
		if(indexOrobj.Hide) indexOrobj.Hide();
		if(this.removeChild)
			this.tbody.removeChild(indexOrobj);
		this.Items.RemoveItem(indexOrobj);
	}
	else if(typeof indexOrobj == "number"){
		var obj = this.Items[indexOrobj];
		if(!obj){
			alert("out of index");
			return;
		}
		if(obj.Hide) obj.Hide();
		if(this.removeChild)
			this.tbody.removeChild(obj);
		this.Items.RemoveItem(obj);
	}	
}
/**
 * Removes the specified MenuItem from the menu item collection.
 */
 js.ui.Menu.prototype.removeAll = function (){

//	 this.tbody.removeChild(this.tbody.childNodes);
//	 this.Items.RemoveAll();
	var num = this.Items.length;
	for(var i=0;i<num;i++){
		this.Remove(0);
		}
	this.RemoveSubMenu();
}

/**
 * Determines if the specified MenuItem is a member of the collection.
 * @param  {menuitem} 
 */
js.ui.Menu.prototype.contains = function (menuitem){
	var value =false;
	for (var i=0;i<this.Items.length;i++ ){
		var currentItem = this.Items[i];
		if(currentItem == menuitem) value = true;
	}
}
/**
 * Retrieves the first index of a specific MenuItem in the collection.
 * @param  {menuitem} 
 */
js.ui.Menu.prototype.indexOf = function (menuitem){
	for (var i=0;i<this.items.length;i++ ){
		var currentItem = this.items[i];
		if(currentItem == menuitem) return i;
	}
	return null;
}
/**
 * Gets the MenuItem located at the specified index of this menu
 * @param  {int} 
 */
js.ui.Menu.prototype.getItem = function (index){
	return this.Items.GetItem(index);
}
/**
 * Get the number of item collection of this menu.
 * @param  {menuitem} 
 */
js.ui.Menu.prototype.getItemCount = function (){
	return this.Items.GetLength();
}
/**
 * Returns a MenuItem in the menu. Returns NULL if no such MenuItem exists.
 * @param  {string} 
 */
js.ui.Menu.prototype.getItemByName  = function (name){
	
}
/**
 * Replaces the first instance of the first MenuItem parameter with the second MenuItem parameter.
 * @param  {menuitem}
 * @param  {menuitem}
 */
js.ui.Menu.prototype.replace = function (olditem,newitem){
	var index  = this.IndexOf(olditem);
	this.Remove(olditem);
	if (index)	this.Add(newitem,index);
}
js.ui.Menu.prototype.hide = function (){
	this.__superHide = js.ui.Component.prototype.hide;
	this.__superHide();
	this.hideSubMenu();
	if(this.root)
		this.root.__isPopuped = false;
	if(this.owner)
		if(this.owner.menu)
			this.owner.menu.autoPopup = false;
}
js.ui.Menu.prototype.hideSubMenu = function (except){
	var menuitem, submenu, isvisible;
	for(var i=0; i<this.items.length; i++){
		menuitem = this.items.getItem(i);
		if(menuitem.hasSubMenu()){
			if(except)
				isvisible = this.isExistSubMenu(except);
			else
				isvisible = false;
			if(!isvisible){
				menuitem.subMenu.hide();
				menuitem.setSelected(false);
				menuitem.subMenu.hideSubMenu(except);
			}
		}
	}
}

js.ui.Menu.prototype.isExistSubMenu = function (menu){
	var result;
	for(var i=0; i<this.items.length; i++){
		menuitem = this.items.getItem(i);
		if(menuitem.subMenu==menu){
			return true;
		}
		if(menuitem.hasSubMenu()){
			result = menuitem.subMenu.isExistSubMenu(menu);
			if(result)
				return true;
		}
	}
	return false;
}

js.ui.Menu.prototype.removeSubMenu = function (except){
	var menuitem, submenu;
	for(var i=0; i<this.Items.length; i++){
		menuitem = this.Items.GetItem(i);
		if(menuitem.HasSubMenu() &&  menuitem.SubMenu!=except ){
			menuitem.SubMenu.RemoveAll();
			//menuitem.SetSelected(false);
			menuitem.SubMenu.RemoveSubMenu(except);
		}
	}
}
js.ui.Menu.prototype.popup = function (x, y, zIndex) {
	browser.sendMessage(null, 201, null, this);//WM_ONMOUSEDOWN(WM_LBUTTONDOWN):201
	if(!zIndex) zIndex = 999;
	this.ui.style.zIndex = zIndex;
	this.ui.style.position = "absolute";
	if(x)
		this.ui.style.left = x + "px";
	if(y)
		this.ui.style.top  = y + "px";
	this.show();
	if(browser.isIE){
		if(document.onclick!=js.ui.Menu.__windowonmousedown){
			document.__oldonmousedown = document.onclick;
			document.onclick = js.ui.Menu.__windowonmousedown;
		}
	}
	else{
		if(window.onclick!=js.ui.Menu.__windowonmousedown){
			window.__oldonmousedown= window.onclick;
			window.onclick = js.ui.Menu.__windowonmousedown;
		}
	}
	this.addListener(201);
	if(this.onpopup) this.onpopup(x, y);
}
js.ui.Menu.__windowonmousedown = function(event){
	if(!event)event = window.event;
	browser.sendMessage(null, 201, event, window);//WM_ONMOUSEDOWN(WM_LBUTTONDOWN):201
}

js.ui.Menu.prototype.processMessage = function (msg, params, src){
	switch (msg){
		case 201://WM_ONMOUSEDOWN(WM_LBUTTONDOWN):201
			if(browser.isIE){
				if(document.__oldonmousedown)
					document.onmousedown = document.__Oldonmousedown;
			}
			else
				window.onmousedown = window.__oldonmousedown;
			this.hide();
			break;
	}
}

js.ui.PopupMenu = function (){
	this.items = new js.lang.Array();
}

js.ui.PopupMenu.prototype = new js.ui.Menu();

js.ui.PopupMenu.prototype.autoPopup = true;

js.ui.MenuBar = function (parent){
	if(!parent)
		parent = GetDocument().body;
	var obj = new js.ui.Menu(parent);
	obj.MenuBarRow = obj.AddRow(obj.Tbody);
	obj.SetVisible(true);
	obj.Items = new js.lang.Array();
	obj.AutoPopup = false;
	obj.RemoveListener(201);
	obj.IsMenuBar = true;
	obj.className = "js_ui_MenuBar";
	return obj;
}

js.ui.MenuBar.prototype.add = function (menuitem){
	var cell  = this.AddCell(this.MenuBarRow);
	cell.style.borderwidth='0';
	cell.style.border='0';
	var table = new js.dhtml.Table(cell);
	table.style.borderwidth='0';
	table.cellSpacing = "0";
	table.cellPadding = "0";
	table.style.border='0';
	table.style.height = "100%";
	table.style.paddingLeft=5;
	table.tbody.appendChild(menuitem);
//	menuitem.MenuBar = this;
	menuitem.Menu = this;
	menuitem.__IsMenuBarItem = true;
//	menuitem.onmouseover = MenuBar_MenuItem_OnMouseOver;
//	menuitem.onmouseout = MenuBar_MenuItem_OnMouseOut;
//	menuitem.onclick = MenuBar_MenuItem_OnClick;

	menuitem.Text.style.paddingLeft=2;
	menuitem.Text.style.paddingRight=2;

	//menuitem.onclick =null;
	this.Items.AddItem(menuitem);

	if(this.Root)
		menuitem.Root = this.Root;
	else
		menuitem.Root = this;
}

js.ui.MenuItem.prototype.popup = function (x,y){
	this.style.zIndex = 999;
	if(x)
		this.style.left = x;
	if(y)
		this.style.top  = y;
	this.PopMenu.Show();

	if(this.PopMenu.OnPopup)this.PopMenu.OnPopup(x, y);

}
js.ui.MenuItem.prototype.getPopLocation = function (x,y){
	var point = new js.ui.Point();
	point.X = this.GetOffsetLeft();
	point.Y = this.GetOffsetTop()+this.offsetHeight;
	return point;
}

js.ui.MenuItem.prototype.hidePopupMenu = function (){
	if(this.HasPopupMenu()){
		this.PopupMenu.Hide();
	}
	this.Menu.AutoPopup = false;
}
js.ui.MenuItem.prototype.showPopupMenu = function (){
	if(!this.HasPopupMenu()) return;
	var point = this.GetPopLocation();
//	this.PopupMenu.SetLocation(point.X,point.Y);
//	this.PopupMenu.SetVisible(true);
	this.PopupMenu.Owner = this;
	this.PopupMenu.Popup(point.X, point.Y);
	this.Menu.AutoPopup = true;
}
js.ui.MenuItem.prototype.add = function (menuitem){
	if(!this.subMenu)
		this.createSubMenu();
	this.subMenu.addItem(menuitem);
}
js.ui.MenuItem.prototype.addSeparator = function (){
	if(!this.subMenu)
		this.createSubMenu();
	this.subMenu.addSeparator();
}
js.ui.MenuBar.prototype.hidePopupMenu = function (item){

	if(item) var item1 = item;
	else var item1 =null;
	for(var i = 0;i<this.Items.length;i++){
		if(this.Items[i]!= item1){
			this.Items[i].HidePopupMenu();		
		}
		else item1.ShowPopupMenu();
	}
	if(item1.PopupMenu) item1.Menu.AutoPopup = true;
}
function MenuBar_MenuItem_OnMouseOver(){
	this.SetSelected(true);
	if(this.HasPopupMenu() && this.PopupMenu.Visible) return;
	if(this.HasPopupMenu() && this.Menu.AutoPopup)
			this.Menu.HidePopupMenu(this);
	
	return true;
	
}
function MenuBar_MenuItem_OnClick(){
    if(this.HasPopupMenu() && (!this.PopupMenu.Visible)){
		this.ShowPopupMenu();
	}
	else if(this.HasPopupMenu() && this.PopupMenu.Visible){
		this.HidePopupMenu();
	}

}
function MenuBar_MenuItem_OnMouseOut(){
	if(this.HasPopupMenu()) this.SetSelected(false);


}
