//arrMenuItems is a global array of MenuItems
//its used to reference MenuItems directly instead of through the tree structure
if(arrMenuItems === undefined){
var arrMenuItems = new Array();
}

function MenuItem(sLabel, sHref, sCssClass, sWidth, sFade, sTimeout)
{
	
	this.ID = arrMenuItems.length;
	arrMenuItems[this.ID] = this;
		
	this.label = !sLabel?'':sLabel;
	this.href = !sHref?'':sHref;
	this.cssClass = !sCssClass?'HeaderBG':sCssClass;
	this.subMenus = new Array();
	this.Parent = null;
	this.isRoot = false;
	this.Width = !sWidth?'':sWidth;
	this.Fade = !sFade?'.2':sFade;
	this.Timeout = !sTimeout?600:sTimeout;
	this.hideTimeout = -1;
}

MenuItem.prototype.WriteMain = function()
{
	document.write('<div ID="MenuItem_' + this.ID + '_DIV">' + this.getSubMenusHTML() + '</div>');
	for(var i = 0; i < this.subMenus.length; i++)
	{
		this.subMenus[i].DrawDiv();
	}
}

MenuItem.prototype.DrawDiv = function()
{
	if(this.subMenus.length > 0)
	{
		document.write('<div ID="MenuItem_' + this.ID + '_DIV" style="display:none;">' + this.getSubMenusHTML() + '</div>');
		for(var i = 0; i < this.subMenus.length; i++)
		{
			this.subMenus[i].DrawDiv();	
		}
	}
}

MenuItem.prototype.getSubMenusHTML = function()
{
	var out = '';
	for(var i = 0; i < this.subMenus.length; i++)
	{
		out += this.subMenus[i].getHTML();	
	}

	return out;
}

MenuItem.prototype.addMenuItem = function(sLabel, sHref, sCssClass, sWidth)
{
	var mMenu;
	if (typeof(sLabel) == 'object')
	{
		mMenu = sLabel;
	}
	else
	{
		mMenu = new MenuItem(sLabel, sHref, sCssClass, sWidth);
	}

	if (sCssClass == null) mMenu.cssClass = sLabel.cssClass;	
	this.subMenus[this.subMenus.length] = mMenu;	
	this.subMenus[this.subMenus.length - 1].Parent = this;
}

MenuItem.prototype.getHTML = function()
{	
	var out = '';
	out += '<a class="' + this.cssClass + '" ID="MenuItem_' + this.ID + '_A" style="display:block;' + ( (this.Width.toString().length > 0)?'width: ' + this.Width + ';':'') + '" href="' + this.href.replace('"', '&#34;') + '"';
	out += ' onmouseover="arrMenuItems[' + this.ID + '].onMouseOver(this);" ';
	out += ' onmouseout="arrMenuItems[' + this.ID + '].onMouseOut(this);" ';
	out += ' >' + this.label.replace(' ', '&nbsp;') + '</a>';
	return out;
}

MenuItem.prototype.onMouseOver = function(aTag)
{
	//make sure parents don't dissapear
	this.ClearHideSubs();
	
	//show subs
	if(this.subMenus.length > 0) this.DisplaySubs();
	
	//hide any lingering siblings
	if(this.Parent)	this.HideSiblings();
}

MenuItem.prototype.onMouseOut = function(aTag)
{
	//set all parents to dissapear
	this.SetHideSubs(this.Timeout);
}

//this function Shows a div and positions it next to aTag - with IE trans
MenuItem.prototype.ShowDiv = function(id, aTag)
{
	var mleft = getAbsoluteLeft(aTag);
	var mtop = getAbsoluteTop(aTag);
	var divTag = document.getElementById(id);
	
	if(document.all)
	{
		divTag.style.filter = 'blendTrans(duration=' + this.Fade + ');';
		window.status = divTag.style.filter;
		divTag.filters.blendTrans.Apply();
		divTag.filters.blendTrans.Play();
	}

	divTag.style.display = 'block';
	divTag.style.position = 'absolute';
	divTag.style.left = mleft + aTag.offsetWidth;
	divTag.style.top = mtop;
	divTag.style.zindex = 100;
}

//show this menu's subs
MenuItem.prototype.DisplaySubs = function()
{
	this.ShowDiv('MenuItem_' + this.ID + '_DIV', document.getElementById('MenuItem_' + this.ID + '_A'));
}

//hide this menus subs
MenuItem.prototype.HideSubs = function(){
	if(this.subMenus.length > 0 && !this.isRoot){
		var divTag = document.getElementById('MenuItem_' + this.ID + '_DIV');
		divTag.style.display='none';
	}
}

//set a timer to hide this menu's subs and it's ancestor's subs as well
MenuItem.prototype.SetHideSubs = function(timeout){
	window.status = timeout;
	if(!timeout){
		timeout = 600;	
	}
	
	//if its already scheduled to timeout, we won't do it twice....
	if (this.hideTimeout == -1)
		this.hideTimeout = setTimeout('arrMenuItems[' + this.ID + '].HideSubs();', timeout);
	
	if(this.Parent)
		this.Parent.SetHideSubs(timeout);
}

//stops the "hide menu" timer for this menu and all ancestors
MenuItem.prototype.ClearHideSubs = function(){
	clearTimeout(this.hideTimeout);
	this.hideTimeout = -1;
	
	if(this.Parent)
		this.Parent.ClearHideSubs();
}


//make sure the siblings of this menu are not showing their subs
MenuItem.prototype.HideSiblings = function(){
	
	for(var i = 0; i < this.Parent.subMenus.length;i++){
	
		if(this.Parent.subMenus[i].ID != this.ID){
			if(this.Parent.subMenus[i].subMenus.length > 0)
				this.Parent.subMenus[i].HideSubs();
		}	
		
	}	
	
}


function getAbsoluteLeft(o) {
	// Get an object left position from the upper left viewport corner
	// Tested with relative and nested objects
	oLeft = o.offsetLeft;           // Get left position from the parent object
	while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent ;   // Get parent object reference
		oLeft += oParent.offsetLeft; // Add parent left position
		o = oParent;
	}
	// Return left postion
	return oLeft;
}

function getAbsoluteTop(o) {
	// Get an object top position from the upper left viewport corner
	// Tested with relative and nested objects
	oTop = o.offsetTop;            // Get top position from the parent object
	while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent;  // Get parent object reference
		oTop += oParent.offsetTop; // Add parent top position
		o = oParent;
	}
	// Return top position
	return oTop;
}