// -------------------------------------------------
//
// DynamicMenu Class
//
// -------------------------------------------------

// <summary></summary>
// <parameter name="varName">The name of variable that will hold a reference to this instance (required for setTimeout function)</parameter>
function DynamicMenu(varName)
{
	this.varName = varName;
	this.menus = new Array();
	this.menuType = "float";
}

// -------------------------------------------------
// DynamicMenu public methods
// -------------------------------------------------

// <summary></summary>
// <parameter name="menu"></parameter>
DynamicMenu.prototype.isMenuExists = function(menu)
{
	if(menu != null)
	{
		for(var i = 0; i < this.menus.length; i++)
		{
			if(this.menus[i] == menu)
			{
				return true;
			}
		}
	}

	return false;
}

// <summary></summary>
// <parameter name="menu"></parameter>
DynamicMenu.prototype.addMenu = function(menu)
{
	// add the menu only if it does not exist within the menus array
	if(menu.length > 0 && !this.isMenuExists(menu))
	{
		var arrLength = this.menus.push(menu);
		if(arrLength == 1 && this.defaultMenu == null)
		{
			this.defaultMenu = this.menus[0];
		}
	}
}

// <summary></summary>
DynamicMenu.prototype.init = function()
{
	this._setMenu();
}

// <summary></summary>
// <parameter name="defaultMenu"></parameter>
DynamicMenu.prototype.setDefaultMenu = function(defaultMenu)
{
	for(var i = 0; i < this.menus.length; i++)
	{
		if(this.menus[i] == defaultMenu)
		{
			this.defaultMenu = defaultMenu;
			break;
		}
	}
}

// <summary>
//	Specifies the type of menu for this instance.
// "Float" (drop downs) are layered over other content and hidden when inactive.
// "Block" menus have their own dedicated space and are css styled as block elements.
// </summary>
// <parameter name="menuType"></parameter>
DynamicMenu.prototype.setMenuType = function(menuType)
{
	menuType = menuType.toLowerCase();
	switch(menuType)
	{
		case("block"):
		case("float"):
			this.menuType = menuType;
	}
}

// <summary></summary>
// <parameter name="menuID"></parameter>
DynamicMenu.prototype.showMenu = function(menuID)
{
	this.clearMenuTimeout();

	if(this.isMenuExists(menuID))
	{
		this.menuTimeoutID = setTimeout(this.varName + "._setMenu('" + menuID + "')",300);
	}
	else
	{
		this.menuTimeoutID = setTimeout(this.varName + "._setMenu()",2000);
	}
}

// <summary></summary>
DynamicMenu.prototype.clearMenuTimeout = function()
{
	clearTimeout(this.menuTimeoutID);
}


// -------------------------------------------------
// DynamicMenu private methods
// -------------------------------------------------

// <summary></summary>
// <parameter name="menuID"></parameter>
DynamicMenu.prototype._setMenu = function(menuID)
{
	var tab;
	var menu;
	var ua = new uaSniffer();

	if (ua.isCool)
	{
		this._resetMenus();

		if(menuID == null)
		{
			tab = document.getElementById("tab" + this.defaultMenu);
			if(tab != null) tab.className = "tabOn";

			if(this.menuType == "block")
			{
				menu = document.getElementById("menu" + this.defaultMenu);
				if(menu != null) menu.className = "menuOn";
			}
		}
		else
		{
			tab = document.getElementById("tab" + menuID);
			if(tab != null) tab.className = "tabOn";

			menu = document.getElementById("menu" + menuID);	
			if(menu != null) menu.className = "menuOn";

			if(menu != null && this.menuType == "float") this._setMenuPosition(menuID);
		}
	}
}

// <summary></summary>
DynamicMenu.prototype._resetMenus = function()
{
	for (var i = 0; i < this.menus.length; i++)
	{
		var menu = document.getElementById("menu" + this.menus[i]);
		if(menu != null) menu.className = "menuOff";

		var tab = document.getElementById("tab" + this.menus[i]);
		if(tab != null) tab.className = "tabOff"
	}
}

// <summary></summary>
// <parameter name="menuID"></parameter>
DynamicMenu.prototype._setMenuPosition = function(menuID)
{
	var tab = document.getElementById("tab" + menuID);
	var menu = document.getElementById("menu" + menuID);
	var tabX = this._findPosX(tab);
	var tabY = this._findPosY(tab);

	menu.style.left = (tabX + 14) + 'px';
	menu.style.top = (tabY + 29) + 'px';
}

// <summary>Returns the x axis coordinates for the specified object</summary>
// <parameter name="obj"></parameter>
DynamicMenu.prototype._findPosX = function(obj)
{
	var curleft = 0;

	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
	{
		curleft += obj.x;
	}

	return curleft;
}

// <summary>Returns the y axis coordinates for the specified object</summary>
// <parameter name="obj"></parameter>
DynamicMenu.prototype._findPosY = function(obj)
{
	var curtop = 0;

	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
	{
		curtop += obj.y;
	}

	return curtop;
}

