/*******************************************************************************
*
*	MenuItem.js
*
*******************************************************************************/

/*******************************************************************************
* CONSTRUCTOR
*******************************************************************************/
function MenuItem(args)
{
	//alert("CONSTRUCT MenuItem");
	if (! args)
	{
		args = [];
	}
	this._init(args);

}


/*******************************************************************************
*	INIT
*******************************************************************************/
MenuItem.prototype._init = function(args)
{
	//alert("_init MenuItem");
	//MenuItem.superclass._init.call(this);

	this._text 									= args['TEXT'] 								|| "No Text specified";
	this._cssClassBlockDefault	= args['CSS_BLOCK_DEFAULT']		|| "defaultBlock";
	this._cssClassInlineDefault	= args['CSS_INLINE_DEFAULT']	|| "defaultInline";
	this._cssClassBlockHover		= args['CSS_BLOCK_HOVER']			|| "hoverBlock";
	this._cssClassInlineHover		= args['CSS_INLINE_HOVER']		|| "hoverInline";
	this._cssClassBlockActive		= args['CSS_BLOCK_ACTIVE']		|| "default_block";

	this._cssClassBlockActiveHover		= args['CSS_BLOCK_ACTIVE_HOVER']		|| "default_block";
	this._cssClassBlockActive					= args['CSS_BLOCK_ACTIVE']		|| "default_block";
	this._cssClassInlineActiveHover		= args['CSS_INLINE_ACTIVE_HOVER']		|| "default_block";
	this._cssClassInlineActive				= args['CSS_INLINE_ACTIVE']		|| "default_block";


	this._extraPosition					= args['EXTRA_POSITION']			|| "10";
	this._separator					= args['SEPARATOR']			|| "";
	this._menuId								= args['MENU_ID']							|| 0;
	this._pageId								= args['PAGE_ID']							|| 0;
	this._productCategoryId			= args['PRODUCT_CATEGORY_ID']						|| 0;
	this._windowLocation				= args['WINDOW_LOCATION']						|| '/';
	this._isActive = false;

	this._alwaysHidden					= args['ALWAYS_HIDDEN']						|| false;

	//alert(this._alwaysHidden);

	// will hold a reference to a submenu of this element
	this._subMenu = undefined;

	// will hold a reference to the menu container where this element is in
	this._parentMenu = undefined;
	// OBSOLETE this._createNode();
}

/*******************************************************************************
* PUBLIC METHODS
*******************************************************************************/

	/*****************************************************************************
	*
	*	toString() : string [ overwritten ]
	*
	*****************************************************************************/
	MenuItem.prototype.toString = function()
	{
		return "[MenuItem " + this._text + "]";
	}

	/*****************************************************************************
	*
	*	setSubMenu(SubMenu) : void
	*
	*****************************************************************************/
	MenuItem.prototype.setSubMenu = function(subMenu)
	{
		this._subMenu = subMenu;

		// Cross reference
		subMenu._parentMenuItem = this;
	}

