
// settings
avtMS_lineHeight = 21;
avtMS_lineWidth = 160;
avtMS_menuoffset = 2;
avtMS_timeout = 500;
avtMS_timeoutSub = 300;
avtMS_maxdepth = 4;

avtMS_clrNormalBk = "#425364";
avtMS_clrNormalText = "#FFFFFF";

avtMS_clrHoverBk = "#FF9900";
avtMS_clrHoverText = "#FFFFFF";


bullet = "images/bullet.gif"; //RELATIVE PATH TO SUBMENU BULLET (NOT MANDATORY)

// POPUP ITEM OBJECT
function avtMenuItem(parentMenu, index, text, link, subItem)
{
	this.parent = parentMenu;
	this.index = index;
	this.text = text;
	this.link = link;
	this.subItem = subItem;
}

//===============================================================


// POPUP MENU OBJECT
function avtPopup(parentObj, divName, objName, x, y)
{
	this.name = divName;
	this.objName = objName;
	this.x = x;
	this.y = y;

	this.timeout = 0;
	this.parent = parentObj;
	this.parentItem = null;
	
	this.items = new Array();
	this.addItem = AddPopupItem;
	this.addItemIndirect = AddPopupItemIndirect;
	this.preCreate = PreCreatePopup;
	
	this.show = ShowPopup;
	this.hide = HidePopup;
	
	this.clrNormalBk = avtMS_clrNormalBk;
	this.clrNormalText = avtMS_clrNormalText;

	this.clrHoverBk = avtMS_clrHoverBk;
	this.clrHoverText = avtMS_clrHoverText;

	this.lineWidth = avtMS_lineWidth;
}


// methods

function AddPopupItem(newItem)
{
	iOrder = this.items.push(newitem) - 1;
	newItem.index = iOrder;

	if (newItem.subItem)
		newItem.subItem.parentItem = newItem;
		
	return newItem;
}

function AddPopupItemIndirect(text, link, subItem)
{
	newItem = new avtMenuItem(this, 0, text, link, subItem);
	iOrder = this.items.push(newItem) - 1;
	newItem.index = iOrder;
	
	if (newItem.subItem)
		newItem.subItem.parentItem = newItem;
	
	return newItem;
}

function PreCreatePopup()
{
	if (this.parent != null) // parent of a menu is an avtMenuItem object
	{
		// determine the position of this submenu
		this.x = parseInt(document.getElementById(this.parentItem.parent.name).style.left,10) + parseInt(document.getElementById(this.parentItem.parent.name).style.width, 10) + avtMS_menuoffset;
		this.y = parseInt(document.getElementById(this.parentItem.parent.name).style.top,10) +  avtMS_lineHeight * this.parentItem.index;
	}
	
	// let's create the popup (invisible for now)
	tmpbuf = "";
	
	// begin
	tmpbuf += "<table border='0' cellpadding='0' cellspacing='0' class='mainmenu' onMouseOver='avtMenuClearTimeout("+ this.objName + ");' onMouseOut='avtMenuSetTimeout("+ this.objName +");' style='width:100%'>";

	// let's draw the items
	for (i=0; i<this.items.length; i++)
	{
		if (this.items[i].subItem == null)
			tmpbuf += "<tr><td id ='asd' onMouseOver=\"this.style.color='"+ this.clrHoverText +"'; this.style.backgroundColor='"+ this.clrHoverBk +"'; this.style.cursor='pointer'\" onMouseOut=\"this.style.color='"+this.clrNormalText+"'; this.style.backgroundColor='"+this.clrNormalBk+"'\" onClick=\"window.open('"+this.items[i].link +"','_self')\" style='background-color: "+ this.clrNormalBk +"'>"+ this.items[i].text +"</td></tr>";
		else
			tmpbuf += "<tr><td style='padding-right: 0px' onMouseOver=\"this.style.color='"+this.clrHoverText+"'; this.style.backgroundColor='"+this.clrHoverBk+"'; "+ this.items[i].subItem.objName +".show(); this.style.cursor='pointer'\" onMouseOut=\"this.style.color='"+this.clrNormalText+"'; this.style.backgroundColor='"+this.clrNormalBk+"'; "+ this.items[i].subItem.objName +".hide("+ avtMS_timeoutSub +")\" onClick=\"window.open('"+this.items[i].link +"','_self')\" style='background-color: "+ this.clrNormalBk +"'><table class='subtable' cellpadding='0' cellspacing='0'><tr><td>"+ this.items[i].text +"</td><td align='right'><img src='"+ bullet +"' width='10' vspace='2' style='padding-left: 10px'></td></tr></table></td></tr>";
	}

	// end
	tmpbuf += "</table>";

	// flsuh the content in the browser
	document.getElementById(this.name).style.display = "none";
	
	if (this.x != -1)
		document.getElementById(this.name).style.left = this.x + "px";

	if (this.y != -1)
		document.getElementById(this.name).style.top = this.y + "px";
	
	if (this.lineWidth != -1)
		document.getElementById(this.name).style.width = this.lineWidth + "px";

	document.getElementById(this.name).innerHTML = tmpbuf;
	document.getElementById(this.name).style.position = "absolute";
//	document.getElementById(this.name).style.position = "static"; 
//	document.getElementById(this.name).style.position = "fixed";
//	document.getElementById(this.name).style.position = "relative";
//	document.getElementById(this.name).style.position = "inherit"; 
  
	document.getElementById(this.name).style.zIndex = 2; 
}

