//start of data
var myDelay=250;
var myTimeOutID=-1;
var currentSubMenu=null;
var currentMenuItem=null;
var verticalMenu=true;
var mainmenu="mainmenu";
var linkclass="amenu1";
// these all relate to the class amenu1 and should be found only once by using findLinkColours
var linkBackgroundColour=null;
var linkColour=null;
var linkActiveBackgroundColour=null;
var linkActiveColour=null;
var triedFindingColours=false;
// this could be a member function of mainmenu object in better organised code
function findLinkColours()
{
// find the active colour and background colour to go with the link class and save these to global variables
	var rule=null;
	var rules=null;
	var styles=null;
	var i=0;
	var activeColour;
	// this is the linkclass used for the main menu
	var foundactivecolours=false;
	var foundcolours=false;
	// remember we have done this once so we don't keep trying if it didn't work
	triedFindingColours=true;
	if(document.styleSheets && document.styleSheets.length>0)
	{
		if(document.styleSheets[0].cssRules)
		{
			rules=document.styleSheets[0].cssRules;
		}
		else if(document.styleSheets[0].rules)
		{
			rules=document.styleSheets[0].rules;
		}	
		if(rules)
		{		
			for(i=0;i<rules.length;i++)
			{
				rule=rules[i];
				if(rule.type==rule.STYLE_RULE)
				{
					if(rule.selectorText=="a."+linkclass+":active" || rule.selectorText=="A."+linkclass+":active" )
					{
						styles=rule.style;
						if(styles)
						{
							//works with IE and Firefox although apparently it should use getPropertyCSSValue according to the book
							linkActiveBackgroundColour=styles.backgroundColor;
							linkActiveColour=styles.color;
							foundactivecolours=true;
						}
	
					}
					else if(rule.selectorText=="a."+linkclass || rule.selectorText=="A."+linkclass)
					{
						styles=rule.style;
						if(styles)
						{
							//works with IE and Firefox although apparently it should use getPropertyCSSValue according to the book
							linkBackgroundColour=styles.backgroundColor;
							linkColour=styles.color;
							foundcolours=true;
						}
					}
				}
				if(foundactivecolours && foundcolours)
				{
					break;
				}
			}
		}
	}
	return;
}
// not used but could be handy
function getElementStyle(elemID, IEStyleProp, CSSStyleProp) 
{
	if(document.getElementById)
	{
	    var elem = document.getElementById(elemID);
	    if (elem.currentStyle) 
		{
	        return elem.currentStyle[IEStyleProp];
	    } 
		else if (window.getComputedStyle) 
		{
	        var compStyle = window.getComputedStyle(elem, "");
	        return compStyle.getPropertyValue(CSSStyleProp);
	    }
	}
    return "";
}
function fontSizeChanged() 
{ 
	var currentWidth=null;
	var FontSizeChanged=false;
	if(document.getElementById)
	{
		var element=document.getElementById(this.id);
		if(element)
		{
			currentWidth=element.offsetWidth;
		}
		if(!this.Width)
		{
			this.Width=currentWidth;
		}
		else
		{
			if(this.Width != currentWidth)
			{
				FontSizeChanged=true;
				this.Width=currentWidth;
			}
		}
	}
	return FontSizeChanged;
}

