//private globals:
var g_nActive = 0;
var g_ActiveMenuIDs			= new Array();
var g_ActiveItemIDs			= new Array();	
var g_ActiveChildMenuIDs	= new Array();
var g_TimerID 				= 0;
var g_MenuBarID				= new String('');

function getObjNN4(obj,name)
{
	var x = obj.layers;
	var foundLayer;
	for (var i=0;i<x.length;i++)
	{
		if (x[i].id == name)
		 	foundLayer = x[i];
		else if (x[i].layers.length)
			var tmp = getObjNN4(x[i],name);
		
		if (tmp) 
			foundLayer = tmp;
	}
	return foundLayer;
}

function getElementHeight(Elem) 
{
	if (isNS4Browser()) 
	{
		var elem = getObjNN4(document, Elem);
		return elem.clip.height;
	} 
	else 
	{
		var elem;
		if(document.getElementById) 
			var elem = document.getElementById(Elem);
		else if (document.all)
			var elem = document.all[Elem];
		
		if (isOP5Browser()) 
			xPos = elem.style.pixelHeight;
		else 
			xPos = elem.offsetHeight;
		return xPos;
	} 
}

function getElementWidth(Elem) 
{
	if (isNS4Browser()) 
	{
		var elem = getObjNN4(document, Elem);
		return elem.clip.width;
	} 
	else 
	{
		var elem;
		if(document.getElementById) 
			var elem = document.getElementById(Elem);
		else if (document.all)
			var elem = document.all[Elem];
		if (isOP5Browser()) 
			xPos = elem.style.pixelWidth;
		else 
			xPos = elem.offsetWidth;
		return xPos;
	}
}

function getElementLeft(Elem) 
{
	if (isNS4Browser()) 
	{
		var elem = getObjNN4(document, Elem);
		return elem.pageX;
	} 
	else 
	{
		var elem;
		if(document.getElementById) 
			var elem = document.getElementById(Elem);
		else if (document.all)
			var elem = document.all[Elem];
		xPos = elem.offsetLeft;
		tempEl = elem.offsetParent;
  		while (tempEl != null) 
		{
  			xPos += tempEl.offsetLeft;
	  		tempEl = tempEl.offsetParent;
  		}
		return xPos;
	}
}

function getElementTop(Elem) 
{
	if (isNS4Browser()) 
	{
		var elem = getObjNN4(document, Elem);
		return elem.pageY;
	} 
	else 
	{
		if(document.getElementById) 
			var elem = document.getElementById(Elem);
		else if (document.all) 
			var elem = document.all[Elem];
		yPos = elem.offsetTop;
		tempEl = elem.offsetParent;
		while (tempEl != null) 
		{
  			yPos += tempEl.offsetTop;
	  		tempEl = tempEl.offsetParent;
  		}
		return yPos;
	}
}

function moveXY(myObject, x, y) 
{
	obj = getStyleObject(myObject);
	if (isNS4Browser()) 
	{
		obj.top = y;
 		obj.left = x;
	} 
	else if (isOP5Browser()) 
	{
		obj.pixelTop = y;
 		obj.pixelLeft = x;
	} 
	else 
	{
		obj.top = y + 'px';
		obj.left = x + 'px';
	}	
}

function getStyleObject(objectId) 
{
    //alert('--tryint to obtain style object!');
	if (objectId != null)
	{
		// cross-browser function to get an object's style object given its id
		if(document.getElementById && document.getElementById(objectId)) 
			return document.getElementById(objectId).style;// W3C DOM
		else if (document.all && document.all(objectId))
			return document.all(objectId).style;// MSIE 4 DOM
		else if (document.layers && document.layers[objectId])
			return getObjNN4(document,objectId);// NN 4 DOM 
		else 
			return false;
	}
	else
		return false;
} // getStyleObject

function changeObjectVisibility(objectId, newVisibility) 
{
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) 
	{
		styleObject.visibility = newVisibility;
		return true;
    } 
	else 
		//we couldn't find the object, so we can't change its visibility
		return false;
} // changeObjectVisibility

