/*
********************************************************************************
**** Popup Menu Handler
********************************************************************************
*/
function popupMenu(id,div,baseStyle,mode)
{
	this.id = id;
	this.div = div;
	this.Subdiv = null;
	this.items = new Array;
	this.selectedItem = 0;
	this.itemCount = 0;
	if (mode)
	{ this.mode = mode; } //h = horizonzal / v=vertical fold out
	else { this.mode = 'h'; }
	this.isOpen = false;
	this.baseStyle = baseStyle;
	this.stretch = false;
};

popupMenu.prototype.getSubDiv = function()
{
	if (this.Subdiv)
	{
		return this.Subdiv;
	}
	else
	{
		this.Subdiv = document.createElement('div');
		this.Subdiv.id = this.id + 'SubMenuDiv';
		document.body.appendChild(this.Subdiv);
		return this.Subdiv;
	}
};

popupMenu.prototype.findPosX = function(obj)
{
	var curleft = 0;
	if(obj.offsetParent)
	{
	    while(1) 
	    {
	      curleft += obj.offsetLeft;
	      if(!obj.offsetParent) {break; }
	      obj = obj.offsetParent;
		}
	}
	else if(obj.x) { curleft += obj.x; }
	return curleft;
};

popupMenu.prototype.findPosY = function(obj)
{
	var curtop = 0;
	if(obj.offsetParent)
	{
		while(1)
		{
			curtop += obj.offsetTop;
			if(!obj.offsetParent) {	break; }
			obj = obj.offsetParent;
		}
	}
	else if(obj.y) { curtop += obj.y; }
	return curtop;
};

popupMenu.prototype.clear = function()
{
	this.items = new Array;
	this.itemCount = 0;
};

popupMenu.prototype.a = function(text,tag,icon,onclick)
{
	//function popMenuItem(menu,parent,text,tag,icon,tag,onclick)
	var item = new popUpMenuItem(this,this,text,icon,tag,onclick);
	this.items.push(item);
	item.id = this.items.length - 1;
	this.itemCount++;
	return item;
};

popupMenu.prototype.refresh=function()
{
	var el = document.getElementById(this.div)
	if (el)
	{
		el.innerHTML = this;
	}
};

popupMenu.prototype.over=function(e,id)
{
	//unhighlight all top menu items
	for (var i=0; i<this.items.length; i++)
	{
		document.getElementById(this.id + '.' + i).className = this.baseStyle + 'Item';
		document.getElementById(this.id + '.' + i + 'Text').className = this.baseStyle + 'ItemText';
	}
				
	var item = this.findItem(id);
	if (item)
	{
		var el = this.getSubDiv();
		var elParent = document.getElementById(this.id + '.' + id);
		var elText = document.getElementById(this.id + '.' + id + 'Text');
		var elDiv  = document.getElementById(this.div);
		
		if (el && elParent && elDiv && elText)
		{
			var yTop = this.findPosY(elParent);
			var xLeft = this.findPosX(elParent);
			
			if (this.mode == 'h')
			{
				xLeft += elParent.offsetWidth + 1;
			}
			else if (this.mode == 'v')
			{
				yTop += elParent.offsetHeight + 1;
			}
		
			el.innerHTML = item;
			el.style.display = 'block';
			el.style.position = 'absolute';
			el.style.top = yTop;
			el.style.left = xLeft;
			this.isOpen = true;
			
			elParent.className = this.baseStyle + 'ItemOver';
			elText.className = this.baseStyle + 'ItemTextOver';
		}
	}
};




popupMenu.prototype.out=function(e,id)
{
	var item = this.findItem(id);
	if (item)
	{
		var el = this.getSubDiv();
		var elParent = document.getElementById(this.id + '.' + id);
		var elDiv  = document.getElementById(this.div);
		
		if (el && elParent && elDiv)
		{
			window.setTimeout(this.id + '.hide(' + id + ')', 500);
			this.isOpen = false;
		}
	}
};