//If the mouse over event is over on of the main menu items then make the submenu visible
function changeMenuVisibility(menuListItem)
{
	clearTimeout(myTimeOutID);
	var found=false;
	var newlocation=null;
	var menuitems=null;
	var submenuid="";
	var subMenu=null;
	var newId;
	var possibleSubMenu=null;
	if(document.getElementById)
	{
		var mainmenu=document.getElementById("mainmenu");
		if(mainmenu && mainmenu.getElementsByTagName)
		{
			menuitems=mainmenu.getElementsByTagName("li");
		}
		if(menuitems)
		{
			for(var i=0; i< menuitems.length;i++)
			{
				if(menuListItem.id==menuitems[i].id)
				{
					// set the function fontSizeChanged to be a member function
					if(!menuitems[i].fontSizeChanged)
					{
						menuitems[i].fontSizeChanged=fontSizeChanged;
					}
					// if the font size has changed then reload without getting information from the server
					if(menuitems[i].fontSizeChanged && menuitems[i].fontSizeChanged())
					{
						window.location.reload(false);
					}
					
					submenuid=menuListItem.id+"sub";
					subMenu=document.getElementById(submenuid);
					// we have have a mouse over event on the main menu so make the submenu visible
					if(subMenu)
					{					
						//remember the active background color and text colour into globals
						// and the non-active ones so that we can set it back
						if(!triedFindingColours)
						{
						 	findLinkColours()
						}
						subMenu.style.visibility="visible";
						// change the colours of the current menu item to the active colours
						// change the colours of the current menu item to the active colours
						found=true;
					}
					if(currentMenuItem && currentMenuItem !=menuitems[i] && linkBackgroundColour  && linkColour )
					{
						changeMenuItemAnchorColours(currentMenuItem, linkBackgroundColour, linkColour);
						currentMenuItem=null;
					}
					if(subMenu && linkActiveColour && linkActiveBackgroundColour)
					{
						currentMenuItem=menuitems[i];
						changeMenuItemAnchorColours(currentMenuItem, linkActiveBackgroundColour, linkActiveColour);
					}
					continue;
				}
			}
		}
		if(found)
		{
			for(var i=0; i< menuitems.length;i++)
			{
				// check it isn't the submenu that we have 
				// just found
				newId=menuitems[i].id+"sub";
				if(subMenu.id!=newId)
				{
					possibleSubMenu=document.getElementById(newId);
					if(possibleSubMenu)
					{
						possibleSubMenu.style.visibility="hidden";
					}
				}
			}
		}
		// the mouse is over one of the submenus - remember which one
		if(!found)
		{
			// find the parent menu item and set its colours to active
			var subposition = menuListItem.id.indexOf("sub"); 
			var menuitemid="";
			if(subposition>-1)
			{
				currentSubMenu=menuListItem;
				menuitemid=menuListItem.id.substring(0,subposition);
				currentMenuItem=document.getElementById(menuitemid);
				if(currentMenuItem && linkActiveBackgroundColour && linkActiveColour )
				{
					changeMenuItemAnchorColours(currentMenuItem, linkActiveBackgroundColour, linkActiveColour);
				}
			}
		}
	}
}

function clearMenu()
{
	if(document.getElementsByTagName)
	{
		var lists = document.getElementsByTagName("ul");
	
	// this clears all the submenus
		if(lists)
		{
			for(var i=0; i< lists.length;i++)
			{
				// check that it is a submenu
				pos=lists[i].id.indexOf("sub");
				// make all the submenus hidden
				if(pos>-1)
				{	
					lists[i].style.visibility="hidden";
				}
			}
		}
	}
	if(currentMenuItem && linkBackgroundColour  && linkColour )
	{
		changeMenuItemAnchorColours(currentMenuItem, linkBackgroundColour, linkColour);
		currentMenuItem=null;
	}
}
function changeMenuItemAnchorColours(itemToChange, newBackgroundColour, newColour)
{
	if(itemToChange && itemToChange.getElementsByTagName)
	{
		var anchors = itemToChange.getElementsByTagName("a");
		if(anchors && anchors.length>0)
		{
			// assume that there is only one
			anchors[0].style.backgroundColor=newBackgroundColour;
			anchors[0].style.color=newColour;
		}
	}
}
function clearSubMenu()
{
	if(currentSubMenu)
	{
		// hide the current menu
		currentSubMenu.style.visibility="hidden";
	}
	if(currentMenuItem && linkBackgroundColour && linkColour )
	{
		changeMenuItemAnchorColours(currentMenuItem, linkBackgroundColour, linkColour)
	}
	currentSubMenu=null;
	currentMenuItem=null;
}
// utility functions - also in ticker.js
function findPosX(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;
}
function findPosY(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;
}
function getItemPos2(id, verticalMenu)
{
	//if(navigator.userAgent.indexOf("Firefox") != -1 || navigator.userAgent.indexOf("Netscape") != -1)
	//{
	//	return getItemPos2(id,verticalMenu);
	//}
	var p1=0;
	if(document.getElementById)
	{
		var element=document.getElementById(id);
		if(verticalMenu)
		{
			p1=element.offsetTop;
		}
		else
		{
			p1 = element.offsetTop-2;
		}
	}
	var offsetLeft = 0;
	var position= "position:absolute;top:"+(p1)+"px;left:"+(offsetLeft)+"px";
	return position;
}
// this finds the item position if outside of the div that we are relating the position to and it
// probably has something to do with whether it is absolute or relative
function getItemPos(id, verticalMenu)
{
	var p1=0;
	var offsetLeft=0;
	var position="";
	if(document.getElementById)
	{
		var element=document.getElementById(id);
		if(verticalMenu)
		{
			p1=findPosY(element);
		}
		else
		{
			p1 = findPosY(element)-2;
		}
		offsetLeft = findPosX(element);
		if(verticalMenu)
		{
			offsetLeft+=element.offsetWidth;
		}
	}
	position= "position:absolute;top:"+(p1)+"px;left:"+(offsetLeft)+"px";
	return position;
}