function MakeVisible(objectId)
{
	if (objectId != null)
		changeObjectVisibility(objectId, 'visible');	
}

function MakeInvisible(objectId)
{
	if (objectId != null)
		changeObjectVisibility(objectId, 'hidden');	
}

function changeBGColour(myObject, colour) 
{
	if (isNS4Browser()) 
	{
		var obj = getObjNN4(document, myObject);
		obj.bgColor=colour;
	} 
	else 
	{
		var obj = getStyleObject(myObject);
		if (isOP5Browser()) 
			obj.background 		= colour;	
		else 
			obj.backgroundColor = colour;
	}
}

function setBGColorActive(myObject)
{
	//alert('--entered setBGColorActive()');
	if (myObject != null)
		changeBGColour(myObject, g_colorActive);
}

function setBGColorInactive(myObject)
{
	if (myObject != null)
		changeBGColour(myObject, g_colorInactive);
}

//function to check whether using an NS4 type browser:
function isNS4Browser() 
{
	//alert('--checking browser type!');
	return (document.layers);
}

function isOP5Browser() 
{
	return ((navigator.userAgent.indexOf("Opera 5")!=-1)||(navigator.userAgent.indexOf("Opera/5")!=-1));
}

function PositionCenter(menuID, labelID) 
{
	if (menuID != null)
	{
		var x_parent 		= getElementLeft(labelID);
		var y_parent 		= getElementTop(labelID);
		var height_parent 	= getElementHeight(labelID); 
		
		x = x_parent;
		y = y_parent + height_parent;
		
		//move this menu popup to required position:
		moveXY(menuID, x, y);
	}
}

function PositionRight(menuID, labelID) 
{
	//alert('--entered PositionRight()');
	if (menuID != null)
	{
		var x_parent 		= getElementLeft(labelID);
		var y_parent 		= getElementTop(labelID);
		var width_parent 	= getElementWidth(labelID); 
		
		//calculate position for this menu popup:
		x = x_parent + width_parent; 
		y = y_parent;
			
		//move this menu popup to required position:
		moveXY(menuID, x, y);
	}
}

function PositionLeft(menuID, labelID) 
{
	//alert('--entered PositionLeft()');
	if (menuID != null)
	{
		var x_parent 		= getElementLeft(labelID);
		var y_parent 		= getElementTop(labelID);
		var width_item 		= getElementWidth(menuID); 
		
		//calculate position for this menu popup:
		x = x_parent - width_item; 
		y = y_parent;
			
		//move this menu popup to required position:
		moveXY(menuID, x, y);
	}
}

//function to hide menu:
function deactivateAllMenus()
{
	//alert('--entered deactivateAllMenus()');
	
	//reinitialize timer:
	g_TimerID = 0;
	
	//remove all levels from the active stack:
	for (var i = g_nActive-1; i >= 0; i--)
	{
		MakeInvisible(g_ActiveChildMenuIDs[i]);
		setBGColorInactive(g_ActiveItemIDs[i]);
		g_ActiveMenuIDs[i] 		= null;	
		g_ActiveItemIDs[i] 		= null;	
		g_ActiveChildMenuIDs[i] = null;
	}
	g_nActive = 0;
}

function stopfadeoutMenu()
{
	//alert('--entered stopfadeoutMenu()');
	
	//clear fade out timer, if any:
	if (g_TimerID)
	{
		clearTimeout(g_TimerID);
		g_TimerID = 0;
	}
}

//function to fade out a menu:
function fadeoutMenu() 
{
	//alert('--entered fadeoutMenu()');
	
	//set timer to execute menu hiding after some period of time:
	if (!g_TimerID)
		g_TimerID = setTimeout('deactivateAllMenus()', g_TimeDelay);
}

