if (typeof ZebuVWTLocation == "undefined") 
	var ZebuVWTLocation = "js/";

Menu.barrowImage = new Image();
Menu.barrowImage.src = ZebuVWTLocation+"barrow.png";
Menu.warrowImage = new Image();
Menu.warrowImage.src = ZebuVWTLocation+"warrow.png";

Menu.idCounter = 0;
// Menu.normalBgColor		= "#DCDCDC";
// Menu.highlightBgColor	= "#F2F2F2";

function MenuItem(x, y, html, submenu, action, parentMenu, id) { 
    this.id = (id ? id : this.type + Menu.idCounter++);

    this.parentMenu = parentMenu;
    this.action    = action;
	this.submenu   = submenu;
	this.html      = html;
	this.normalBgColor = Menu.normalBgColor;
	this.highlightBgColor = Menu.highlightBgColor;

	var cSize = document.zbComputeSize(html);
	var width, height;
	if (parentMenu.direction == "horizontal"){
		width = cSize.width + 10;
		height = cSize.height;
	}
	else {
		// width = cSize.width + 7 + 7 + 8;
		// height = cSize.height + 4;
		width = cSize.width + 7 + 7 + 18;
		height = cSize.height + 10;
	}
	this.size = new Size( width, height );

    this.mainWindow = document.zbAppendLayer(this.id, width, height, parentMenu.mainWindow.client);
    this.mainWindow.zbSetLocation(x, y);
	this.mainWindow.zbSetBackgroundColor(this.normalBgColor);

    this.itemWindow = document.zbAppendLayer(this.id+"item", cSize.width, cSize.height, this.mainWindow);
	if (parentMenu.direction == "horizontal")
	    this.itemWindow.zbSetLocation(5, 0);
	else    
    	this.itemWindow.zbSetLocation(4, 2);
    this.itemWindow.zbSetContent(html);

	if (parentMenu.direction == "vertical"){
		var arrow = "";
	    if (this.submenu)
			arrow = "<img name='"+this.id+"arrow"+"' width='7' height='7' src='"+Menu.barrowImage.src+"'>";

	    this.arrowWindow = document.zbAppendLayer(this.id+"arrow", 7, 7, this.mainWindow);
	    this.arrowWindow.zbSetLocation(width-15, (height-7)/2);
	    this.arrowWindow.zbSetContent(arrow);
	}

	this.setSize = MenuItem_setSize;
	this.showSubMenu = MenuItem_showSubMenu;
	this.activate = MenuItem_activate;
	this.deactivate = MenuItem_deactivate;

	this.mainWindow.zbAddEventListener("mouseover", MenuItem_mouseEntered, false);
	this.mainWindow.zbAddEventListener("mouseout", MenuItem_mouseExited, false);
	this.mainWindow.zbAddEventListener("mouseup", MenuItem_mouseReleased, false);

	this.mainWindow.owner = this;

	return this; 
} 

MenuItem.prototype.type = "MenuItem";

function MenuItem_showSubMenu(){
	if (this.submenu == null)
		return;
	
	var size = this.mainWindow.zbGetSize();
	var loc  = this.mainWindow.zbGetLocationOnPage();

	if (this.parentMenu.direction == "horizontal")
	    this.submenu.mainWindow.zbSetLocation(loc.x, loc.y+size.height-2);
	 else
    	this.submenu.mainWindow.zbSetLocation(loc.x+size.width-2, loc.y);

	if (this.submenu.mainWindow.zbGetZIndex() <= this.parentMenu.mainWindow.zbGetZIndex())
		this.submenu.mainWindow.zbSetZIndex(this.parentMenu.mainWindow.zbGetZIndex()+1);

    this.submenu.mainWindow.zbSetVisibility("visible");
}

function MenuItem_activate(){
	this.mainWindow.zbSetBackgroundColor(this.highlightBgColor);
   	if (this.submenu){
   		if (this.arrowWindow){
	  		var imgCollect = document.zbGetImages(this.arrowWindow);
	  		imgCollect[this.id+"arrow"].src = Menu.warrowImage.src;
  		}
   		this.showSubMenu();
    }
}	

function MenuItem_deactivate(){
	this.mainWindow.zbSetBackgroundColor(this.normalBgColor);
    if (this.submenu){
   		if (this.arrowWindow){
	  		var imgCollect = document.zbGetImages(this.arrowWindow);
			imgCollect[this.id+"arrow"].src = Menu.barrowImage.src;
		}
		this.submenu.hideMenu();
	}
}

function MenuItem_setSize(width, height){
	if (this.parentMenu.direction == "horizontal")
		return;
	this.mainWindow.zbSetSize(width, height);
	this.itemWindow.zbSetSize(width-20, height-4);
	if (this.arrowWindow){
		var	loc = this.arrowWindow.zbGetLocation();
		var	arrowSize = this.arrowWindow.zbGetSize();
		this.arrowWindow.zbSetLocation(width-11, (height-arrowSize.height)/2);
	}		
}	

