/*******************************************************************************
*
*	SubMenu.js
*
*******************************************************************************/

/*******************************************************************************
* CONSTRUCTOR
*******************************************************************************/
function SubMenu(args)
{
	/* Empty Constuctor Helper */
	if (! args)
	{
		args = [];
	}
	
	/* Need this construct because of JavaScript inheritance problems */
	if (args[ConstructorHelper] == ConstructorHelper)
	{
		return;
	}
	
	this._init(args);

}

/*******************************************************************************
*	INIT
*******************************************************************************/
SubMenu.prototype._init = function(args)
{
	// holds references to all menuitems of this collection
	this._menuItems = new Array();
	
	// holds a reference to the menuitem which opened this submenu
	this._parentMenuItem = undefined;
	
	this._node = undefined;
	this._direction = args['DIRECTION'] || 'VERTICAL';

	// Create DOM elements
	//this._createNode();
	
	// Hide this by default
	//this._hide();
	//this._show();
}

/*******************************************************************************
* PUBLIC METHODS
*******************************************************************************/

	/*****************************************************************************
	*
	*	addMenuItem(MenuItem) : void
	*
	*****************************************************************************/
	SubMenu.prototype.addMenuItem = function(menuItem)
	{
		// Reference to all menu items
		this._menuItems.push(menuItem);
		
		// Cross reference
		menuItem._parentMenu = this;
		
		if (this._direction == 'HORIZONTAL')
		{
			// CHANGE
			//this._getContentNode().appendChild(menuItem._node);
		}
		else
		{
			// CHANGE
			//var tr = createBitbaseNodeElement('TR');
			//tr.appendChild(menuItem._node);
			//this._getContentNode().appendChild(tr);
		}
	}

	/*****************************************************************************
	*
	*	addMenuItem() : string [ overwritten ]
	*
	*****************************************************************************/
	SubMenu.prototype.toString = function()
	{
		return "[SubMenu " + this.id + "]";
	}
	
	SubMenu.prototype._createCollapseNode = function()
	{
		//alert("OBSOLETE");
		// NOTHING
	}
	
	SubMenu.prototype._paint = function()
	{
		//alert("OBSOLETE");
		//alert("painting " + this);		

		if (this._getLayout() == 'COLLAPSE_MENU')
		{
			alert("COLLAPSING");
			//this._createCollapseNode();
			

			for (var i = 0; i < this._getMenuItemsCount(); i++)
			{
				this._menuItems[i]._paint();
				
				if (i == 0)
				{
					this._menuItems[i]._setTopPos(0);
				}
				else
				{
					this._menuItems[i]._setTopPos(this._menuItems[i - 1]._getBottom());
				}
				
				document.getElementById('bitbase_menu').appendChild(this._menuItems[i]._node);
			}
		}
		else
		{
			this._createNode();
			
			for (var i = 0; i < this._getMenuItemsCount(); i++)
			{
				this._menuItems[i]._paint();
				
				if (this._direction == 'HORIZONTAL')
				{
					// CHANGE
					this._getContentNode().appendChild(this._menuItems[i]._node);
				}
				else
				{
					// CHANGE
					var tr = createBitbaseNodeElement('TR');
					tr.appendChild(this._menuItems[i]._node);
					this._getContentNode().appendChild(tr);
				}	
			}
			
			this._hide();
		}
	}