/*******************************************************************************
* PRIVATE METHODS
*******************************************************************************/

	/*****************************************************************************
	*
	*	_createNode() : void
	*
	*****************************************************************************/
	MenuItem.prototype._createNode = function()
	{
		//alert("OBSOLETE MenuItem.prototype._createNode");
		this._node = createBitbaseNodeElement('TD');
		this._node.style.border = "1px solid blue";
		var text = document.createTextNode(this._text);
		this._node.appendChild(text);

		// set the CSS class
		this._setCssClass("default");


	}

	/*****************************************************************************
	*
	*	_setCssClass(string) : void
	*
	*****************************************************************************/
	MenuItem.prototype._setCssClass = function(cssClass)
	{
		//alert("OBSOLETE MenuItem.prototype._setCssClass");
		this._node.className = cssClass;
	}

	/*****************************************************************************
	*
	*	_hasSubMenu() : boolean
	*
	*****************************************************************************/
	MenuItem.prototype._hasSubMenu = function()
	{
		//alert("ERROR: OBSOLETE");
		if (this._subMenu)
		{
			return true;
		}
		return false;
	}

	/*****************************************************************************
	*
	*	_getLeft() : int
	*
	*****************************************************************************/
	MenuItem.prototype._getLeft = function()
	{
		//alert("OBSOLETE MenuItem.prototype._getLef");
		var obj = this._node;
		var curleft = 0;

		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				curleft += obj.offsetLeft
				obj = obj.offsetParent;
			}
		}
		else if (obj.x)
			curleft += obj.x;

		return curleft;
	}

	/*****************************************************************************
	*
	*	_getTop() : int
	*
	*****************************************************************************/
	MenuItem.prototype._getTop = function()
	{
		//alert("OBSOLETE MenuItem.prototype._getTop");
		var obj = this._node;
		var curtop = 0;
		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				curtop += obj.offsetTop
				obj = obj.offsetParent;
			}
		}
		else if (obj.y)
			curtop += obj.y;
		return curtop;
	}

	/*****************************************************************************
	*
	*	_getHeight() : int
	*
	*****************************************************************************/
	MenuItem.prototype._getHeight = function()
	{
	  //alert("OBSOLETE MenuItem.prototype._getHeight");
	  // http://www.aspandjavascript.co.uk/javascript/javascript_api/get_element_width_height.asp
	  var obj = this._node;
	  var xPos = obj.offsetHeight;
	  return xPos;
	}

	/*****************************************************************************
	*
	*	_getTop() : int
	*
	*****************************************************************************/
	MenuItem.prototype._getBottom = function()
	{
		//alert("OBSOLETE MenuItem.prototype._getBottom");
		return this._getTop() + this._getHeight();
	}

	/*****************************************************************************
	*
	*	_getWidth() : int
	*
	*****************************************************************************/
	MenuItem.prototype._getWidth = function()
	{
	  //alert("OBSOLETE MenuItem.prototype._getWidth");
	  var obj = this._node;
	  var xPos = obj.offsetWidth;
	  return xPos;
	}

	/*****************************************************************************
	*
	*	_getRight() : int
	*
	*****************************************************************************/
	MenuItem.prototype._getRight = function()
	{
		//alert("OBSOLETE MenuItem.prototype._getRight");
		return this._getLeft() + this._getWidth();
	}

	/*****************************************************************************
	*
	*	_getRootMenu() : Menu
	*
	*****************************************************************************/
	MenuItem.prototype._getRootMenu = function()
	{
		return this._parentMenu._getRootMenu();
	}

	/*****************************************************************************
	*
	*	_getLayout() : string
	*
	*****************************************************************************/
	MenuItem.prototype._getLayout = function()
	{
		return this._getRootMenu()._getLayout();
	}

/*******************************************************************************
*	PRIVATE EVENT HANDLERS
*******************************************************************************/

	/*****************************************************************************
	*
	*	_menuItemOut() : void
	*
	*****************************************************************************/
	MenuItem.prototype._menuItemOut = function(e)
	{
		//alert("ERROR: OBSOLETE");

		if (!e) var e = window.event;

		var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
	}

	/*****************************************************************************
	*
	*	_menuItemEnter() : void
	*
	*****************************************************************************/
	MenuItem.prototype._menuItemEnter = function()
	{
		//alert("OBSOLETE MenuItem.prototype._menuItemEnter");
		if (!e) var e = window.event;

		// Hide all open items
		for (var i = 0; i < this._parentMenu._getMenuItemsCount(); i++)
		{
			//this._parentMenu._menuItems[i]._subMenu._hide();
			if (this._parentMenu._menuItems[i]._subMenu)
			{
				// TODO: Check if its the current item
				this._parentMenu._menuItems[i]._subMenu._hideAll();
			}
		}

		// if there is a submenu, open it
		if (this._subMenu)
		{
			switch (this._getLayout())
			{
				case 'TOP_MENU':
					if (this._subMenu._getLevel() == 1)
					{
						this._subMenu._setLeftPos(this._getLeft());
						this._subMenu._setTopPos(this._getBottom());
					}
					else
					{
						this._subMenu._setLeftPos(this._getRight());
						this._subMenu._setTopPos(this._getTop());
					}
					break;

				case 'LEFT_MENU':
					this._subMenu._setLeftPos(this._getRight());
					this._subMenu._setTopPos(this._getTop());
					break;
			}


			this._subMenu._show();
		}
	}

/*MenuItem.prototype._show = function()
{
	this._node.style.visibility = "visible";
}*/

MenuItem.prototype._createCollapseNode = function()
{
	//alert("OBSOLETE MenuItem.prototype._createCollapseNode");
	alert("Creating " + this);
	this._node = createBitbaseNodeElement('DIV');
	this._node.style.border = "1px solid blue";
	var text = document.createTextNode(this._text);
	this._node.style.position = "absolute";
	this._node.appendChild(text);

	if (this._getLevel() == 0)
	{
		this._node.style.visibility = 'visible';
	}
	else
	{
		this._node.style.visibility = 'hidden';
	}

}

MenuItem.prototype._getLevel = function()
{
	return this._parentMenu._getLevel();
}