function MenuItem_mouseEntered(mouseEvent) {
	// prevent refresh
	if (this.owner.parentMenu.activeItem == this.owner)
		return;
	
	if (this.owner.parentMenu.activeItem)
		this.owner.parentMenu.activeItem.deactivate();
	this.owner.parentMenu.activeItem = this.owner;
	this.owner.activate();
}

function MenuItem_mouseExited(mouseEvent) {
	var menu = this.owner.parentMenu;
	while(menu.topMenu) menu = menu.topMenu;
	if (!menu.insideMenu(mouseEvent.pageX, mouseEvent.pageY)){
		if (menu.activeItem){
			menu.activeItem.deactivate();
			menu.activeItem = null;
		}
	}	
}

function MenuItem_mouseReleased(mouseEvent) {
	var menu = this.owner.parentMenu;

	/* Find the topmost menu */
	while(menu.topMenu) menu = menu.topMenu;

	menu.activeItem.deactivate();
	menu.activeItem = null;

	if (this.owner.action)
		this.owner.action();
}


function Menu(x, y, direction, parentLayer, id) { 
    this.id = (id ? id : this.type + Menu.idCounter++);
    this.direction = (direction ? direction : "vertical");
	this.normalBgColor = Menu.normalBgColor;
	this.topMenu = null;

    this.mainWindow = document.zbAppendLayer(this.id, 0, 0, parentLayer);
   
    if (x == null || y == null)
     	this.mainWindow.zbSetVisibility("hidden");
	else     	
	    this.mainWindow.zbSetLocation(x, y);
	    
	this.mainWindow.zbSetBackgroundColor(this.normalBgColor);
    ZebuDOM.turnIntoZebuBorderElement(this.mainWindow, 1, BORDER_PLAIN);
	
	this.items = new Array();
	this.activeItem = null;
	this.closed     = false;
		
    this.setLocation = Menu_setLocation;
    this.addItem     = Menu_addItem;
    this.hideMenu    = Menu_hideMenu;
	this.insideMenu  = Menu_insideMenu;
    this.show		 = Menu_show;
    this.hide		 = Menu_hide;
    this.close		 = Menu_close;

	return this; 
} 

Menu.prototype.type = "Menu";

function Menu_setLocation(x, y){
	if (this.closed) return;
	this.mainWindow.zbSetLocation(x, y);
}

function Menu_addItem(html, action, submenu, id){
	var s = this.mainWindow.client.zbGetSize();
	var x = (this.direction == "horizontal" ? s.width : 0); 		       
	var y = (this.direction == "horizontal" ? 0 : s.height); 		       
    var item = new MenuItem(
                    x, y, 
                    html, submenu, action, this, id);

	this.items[this.items.length] = item;

	if (submenu){
    	submenu.mainWindow.zbSetVisibility("hidden");
		submenu.topMenu = this;
	}

   	var size = item.mainWindow.zbGetSize();
   	
   	if (this.direction == "horizontal"){
		s.width += size.width;
		if (size.height > s.height)
			s.height = size.height;
	}
	else{
	   	s.height += size.height;
		if (size.width > s.width)
			s.width = size.width;
	
	    for (var i=0; i<this.items.length; i++) {
	    	var size = this.items[i].mainWindow.zbGetSize();
	    	this.items[i].setSize(s.width, size.height);
	    }
    }
    var border = this.mainWindow.borderWidth*2;
    this.mainWindow.zbSetSize(s.width+border, s.height+border);
	
    return item;
}


function Menu_hideMenu(){
	if (this.activeItem){
		this.activeItem.mainWindow.zbSetBackgroundColor(this.activeItem.normalBgColor);
	    if (this.activeItem.submenu){
	    	if (this.activeItem.arrowWindow){
		  		var imgCollect = document.zbGetImages(this.activeItem.arrowWindow);
				imgCollect[this.activeItem.id+"arrow"].src = Menu.barrowImage.src;
	    	}
	        this.activeItem.submenu.hideMenu();
		}
		this.activeItem = null;
	}
	
    this.mainWindow.zbSetVisibility("hidden");
}


function Menu_insideMenu(x, y){
	var point = this.mainWindow.client.zbGetLocationOnPage();
	var size = this.mainWindow.client.zbGetSize();	
	var rect = new Rectangle(point.x, point.y, size.width, size.height);
	if (rect.contains(x, y))
		return true;

	if (this.activeItem && this.activeItem.submenu &&
		(this.activeItem.submenu.mainWindow.zbGetVisibility() == "visible") &&
		(this.activeItem.submenu.insideMenu(x, y) == true))
		return true;

	return false;	
}


function Menu_show(){
	if (this.closed) return;
    this.mainWindow.zbSetVisibility("visible");
}

function Menu_hide(){
	if (this.closed) return;
    this.mainWindow.zbSetVisibility("hidden");
}

function Menu_close() {
	if (this.closed) return;
    document.zbRemoveLayer(this.mainWindow);
    this.closed = true;
}