/*******************************************************************************
* PRIVATE METHODS
*******************************************************************************/

	/*****************************************************************************
	*
	*	_createNode : void
	*
	*	Create <DIV><TABLE></TABLE><DIV> structure; each table element is a menu 
	* item might be ether horizontal or vertical
	*****************************************************************************/
	SubMenu.prototype._createNode = function()
	{
		//alert("OBSOLETE SubMenu.prototype._createNode");
		// DIV
		this._node = createBitbaseNodeElement('DIV');
		this._node.style.position = "absolute";
	
		// TABLE STRUCTURE
		table = createBitbaseNodeElement('TABLE');
		table.style.tableLayout = "fixed";
		table.style.emptyCells = "show";
		table.style.backgroundColor = "green";
		table.style.borderWidth = "0px";
		table.style.borderSpacing = "px";
		// Internet Explorer Compatibility
		table.setAttribute("cellSpacing", 0);
		var tbody = createBitbaseNodeElement('TBODY');
		
		tbody.setAttribute("bgColor", "#CCCCCC");
		
		table.appendChild(tbody);
		
		// DIV<->TABLE
		this._node.appendChild(table);
			
		// MAIN CONTAINER
		document.getElementById('bitbase_menu').appendChild(this._node);
		
		// TABLE ROW
		if (this._direction == 'HORIZONTAL')
		{
			var tr = createBitbaseNodeElement('TR');
			tbody.appendChild(tr);
		}
		
		// Register Event Handlers
		var self = this;
		this._node.onmouseout = function(e) {  self._subMenuMouseOut(e); }
	}

	/*****************************************************************************
	*
	*	_getContentNode : node
	*
	*****************************************************************************/
	SubMenu.prototype._getContentNode = function()
	{
		//alert("OBSOLETE SubMenu.prototype._getContentNode");
		if (this._direction == 'HORIZONTAL')
		{
			return this._node.getElementsByTagName("TR")[0];
		}
		return this._node.getElementsByTagName("TBODY")[0];
	}

	/*****************************************************************************
	*
	*	_setLeftPos(int) : void
	*
	*****************************************************************************/
	SubMenu.prototype._setLeftPos = function(posX)
	{
		//alert("OBSOLETE SubMenu.prototype._setLeftPos");
		this._node.style.left = posX;
	}

	/*****************************************************************************
	*
	*	_setTopPos(int) : void
	*
	*****************************************************************************/
	SubMenu.prototype._setTopPos = function(posY)
	{
		//alert("OBSOLETE SubMenu.prototype._setTopPos");
		this._node.style.top = posY;
	}

	/*****************************************************************************
	*
	*	_hide() : void
	*
	*****************************************************************************/
	SubMenu.prototype._hide = function()
	{
	  //alert("OBSOLETE SubMenu.prototype._setTopPos");
	  //alert("hide");
	  if (this._node)	this._node.style.visibility = 'hidden';
	  this._opened = false;
	}

	/*****************************************************************************
	*
	*	_show() : void
	*
	*****************************************************************************/
	SubMenu.prototype._show = function()
	{
		//alert("OBSOLETE SubMenu.prototype._setTopPos");
		if (this._getLayout() == 'COLLAPSE_MENU')
		{
			for (var i = 0; i < this._getMenuItemsCount(); i++)
			{
				this._menuItems[i]._node.style.visibility = "visible";
			}
		}
		else
		{
			this._node.style.visibility = 'visible';
		}
	}

	/*****************************************************************************
	*
	*	_getMenuItemsCount() : int
	*
	*****************************************************************************/
	SubMenu.prototype._getMenuItemsCount = function()
	{
		return this._menuItems.length;
	}

	/*****************************************************************************
	*
	*	_getParentMenu() : SubMenu
	*
	*****************************************************************************/
	SubMenu.prototype._getParentMenu = function()
	{
		if (this._parentMenuItem._parentMenu)
		{
			return this._parentMenuItem._parentMenu;
		}
	}

	/*****************************************************************************
	*
	*	_getLevel() : int
	*
	*****************************************************************************/
	SubMenu.prototype._getLevel = function()
	{
		return this._getParentMenu()._getLevel() + 1;
	}

	/*****************************************************************************
	*
	*	_getRootMenu() : Menu
	*
	*****************************************************************************/
	SubMenu.prototype._getRootMenu = function()
	{
		return this._getParentMenu()._getRootMenu();
	}

	/*****************************************************************************
	*
	*	_getLayout() : string
	*
	*****************************************************************************/	
	SubMenu.prototype._getLayout = function()
	{
		return this._getRootMenu()._getLayout();
	}

	/*****************************************************************************
	*
	*	_hideAll() : void
	*
	*****************************************************************************/
	SubMenu.prototype._hideAll = function()
	{
		//alert("OBSOLETE");
		this._hide();
		
		for (var i = 0; i < this._getMenuItemsCount(); i++)
		{
			if (this._menuItems[i]._subMenu)
			{
				this._menuItems[i]._subMenu._hideAll();
				
				this._menuItems[i]._isActive = false;
				this._menuItems[i]._setCssClassBlockDefault();
				this._menuItems[i]._setCssClassInlineDefault();
			}
		}	
	}

/*******************************************************************************
*	PRIVATE EVENT HANDLERS
*******************************************************************************/

	/*****************************************************************************
	*
	*	_subMenuMouseOut : void
	*
	*****************************************************************************/
	SubMenu.prototype._subMenuMouseOut = function(e)
	{
		//alert("OBSOLETE");
		if (!e) var e = window.event;
		
		var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
		
		// Hide this element if we leave the whole menu
		if (! reltg.bitbaseMenuItem)
		{
			this._getRootMenu()._hideAll();
		}
	}