popupMenu.prototype.hide=function(id)
{
	var item = this.findItem(id);
	if (item)
	{
		var el = this.getSubDiv();
		var elParent = document.getElementById(this.id + '.' + id);
		var elDiv  = document.getElementById(this.div);
		if (el && elParent && elDiv)
		{
			if (!this.isOpen)
			{
				el.style.display = 'none';
				
				//unhighlight all top menu items
				for (var i=0; i<this.items.length; i++)
				{
					document.getElementById(this.id + '.' + i).className = this.baseStyle + 'Item';
					document.getElementById(this.id + '.' + i + 'Text').className = this.baseStyle + 'ItemText';
				}
			}
		}
	}
};

popupMenu.prototype.setItemText=function(index,text)
{
	var el = document.getElementById(this.id + '.' + index);
	if (el)
	{
		el.innerHTML = text;
	}
};

popupMenu.prototype.setSubItemText=function(index, subIndex, text)
{
	var c = this.findItem(index).findItem(subIndex);
	if (c) { c.text = text; }
	var el = document.getElementById(this.id + '.' + index + '.' + subIndex);
	if (el)
	{
		el.innerHTML = text;
	}
};

popupMenu.prototype.toString=function()
{
	var s = '';
	
	if (this.mode == 'h')
	{
		s+= '<table id=\"' + this.id + 'tab\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\" style=\"table-layout:fixed;\" class=\"' + this.baseStyle + '\"';
		s += '>';
		
		for (var i=0; i<this.items.length; i++)
		{
			s += '<tr>';
			s += '<td';
			s += ' class=\"' + this.baseStyle + 'Item\"';
			s += ' onMouseOver=\"' + this.id + '.over(event,' + i + ');\"';
			s += ' onMouseOut=\"' + this.id + '.out(event,' + i + ');\"';
			s += ' onClick=\"' + this.id + '.sel(' + i + ');\"';
			s += ' id=\"' + this.id + '.' + i + '\"';
			s += ' nowrap>';
			s += '<span class=\"' + this.baseStyle + 'ItemText\" id=\"' + this.id + '.' + i + 'Text\">';
			s += this.items[i].text;
			s += '</span>';
			s += '</td>';
			s += '</tr>';
		}
		
		s+= '</table>';
	}
	else if (this.mode == 'v')
	{
		s+= '<table id=\"' + this.id + 'tab\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"\" class=\"' + this.baseStyle + '\"';
		if (this.stretch)
		{
		  s += ' width=\"100%\"';
		}
		s += '>';
		
		s += '<tr>';
		for (var i=0; i<this.items.length; i++)
		{
			s += '<td';
			s += ' class=\"' + this.baseStyle + 'Item\"';
			s += ' onMouseOver=\"' + this.id + '.over(event,' + i + ');\"';
			s += ' onMouseOut=\"' + this.id + '.out(event,' + i + ');\"';
			s += ' onClick=\"' + this.id + '.sel(' + i + ');\"';
			s += ' id=\"' + this.id + '.' + i + '\"';
			s += ' nowrap>';
			s += '<span class=\"' + this.baseStyle + 'ItemText\" id=\"' + this.id + '.' + i + 'Text\">';
			s += this.items[i].text;
			s += '</span>';
			s += '</td>';
		}
		s += '</tr>';
		
		s+= '</table>';
	}
	
	return s;
};

popupMenu.prototype.sel = function(id)
{
	var item = this.findItem(id);
	if (item != null)
	{
		if (item.onclick)
		{
			//item.onclick();
			window.location = item.onclick;
		}
		else
		{
		}
	}
};

popupMenu.prototype.findItemByTag = function(tag)
{
	for (var i=0; i<this.items.length; i++)
	{
		if (this.items[i].tag == tag)
		{ return this.items[i]; }
	}
};

popupMenu.prototype.findItem = function(id)
{
	for (var i=0; i<this.items.length; i++)
	{
		if (this.items[i].id == id)
		{ return this.items[i]; }
	}
};