function Activate(ThisMenuID,ThisItemID,ThisChildMenuID,Relative)
{
	//if there was a timer set, reset it:
	stopfadeoutMenu();
	
	//if this is the very first activation, simply add first level to the active stack: 
	if (g_nActive == 0)
	{
		//alert('--adding first lavel to the active stack');
		
		g_ActiveMenuIDs[0] 				= ThisMenuID;	
		g_ActiveItemIDs[0] 				= ThisItemID;	
		g_ActiveChildMenuIDs[0] 		= ThisChildMenuID;
		
		//***decided not to highlight items in the menu bar
		//setBGColorActive(g_ActiveItemIDs[0]);

		if (Relative == "below")
			PositionCenter(g_ActiveChildMenuIDs[0],g_ActiveItemIDs[0]);	
		else if (Relative == "right")
			PositionRight(g_ActiveChildMenuIDs[0],g_ActiveItemIDs[0]);
		else if (Relative == "left")	
			PositionLeft(g_ActiveChildMenuIDs[0],g_ActiveItemIDs[0]);

		MakeVisible(g_ActiveChildMenuIDs[0]);

		g_nActive=1;				
	}
	
	//make sure not trying to double-activate the same item:
	else if (ThisItemID != g_ActiveItemIDs[g_nActive-1])
	{
		//activating another item at the same level:
		if (ThisMenuID == g_ActiveMenuIDs[g_nActive-1])
		{
			//replace top level on the stack of active items
			
			//alert('--activating another item at the same level of the active stack');
			
			MakeInvisible(g_ActiveChildMenuIDs[g_nActive-1]);
			
			if (ThisMenuID != g_MenuBarID)
				//***decided not to highlight items in the menu bar
				setBGColorInactive(g_ActiveItemIDs[g_nActive-1]);
			
			g_ActiveItemIDs[g_nActive-1] 		= ThisItemID;
			g_ActiveChildMenuIDs[g_nActive-1] 	= ThisChildMenuID;
			
			if (ThisMenuID != g_MenuBarID)
				//***decided not to highlight items in the menu bar
				setBGColorActive(g_ActiveItemIDs[g_nActive-1]);
			
			if (Relative == "below")
				PositionCenter(g_ActiveChildMenuIDs[g_nActive-1],g_ActiveItemIDs[g_nActive-1]);	
			else if (Relative == "right")
				PositionRight(g_ActiveChildMenuIDs[g_nActive-1],g_ActiveItemIDs[g_nActive-1]);
			else if (Relative == "left")	
				PositionLeft(g_ActiveChildMenuIDs[g_nActive-1],g_ActiveItemIDs[g_nActive-1]);
	
			MakeVisible(g_ActiveChildMenuIDs[g_nActive-1]);		
		}
	
		//else if moved one level deeper in the menu stack:	
		else if (ThisMenuID == g_ActiveChildMenuIDs[g_nActive-1])	
		{
			//add one more level to the active items stack:
		
			//alert('--add one more level to the active items stack');
			
			g_ActiveMenuIDs[g_nActive] 			= ThisMenuID;	
			g_ActiveItemIDs[g_nActive] 			= ThisItemID;	
			g_ActiveChildMenuIDs[g_nActive] 	= ThisChildMenuID;
	
			setBGColorActive(g_ActiveItemIDs[g_nActive]);
	
			if (Relative == "below")
				PositionCenter(g_ActiveChildMenuIDs[g_nActive],g_ActiveItemIDs[g_nActive]);	
			else if (Relative == "right")
				PositionRight(g_ActiveChildMenuIDs[g_nActive],g_ActiveItemIDs[g_nActive]);
			else if (Relative == "left")	
				PositionLeft(g_ActiveChildMenuIDs[g_nActive],g_ActiveItemIDs[g_nActive]);
	
			MakeVisible(g_ActiveChildMenuIDs[g_nActive]);
	
			g_nActive++;
		}
	
		//else if moved up the active stack or to the new menu bar label:
		else if (ThisItemID != g_ActiveItemIDs[g_nActive-1])
		{
			//alert('--moved up the active stack');
			
			var StartBackTrack	= g_nActive;
			var bStop 			= false;
			for (var i = StartBackTrack-1; (i >= 0) && (!bStop); i--)
			{
				//if item is not at this level, then it must be below this level
				//must remove this level from the active stack:	
				if (g_ActiveMenuIDs[i] != ThisMenuID)
				{
					//alert('--removing active stack level: '+i);
					
					MakeInvisible(g_ActiveChildMenuIDs[i]);
	
					setBGColorInactive(g_ActiveItemIDs[i]);
	
					g_ActiveMenuIDs[i] 		= null;	
					g_ActiveItemIDs[i] 		= null;	
					g_ActiveChildMenuIDs[i] = null;
		
					g_nActive--;
				}
				//else if found the level of the item:
				else 
				{
					//if pointing at one of the already active items, keep that item in the stack
					//and do nothing else:
					if((ThisItemID==g_ActiveItemIDs[i])&&(ThisChildMenuID=g_ActiveChildMenuIDs[i]))
					{
						//alert('--pointing at one of the already active items at level: '+i);
						bStop = true;
					}
					//else must be pointing at other item at that level;
					//must replace previously active item at that level with a new one:
					else
					{
						//alert('--replace previously active item with a new one at level: '+i);
						
						MakeInvisible(g_ActiveChildMenuIDs[i]);
	
						if (ThisMenuID != g_MenuBarID)
							//***decided not to highlight items in the menu bar
							setBGColorInactive(g_ActiveItemIDs[i]);
			
						g_ActiveItemIDs[i] 			= ThisItemID;
						g_ActiveChildMenuIDs[i] 	= ThisChildMenuID;
		
						if (ThisMenuID != g_MenuBarID)
							//***decided not to highlight items in the menu bar
							setBGColorActive(g_ActiveItemIDs[i]);
			
						if (Relative == "below")
							PositionCenter(g_ActiveChildMenuIDs[i],g_ActiveItemIDs[i]);	
						else if (Relative == "right")
							PositionRight(g_ActiveChildMenuIDs[i],g_ActiveItemIDs[i]);
						else if (Relative == "left")	
							PositionLeft(g_ActiveChildMenuIDs[i],g_ActiveItemIDs[i]);
	
						MakeVisible(g_ActiveChildMenuIDs[i]);
	
						bStop = true;
					}
				}			
			}	
		}
	}
}

