

function CMenuItem(menuText, objectKey, subMenu, selected)
{
	this.menuText = menuText;
	this.objectKey = objectKey;
	this.subMenu = subMenu;
	this.selected = selected;
}

var CMenu_ORIENTATION_H = 0;
var CMenu_ORIENTATION_V = 1;

function CMenu (ID, containerObject, internalPadding, internalSpacing, mainBorder)
{
	this.items = [];
	this.ContainerObject = containerObject;
	this.ID = ID;
	this.internalPadding = (internalPadding == null ? 0 : internalPadding);
	this.internalSpacing = (internalSpacing == null ? 0 : internalSpacing);
	this.mainBorder = (mainBorder == null ? 0 : mainBorder);
	// Se il div e' passato imposta il flag che indica che il DIV
	// e' esterno e non va ne' creato ne' distrutto.
	this.OwnsDiv = (containerObject == null);
	this.rootMenu = null;
}


CMenu.prototype.getItemStyle 
= // =================================
function (itemIndex, over)
{
	var menuitem = this.items[itemIndex];
	if (menuitem.selected == true)
	{
		if (over) 
			return this.MenuSelectedOverItemStyle;
		else
			return this.MenuSelectedItemStyle;
	}
	else
	{
		if (over) 
			return this.MenuOverItemStyle;
		else
			return this.MenuItemStyle;
	}
}



CMenu.prototype.GetRootMenu 
= // ============================
function()
{
	return (this.rootMenu == null ? this : this.rootMenu);
}
 

CMenu.prototype.OnMenuMouseEnter = null; // handler per il lancio del submenu all' ingresso nella regione dell' item
CMenu.prototype.OnMenuMouseExit = null; // handler per il lancio del submenu all' uscita nella regione dell' item
CMenu.prototype.OnMenuClick = null; // handler per il lancio del submenu.
CMenu.prototype.ParentMenu = null; // riferimento al menu genitore
CMenu.prototype.Orientation = CMenu_ORIENTATION_V;

CMenu.prototype.MenuItemStyle = "cls-MenuDefaultItemStyle";
CMenu.prototype.MenuOverItemStyle = "cls-MenuDefaultItemStyle";

CMenu.prototype.MenuSelectedItemStyle = "cls-MenuDefaultItemStyle";
CMenu.prototype.MenuSelectedOverItemStyle = "cls-MenuDefaultItemStyle";

CMenu.prototype.MenuBodyStyle = "cls-MenuDefaultBodyStyle";

CMenu.prototype.HideMenuDiv =
function ()
{
	if (this.OwnsDiv == false)
	return;
	this.ContainerObject.style.display = "none";
}

 

CMenu.prototype.ShowMenuDiv =
function ()
{
	if (this.OwnsDiv == false)
	return;
	this.ContainerObject.style.display = "block";
}

 

CMenu.prototype.AddItem =
function (Text, ObjectKey)
{
            // 0-> testo
            // 1-> ItemId
            // 2-> SubMenu eventuale collegato

            this.items[this.items.length] = new CMenuItem(Text, ObjectKey, null, false);  // $$ [Text, ItemId, null];
            return true;

}

 

CMenu.prototype.RemoveItem = 
function(ObjectKey)
{
            for (var item = 0; item < this.items.length; item++)
            {
                        if (this.items[item].ObjectKey == ObjectKey)
                        {
                                   this.items.splice(item,1);
                                   return true;
                        }
            }
            return false;
}

CMenu.prototype.ClearItems =
function ()
{
	this.items = [];
}

CMenu.prototype.internalShowMenu =
function (oMenu, left, top)
{
            var menuContainer = this.getDIVContainer();
            oMenu.ContainerObject = menuContainer;
            menuContainer.innerHTML = "";
            oMenu.RenderMenu();
            menuContainer.style.left = left;
            menuContainer.style.top = top;
            menuContainer.style.zIndex = "100";
            this.ShowMenuDiv();
}

CMenu.prototype.LinkMenu = 
function (index, menu)
{
	if ((index < 0) || index >= this.items.length)
		return false;
	this.items[index].subMenu = menu;
	menu.OnMenuClick = this.OnMenuClick;
	menu.ParentMenu = this;
	menu.rootMenu = this.GetRootMenu();
}

 

CMenu.prototype.getXYToParent =
function (childObject, ParentObject)
{
	var curobj = childObject;
	var totleft=0, tottop=0;
	while ( (curobj != null) && (curobj != ParentObject))
	{
					totleft += curobj.offsetLeft;
					tottop += curobj.offsetTop;
					curobj = curobj.offsetParent;
	}
	var result = new Object;
	result.left = totleft;
	result.top = tottop;
	return result;
}


// Annulla un evento generico.

CMenu.prototype.stopEvent 
= // ================================
function (ev)
{
	if (ev == null) 
	{
		window.event.cancelBubble = true;	// IE
	}
	else
	{
		ev.stopPropagation();					// Mozilla / Firefox / Netscape
	}
}


CMenu.prototype.SetSelected 
= // =============================
function(itemIndex, selected)
{

	this.items[itemIndex].selected = selected;
	if (this.items[itemIndex].menuCell != null)
		this.items[itemIndex].menuCell.className = this.getItemStyle( itemIndex, false );  // this.MenuItemStyle;
}


CMenu.prototype.IsSelected 
= // =============================
function(itemIndex)
{
	return this.items[itemIndex].selected;
}