/*
********************************************************************************
**** Popup Menu SubItem Handler
********************************************************************************
*/
function popUpMenuItem(m,parent,text,tag,icon,onclick)
{
	this.menu = m;
	this.p = parent;
	this.icon = icon;
	this.text = text;
	this.onclick = onclick;
	this.tag = tag;
	this.id = -1;
	
	this.items = new Array;
	this.itemCount = 0;
};

popUpMenuItem.prototype.sel = function(id)
{
	var item = this.findItem(id);
	if (item != null)
	{
		if (item.onclick)
		{
			//item.onclick();
			window.location = item.onclick;
		}
		else
		{
		}
	}
};

popUpMenuItem.prototype.a = function(text,tag,icon,onclick)
{
	//function popMenuItem(parent,text,tag,icon,tag,onclick)
	var item = new popUpMenuItem(this.menu,this,text,icon,tag,onclick);
	this.items.push(item);
	item.id = this.items.length - 1;
	this.itemCount++;
	return this;
};

popUpMenuItem.prototype.over=function(e,id)
{
	var item = this.findItem(id);
	if (item)
	{
		var el = document.getElementById(this.menu.id + '.' + this.id + '.' + id);
		var elText = document.getElementById(this.menu.id + '.' + this.id + '.' + id + 'Text');
		var elParent = document.getElementById(this.menu.id + '.' + this.id);
		if (el && elText && elParent)
		{
			el.className = this.menu.baseStyle + 'SubItemOver';
			elText.className = this.menu.baseStyle + 'SubItemTextOver';
			this.menu.isOpen = true;
		}
	}
};

popUpMenuItem.prototype.out=function(e,id)
{
	var item = this.findItem(id);
	if (item)
	{
		var el = document.getElementById(this.menu.id + '.' + this.id + '.' + id);
		var elText = document.getElementById(this.menu.id + '.' + this.id + '.' + id + 'Text');
		var elParent = document.getElementById(this.menu.id + '.' + this.id);
		if (el && elParent && elText)
		{
			el.className = this.menu.baseStyle + 'SubItem';
			elText.className = this.menu.baseStyle + 'SubItemText';
			this.menu.isOpen = false;
			window.setTimeout(this.menu.id + '.hide(' + this.id + ')', 500);
		}
	}
};

popUpMenuItem.prototype.findItemByTag = function(tag)
{
	for (var i=0; i<this.items.length; i++)
	{
		if (this.items[i].tag == tag)
		{ return this.items[i]; }
	}
};

popUpMenuItem.prototype.findItem = function(id)
{
	for (var i=0; i<this.items.length; i++)
	{
		if (this.items[i].id == id)
		{ return this.items[i]; }
	}
};

popUpMenuItem.prototype.toString = function()
{
	var s = '';
	
	s+= '<table id=\"' + this.id + 'tab\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\" style=\"table-layout:fixed;\" class=\"' + this.menu.baseStyle + 'Sub\">';
		
	for (var i=0; i<this.items.length; i++)
	{
		s += '<tr>';
		s += '<td';
		s += ' class=\"' + this.menu.baseStyle + 'SubItem\"';
		s += ' onMouseOver=\"' + this.menu.id + '.findItem(' + this.id + ').over(event,' + i + ');\"';
		s += ' onMouseOut=\"' + this.menu.id + '.findItem(' + this.id + ').out(event,' + i + ');\"';
		s += ' onClick=\"' + this.menu.id + '.findItem(' + this.id + ').sel(' + i + ');\"';
		s += ' id=\"' + this.menu.id + '.' + this.id + '.' + i + '\"';
		s += ' nowrap>';
		s += '<span class=\"' + this.menu.baseStyle + 'SubItemText\" id=\"' + this.menu.id + '.' + this.id + '.' + i + 'Text\">';
		s += this.items[i].text;
		s += '</span>';
		s += '</td>';
		s += '</tr>';
	}
	s+= '</table>';
		
	return s;
};