MenuItem.prototype._menuItemClick = function()
{
	//alert("OBSOLETE MenuItem.prototype._menuItemClick");
	if (!e) var e = window.event;

	//alert(this);

	this._subMenu._show();

	pos = 0;

	for (var i = 0; i < this._parentMenu._getMenuItemsCount(); i++)
	{
		//alert(this._parentMenu._menuItems[i]);


		if (i == 0)
		{
			this._parentMenu._menuItems[i]._setTopPos(0);
		}
		else
		{
			this._parentMenu._menuItems[i]._setTopPos(pos);
		}

		pos = this._parentMenu._menuItems[i]._getBottom();



		if (this == this._parentMenu._menuItems[i])
		{
			for (var j = 0; j < this._subMenu._getMenuItemsCount(); j++)
			{
				//alert(this._subMenu._menuItems[j]);
				this._subMenu._menuItems[j]._setTopPos(pos);
				pos = this._subMenu._menuItems[j]._getBottom();
			}
		}


	}
}

	/*****************************************************************************
	*
	*	_setTopPos(int) : void
	*
	*****************************************************************************/
	MenuItem.prototype._setTopPos = function(posY)
	{
		//alert("OBSOLETE MenuItem.prototype._setTopPos");
		this._node.style.top = posY;
	}

MenuItem.prototype._paint = function()
{
	//alert("OBSOLETE MenuItem.prototype._paint");
	if (this._getLayout() == 'COLLAPSE_MENU')
	{
		this._createCollapseNode();

		var self = this;
		this._node.onclick = function(e) {  self._menuItemClick(e); }

		if (this._subMenu)
		{
			this._subMenu._paint();
		}
	}
	else
	{
		this._createNode();

		// register event handlers
		var self = this;
		this._node.onmouseover = function(e) {  self._menuItemEnter(e); }
		//this._node.onmouseout = function(e) {  self._menuItemOut(e); }

		if (this._subMenu)
		{
			this._subMenu._paint();
		}
	}
}

MenuItem.prototype.setActive = function()
{
	this._setCssClassBlockActive();
	this._setCssClassInlineActive();

	this._isActive = true;

	if (this._parentMenu)
	{
		//alert(this + " has parentMenu");
		this._parentMenu._opened = true;
		if (this._getLevel() > 0)
		{
			//alert(this._parentMenu._parentMenuItem);
			this._parentMenu._parentMenuItem.setActive();
		}
	}
}


	/*****************************************************************************
	*
	*	_setCssClassDefault(string) : void
	*
	*****************************************************************************/
	MenuItem.prototype._setCssClassBlockDefault = function()
	{
		//alert("OBSOLETE MenuItem.prototype._setCssClass");
		this._node.className = this._cssClassBlockDefault;
	}

	MenuItem.prototype._setCssClassBlockActive = function()
	{
		//alert(this._cssClassBlockActive);
		this._node.className = this._cssClassBlockActive;
	}

	MenuItem.prototype._setCssClassBlockActiveHover = function()
	{
		//alert('MenuItem.prototype._setCssClassBlockActiveHover');
		this._node.className = this._cssClassBlockActiveHover;
	}

	MenuItem.prototype._setCssClassInlineActive = function()
	{
		//alert('MenuItem.prototype._setCssClassInlineActive');
		this._inlineNode.className = this._cssClassInlineActive;
	}

	MenuItem.prototype._setCssClassInlineActiveHover = function()
	{
		//alert(this._cssClassInlineActiveHover);
		this._inlineNode.className = this._cssClassInlineActiveHover;
	}

	/*****************************************************************************
	*
	*	_setCssClassDefault(string) : void
	*
	*****************************************************************************/
	MenuItem.prototype._setCssClassInlineDefault = function()
	{
		//alert("OBSOLETE MenuItem.prototype._setCssClass");
		this._inlineNode.className = this._cssClassInlineDefault;
	}

	/*****************************************************************************
	*
	*	_setCssClassBlockHover() : void
	*
	*****************************************************************************/
	MenuItem.prototype._setCssClassBlockHover = function()
	{
		//alert("OBSOLETE MenuItem.prototype._setCssClass");
		this._node.className = this._cssClassBlockHover;
	}

	/*****************************************************************************
	*
	*	_setCssClassInlineHover() : void
	*
	*****************************************************************************/
	MenuItem.prototype._setCssClassInlineHover = function()
	{
		//alert("OBSOLETE MenuItem.prototype._setCssClass");
		this._inlineNode.className = this._cssClassInlineHover;
	}

	MenuItem.prototype.activate = function()
	{
		alert("ACTIVATE");
	}