CMenu.prototype.MenuClick =
function (ev)
{
            var Result = false;
            var objRef = this.objRef;

            if (this.menuRef.OnMenuClick != null) Result = this.menuRef.OnMenuClick(this.menuRef, objRef.ItemIndex);
            if (Result == true) 
            {
            	this.menuRef.stopEvent(ev);
            	return false;
            }
			
        	var position = this.menuRef.getXYToParent(this, null);
        	

			// elimina eventuali sub menu aperti
			this.menuRef.CloseDescendant();
			this.menuRef.childMenu = null;
			
            if (objRef.subMenu != null) // c'e' un submenu' collegato?
            {
						// launch menu.
						theMenu = objRef.subMenu;

						switch (this.menuRef.Orientation)
						{
							case CMenu_ORIENTATION_H:
								position.top += this.offsetHeight;
								break;
							case CMenu_ORIENTATION_V:
								position.left += this.offsetWidth;
								break;
						}
						this.menuRef.internalShowMenu(theMenu, position.left, position.top );
						theMenu.ContainerObject.style.display ="block";
						this.menuRef.childMenu = theMenu;
            }
            else
            {
            	
            	if (this.menuRef.ParentMenu != null)
            	{
            		this.menuRef.GetRootMenu().CloseDescendant();
            	}
            }

	
		this.menuRef.stopEvent(ev);
            
      return false;

}



CMenu.prototype.MenuMouseEnter =
function ()
{
	var Result = false;
	var objRef = this.objRef;

	if (this.menuRef.OnMenuMouseEnter != null) Result = this.menuRef.OnMenuMouseEnter(this.menuRef, objRef.ItemIndex);
	if (Result == true) return;
	var menu = this.menuRef;
	this.className = menu.getItemStyle( objRef.ItemIndex, true ); // menu.MenuOverItemStyle;
	
}


CMenu.prototype.MenuMouseExit =
function ()
{
	var Result = false;
	var objRef = this.objRef;


	if (this.menuRef.OnMenuMouseExit != null) Result = this.menuRef.OnMenuMouseExit(this.menuRef, objRef.ItemIndex);
	if (Result == true) return;
	
	var menu = this.menuRef;
	this.className = menu.getItemStyle( objRef.ItemIndex, false );  //menu.MenuItemStyle;

	
}



CMenu.prototype.CloseDescendant =
function ()
{
	if (this.childMenu != null)
	{
		this.childMenu.CloseDescendant();
		this.childMenu.HideMenuDiv();
		this.childMenu = null;
	}
}

 

CMenu.prototype.AddRows = 
function (oTable)
{
	for (var i = 0; i < this.items.length; i++)
	{
		// aggiungiamo la riga della tabella HTML. (per ogni menu')
		var oTableRow = oTable.insertRow(-1);
		// aggiungiamo le celle e impostiamo la parte di InnerHtml con cio' che e' stato passato.
		var oTableCell = oTableRow.insertCell(0);
		oTableCell.className = this.getItemStyle( i, false );  // this.MenuItemStyle;
		oTableCell.innerHTML = this.items[i].menuText;
		oTableCell.objRef = this.items[i];
		oTableCell.objRef.ItemIndex = i;
		oTableCell.menuRef = this;
		oTableCell.onclick = this.MenuClick;
		oTableCell.onmouseout= this.MenuMouseExit;
		oTableCell.onmouseover= this.MenuMouseEnter;
		this.items[i].menuCell = oTableCell;
	}
}

CMenu.prototype.AddCells = 
function (oTable)
{
	// aggiungiamo la riga della tabella HTML.
	var oTableRow = oTable.insertRow(0);
	// aggiungiamo le celle e impostiamo la parte di InnerHtml con cio' che e' stato passato.
	for (var i = 0; i < this.items.length; i++)
	{
		var oTableCell = oTableRow.insertCell(i);
		oTableCell.className = this.getItemStyle( i, false );  // this.MenuItemStyle;
		oTableCell.innerHTML = this.items[i].menuText;
		oTableCell.objRef = this.items[i];
		oTableCell.objRef.ItemIndex = i;
		oTableCell.menuRef = this;
		oTableCell.onclick = this.MenuClick;
		oTableCell.onmouseout= this.MenuMouseExit;
		oTableCell.onmouseover= this.MenuMouseEnter;
		this.items[i].menuCell = oTableCell;
	}
}

 

CMenu.prototype.createMenuBody = 
function(id, menuOrientation)
{
	var id = 'TableMenu_' + id;
	var element;
	if ((element = document.getElementById(id)) != null)
	{
		this.ContainerObject.removeChild(element);
	}
	var oTable = document.createElement('TABLE');
	oTable.cellSpacing = this.internalSpacing;
	oTable.cellPadding = this.internalPadding;
	oTable.border=this.mainBorder;
	oTable.className = this.MenuBodyStyle;
	if (menuOrientation == CMenu_ORIENTATION_H)
		this.AddCells(oTable);
	else
		this.AddRows(oTable);

	oTable.id = id;
	return oTable;
}

 

CMenu.prototype.RenderMenu = 
function ()
{
	oTable = this.createMenuBody(this.ID, this.Orientation);
	this.ContainerObject.appendChild(oTable);
}

 

CMenu.prototype.getDIVContainer = 
function()
{
	if (this.DIVContainer == null)
	{
		this.DIVContainer = document.createElement('DIV');
		this.ContainerObject.parentNode.appendChild(this.DIVContainer);
		this.DIVContainer.id = 'DIV_Container' + this.ID;
		this.DIVContainer.style.position = 'absolute';
		this.DIVContainer.overflow = 'visible';
	}
	return this.DIVContainer; 
}
	