function menuObject(menuID,itemWidth) 
{
	this.m_ID					= menuID;
	this.m_itemWidth			= itemWidth;
	this.m_numItems 			= 0;
	this.m_arrayItemsIDs		= new Array();
	this.m_arrayItemsText		= new Array();
	this.m_arrayURLs			= new Array();
	this.m_arraySubmenuIDs		= new Array();
		
	this.addMenuItem = function(itemID, itemText, itemURL, submenuID) 
	{
		this.m_arrayItemsIDs[this.m_numItems]			= itemID;
		this.m_arrayItemsText[this.m_numItems] 			= itemText;
		this.m_arrayURLs[this.m_numItems] 				= itemURL;
		this.m_arraySubmenuIDs[this.m_numItems]			= submenuID;
		
		//alert('--adding menu item with id: '+this.m_arrayItemsIDs[this.m_numItems]);
		this.m_numItems++;
	}
	
	this.writeMenu = function() 
	{
		var szTestTableStr 		= new String('');
		var szTestItemRowStr 	= new String('');
		var i					= 0;
		
		//start to draw the table:
		szTestTableStr += '<DIV ID="'+this.m_ID+'" class="Menu_class">\n';
		szTestTableStr += '<TABLE border="0" cellpadding="1" cellspacing="0" bgcolor="'+g_colorBorder+'">\n';
		szTestTableStr += '<TR><TD>\n';
		szTestTableStr += '<TABLE border="0" cellpadding="0" cellspacing="0"  width="100%" height="100%" hspace="0" vspace="0">\n';		
		for (i = 0; i < this.m_numItems; i++)
		{ 
			//alert('--adding menu item with id: '+this.m_arrayItemsIDs[i]);
			szTestItemRowStr = '';
			szTestItemRowStr += '<TR>\n';
			
			if (isNS4Browser())
			{
				szTestItemRowStr += '<TD class="MenuText_class" bgcolor="'+g_colorInactive+'" width="'+this.m_itemWidth+'">\n';
				szTestItemRowStr += '<ILAYER>\n';
				szTestItemRowStr += '<LAYER ID="'+this.m_arrayItemsIDs[i]+'" width="100%" height="100%"\n';
			}
			else
				szTestItemRowStr += '<TD ID="'+this.m_arrayItemsIDs[i]+'" class="MenuText_class" bgcolor="'+g_colorInactive+'" width="'+this.m_itemWidth+'"\n';
			
			if (this.m_arraySubmenuIDs[i])
				szTestItemRowStr += 'onMouseover="Activate(\''+this.m_ID+'\',\''+this.m_arrayItemsIDs[i]+'\',\''+this.m_arraySubmenuIDs[i]+'\',\'right\');"\n';
			else
				szTestItemRowStr += 'onMouseover="Activate(\''+this.m_ID+'\',\''+this.m_arrayItemsIDs[i]+'\',null,\'right\');"\n';
			szTestItemRowStr += 'onMouseout="fadeoutMenu();"\n';
			szTestItemRowStr += '>\n';//close layer or td start tag
				
			if (this.m_arrayURLs[i] != null)
				szTestItemRowStr += '<a class="inmenu" href="'+this.m_arrayURLs[i]+'">'+this.m_arrayItemsText[i]+'</a>\n';
			else
				szTestItemRowStr += this.m_arrayItemsText[i]+'\n';

			if ((this.m_arraySubmenuIDs[i] != null)&&(g_SubmenuImageMarkerSrc != null))
				szTestItemRowStr += '<img src="'+g_SubmenuImageMarkerSrc+'" border="0" align="absmiddle">\n';
			
			if (isNS4Browser())
			{		
				szTestItemRowStr += '</LAYER>\n';
				szTestItemRowStr += '</ILAYER>\n';
			}
			
			szTestItemRowStr += '</TD>\n';
			szTestItemRowStr += '</TR>\n';
			//alert('Going to add a table row:\n'+szTestItemRowStr);
			szTestTableStr += szTestItemRowStr;
		}
		szTestTableStr += '</TABLE>\n';
		szTestTableStr += '</TD></TR></TABLE>\n';
		szTestTableStr += '</DIV>\n';
		//end drawing of the table
		
		//alert('writing a table :\n'+szTestTableStr);
		document.write(szTestTableStr);
	}
}