function ShowPopup(x)
{
	clearTimeout(this.timeout);
	// also, hide all the sibling
	if (this.parent != null)
	{
		for (i = 0; i < this.parent.items.length; i++)
		{
			if (this.parent.items[i].subItem && this.parent.items[i].subItem != this)
			{
				document.getElementById(this.parent.items[i].subItem.name).style.display = "none";
			}
		}
	}
	
	var left_pos=getLeftPos(document.getElementById(x));
	document.getElementById(this.name).style.left = (left_pos)+"px";
	document.getElementById(this.name).style.display = "block";
}

function HidePopup(timeToWait)
{
	if (timeToWait)
		this.timeout = setTimeout("document.getElementById('"+this.name+"').style.display = 'none'", timeToWait);
	else
	{
		clearTimeout(this.timeout);
		document.getElementById(this.name).style.display = 'none';
	}

	var i;
	for (i = 0; i < this.items.length; i++)
		if (this.items[i].subItem)
			this.items[i].subItem.hide();
}

//===============================================================

function avtMenuClearTimeout(objMenu)
{
	// clear the timer for all the opened parents as well
	tmpobjMenu = objMenu;
	while (tmpobjMenu)
	{
		clearTimeout(tmpobjMenu.timeout);
		tmpobjMenu = tmpobjMenu.parent;
	}
}

function avtMenuSetTimeout(objMenu)
{
	// also, let's send command to hide all the parents
	i = 1;
	while (objMenu)
	{
		objMenu.timeout = setTimeout("document.getElementById('"+objMenu.name+"').style.display = 'none'", i*avtMS_timeoutSub);
		if (!objMenu.parent)
		{
			clearTimeout(objMenu.timeout);
			objMenu.timeout = setTimeout("document.getElementById('"+objMenu.name+"').style.display = 'none'", (i-1)*avtMS_timeoutSub + avtMS_timeout);
		}

		objMenu = objMenu.parent;
		i++;
	}
}
function getLeftPos(inputObj) 
{ 
	var returnValue = inputObj.offsetLeft; 
	var topOfMenuReached = false; 
	while((inputObj = inputObj.offsetParent) != null)
	{ 
		if(inputObj.parentNode.id=='links')topOfMenuReached=true; 
		if(topOfMenuReached && !inputObj.className.match(/menuBlock/gi))
		{
			var style = getStyle(inputObj,'position'); 
			if(style=='absolute' || style=='relative')return returnValue; 
		} 
		returnValue += inputObj.offsetLeft; 
	} 
	return returnValue; 
}