function menuBar(barID,labelWidth,alignHorz) 
{
	g_MenuBarID				= barID;
	this.m_ID				= barID;
	this.m_labelWidth		= labelWidth;
	this.m_alignHorz		= alignHorz;
	this.m_numLabels 		= 0;
	this.m_arrayLabelsIDs	= new Array();
	this.m_arrayMenusIDs	= new Array();
	this.m_arrayLabelsText	= new Array();
	this.m_arrayURLs		= new Array();
	
	this.addLabel = function(lebelID, labelText, labelURL, menuID) 
	{
		this.m_arrayLabelsIDs[this.m_numLabels] 	= lebelID;
		this.m_arrayLabelsText[this.m_numLabels] 	= labelText;
		this.m_arrayURLs[this.m_numLabels] 			= labelURL;
		this.m_arrayMenusIDs[this.m_numLabels] 		= menuID;
		//alert('--adding a lebel with id: '+this.m_arrayLabelsIDs[this.m_numLabels]);
		this.m_numLabels++;
	}
	
	this.writeMenuBar = function() 
	{
		var szTestTableStr 		= new String('');
		var szTestLabelColStr 	= new String('');
		var i					= 0;
				
		//start to draw the table:
		szTestTableStr += '<DIV ID="'+this.m_ID+'">\n';
		szTestTableStr += '<TABLE width="'+((this.m_numLabels*labelWidth)+this.m_numLabels+1)+'px"border="0" cellpadding="0" cellspacing="0"  bgcolor="'+g_colorBorder+'">\n';
		szTestTableStr += '<TR><TD>\n';
		szTestTableStr += '<TABLE border="0" cellpadding="0" cellspacing="1"  width="100%" height="100%">\n';
		szTestTableStr += '<TR>\n';
		
		for (i = 0; i < this.m_numLabels; i++)
		{ 
			//alert('--adding a lebel with id: '+this.m_arrayLabelsIDs[i]);
			szTestLabelColStr = '';
			if (isNS4Browser())
			{
				szTestLabelColStr += '<TD class="MenuText_class" align="'+this.m_alignHorz+'" bgcolor="'+g_colorInactive+'" width="'+this.m_labelWidth+'">\n';
				szTestLabelColStr += '<ILAYER>\n';
				szTestLabelColStr += '<LAYER ID="'+this.m_arrayLabelsIDs[i]+'" width="100%" height="100%"\n';
				
				if (this.m_arrayMenusIDs[i])
					szTestLabelColStr += 'onMouseover="Activate(\''+this.m_ID+'\',\''+this.m_arrayLabelsIDs[i]+'\',\''+this.m_arrayMenusIDs[i]+'\',\'below\');"\n';
				else
					szTestLabelColStr += 'onMouseover="Activate(\''+this.m_ID+'\',\''+this.m_arrayLabelsIDs[i]+'\',null,\'below\');"\n';
				szTestLabelColStr += 'onMouseout="fadeoutMenu();"\n';
				szTestLabelColStr += '>\n';//close layer or td start tag
				
				if (this.m_alignHorz == 'center')
					szTestLabelColStr += '<center>\n';
				if (this.m_arrayLabelsText[i] != null)
				{
					if (this.m_arrayURLs[i] != null)
						szTestLabelColStr += '<a class="inmenu" href="'+this.m_arrayURLs[i]+'">'+this.m_arrayLabelsText[i]+'</a>\n';
					else
						szTestLabelColStr += this.m_arrayLabelsText[i]+'\n';
				}
				if (this.m_alignHorz == 'center')
					szTestLabelColStr += '</center>\n';
				
				szTestLabelColStr += '</LAYER>\n';
				szTestLabelColStr += '</ILAYER>\n';
			}
			else
			{
				szTestLabelColStr += '<TD ID="'+this.m_arrayLabelsIDs[i]+'" class="MenuText_class" align="'+this.m_alignHorz+'" bgcolor="'+g_colorInactive+'" width="'+this.m_labelWidth+'"\n';
				
				if (this.m_arrayMenusIDs[i])
					szTestLabelColStr += 'onMouseover="Activate(\''+this.m_ID+'\',\''+this.m_arrayLabelsIDs[i]+'\',\''+this.m_arrayMenusIDs[i]+'\',\'below\');"\n';
				else
					szTestLabelColStr += 'onMouseover="Activate(\''+this.m_ID+'\',\''+this.m_arrayLabelsIDs[i]+'\',null,\'below\');"\n';
				szTestLabelColStr += 'onMouseout="fadeoutMenu();"\n';
				szTestLabelColStr += '>\n';//close layer or td start tag
				
				if (this.m_alignHorz == 'center')
					szTestLabelColStr += '<center>\n';
				if (this.m_arrayLabelsText[i] != null)
				{
					if (this.m_arrayURLs[i] != null)
						szTestLabelColStr += '<a class="inmenu" href="'+this.m_arrayURLs[i]+'">'+this.m_arrayLabelsText[i]+'</a>\n';
					else
						szTestLabelColStr += this.m_arrayLabelsText[i]+'\n';
				}
				if (this.m_alignHorz == 'center')
					szTestLabelColStr += '</center>\n';
			}
			
			szTestLabelColStr += '</TD>\n';
			
			//alert('Going to add a table column:\n'+szTestLabelColStr);
			szTestTableStr += szTestLabelColStr;
		}
		szTestTableStr += '</TR>\n';
		szTestTableStr += '</TABLE>\n';
		szTestTableStr += '</TD></TR></TABLE></DIV>\n';
		//end drawing of the table
		
		//alert('--drawing a table:\n'+szTestTableStr);
		document.write(szTestTableStr);
	
	}
}

