var aMenus = new Array;
var objPrevHover;			// Used to reset styles on previously hovered menu item.
var objPrevSubHover;
var objMenu;
var bHovering = false;
document.onmouseover = fnClear;

function navMenu()
{
	// Set up the methods for the menu object.
	this.setWidths = fnSetWidths;
	this.setFonts = fnSetFonts;
	this.setBackgrounds = fnSetBackgrounds;
	this.setBorders = fnSetBorders;
	this.addnew = fnCreateMenuItem;
	this.create = fnCreateMenu;
	this.resetHovers = fnResetHovers;

	// Return the menu object.
	objMenu = this;
	return this;
}
function fnSetWidths(sMenuWidth, sSubMenuWidth, sSpacing, bHorizontal)
{
	this.menuWidth = sMenuWidth;
	this.subMenuWidth = sSubMenuWidth;
	this.menuSpacing = sSpacing;
	this.horizontal = bHorizontal;
}
function fnSetFonts(sFontName, sFontSize, sSubFontSize, sColour, sColour_Hover, sSubColour, sSubColour_Hover)
{
	this.menuFont = sFontName;
	this.menuFontSize = sFontSize;
	this.menuSubFontSize = sSubFontSize;
	this.menuColour = sColour;
	this.menuColourHover = sColour_Hover;
	this.subMenuColour = sSubColour;
	this.subMenuColourHover = sSubColour_Hover;
}
function fnSetBackgrounds(sBgColour, sBgColour_Hover, sSubBgColour, sSubBgColour_Hover)
{
	this.menuBackground = sBgColour;
	this.menuBackgroundHover = sBgColour_Hover;
	this.subMenuBackground = sSubBgColour;
	this.subMenuBackgroundHover = sSubBgColour_Hover;
}
function fnSetBorders(sBorderWidth, sBorder, sBorder_Hover, sSubBorder, sSubBorder_Hover)
{
	this.menuBorderWidth = sBorderWidth;
	this.menuBorder = sBorder;
	this.menuBorderHover = sBorder_Hover;
	this.subMenuBorder = sSubBorder;
	this.subMenuBorderHover = sSubBorder_Hover;
}
function fnCreateMenuItem(sText, sHREF, sWidth)
{
	// Create a menu item object and add 
	// it to the menus array (aMenus).
	var aMenuItem = new objMenuItem;
	aMenuItem.text = sText;
	aMenuItem.href = sHREF;
	aMenuItem.width = sWidth;
	aMenuItem.subItems = new Array;
	// Add the menu item object to 
	// the array of menu items.
	aMenus[aMenus.length] = aMenuItem;
	// Return the created menu item.
	return aMenuItem;	
}

function objMenuItem()
{
	/* These are the properties of the menuitem object.
	this.text;
	this.href;
	this.subItems;
	*/
	this.addnew = fnCreateSubMenuItem;
}

function fnCreateSubMenuItem(sText, sHREF)
{
	// Create a new sub item object.
	var newsub = new objSubMenuItem;
	newsub.text = sText;
	newsub.href = sHREF;
	newsub.parent = this;
	// Add the sub item object to the 
	// subitems array of this menu item.
	this.subItems[this.subItems.length] = newsub;
	// Return the new sub item (for the hell of it);
	return newsub;
}

function objSubMenuItem()
{
	/* These are the properties of the submenuitem object.
	this.text;
	this.href;
	*/
}

function fnCreateMenu(HTMLElement)
{
	objMenu.container = HTMLElement;
	HTMLElement.innerHTML = "";
	
	for(i=0;i<aMenus.length;i++)
	{
		if(this.horizontal==true)
		{
			var newDIV = document.createElement("SPAN")
			newDIV.style.borderTopWidth = objMenu.menuBorderWidth;
			newDIV.style.borderBottomWidth = objMenu.menuBorderWidth;
			if(i==0)
			{
				newDIV.style.borderLeftWidth = objMenu.menuBorderWidth;
				newDIV.style.borderRightWidth = objMenu.menuBorderWidth * 0.5;
			}
			else if(i==aMenus.length-1)
			{
				newDIV.style.borderLeftWidth = objMenu.menuBorderWidth * 0.5;
				newDIV.style.borderRightWidth = objMenu.menuBorderWidth;
			}
			else
			{
				newDIV.style.borderLeftWidth = objMenu.menuBorderWidth * 0.5;
				newDIV.style.borderRightWidth = objMenu.menuBorderWidth * 0.5;
			}
		}
		else
		{
			var newDIV = document.createElement("DIV")
			newDIV.style.borderLeftWidth = objMenu.menuBorderWidth;
			newDIV.style.borderRightWidth = objMenu.menuBorderWidth;
			if(i==0)
			{
				newDIV.style.borderTopWidth = objMenu.menuBorderWidth;
				newDIV.style.borderBottomWidth = objMenu.menuBorderWidth * 0.5;
			}
			else if(i==aMenus.length-1)
			{
				newDIV.style.borderTopWidth = objMenu.menuBorderWidth * 0.5;
				newDIV.style.borderBottomWidth = objMenu.menuBorderWidth;
			}
			else
			{
				newDIV.style.borderTopWidth = objMenu.menuBorderWidth * 0.5;
				newDIV.style.borderBottomWidth = objMenu.menuBorderWidth * 0.5;
			}
		}
		// Text
		newDIV.innerText = aMenus[i].text;
		// Events
		newDIV.onmouseover = fnMenu_onMouseOver;
		newDIV.onclick = fbMenu_onClick;
		// Styles
		newDIV.style.fontFamily = objMenu.menuFont;
		newDIV.style.fontSize = objMenu.menuFontSize;
		newDIV.style.color = objMenu.menuColour;
		newDIV.style.background = objMenu.menuBackground;
		//newDIV.style.width = objMenu.menuWidth;
		newDIV.style.cursor = "hand";
		// Default styles
		//newDIV.style.padding = objMenu.menuSpacing;
		newDIV.style.paddingTop = "20px";
		newDIV.style.paddingBottom = "3px";		
		newDIV.style.paddingLeft = "20px";
		newDIV.style.paddingRight = "30px";		
		newDIV.textAlign = "Center";

		newDIV.style.margin = 0;
		
		newDIV.style.borderColor = objMenu.menuBorder;
		newDIV.style.borderLeftStyle = "solid";
		HTMLElement.appendChild(newDIV);

		aMenus[i].HTMLElement = newDIV;
		
	}

	// Create the sub menu container.
	var newSPAN = document.createElement("SPAN")
	newSPAN.style.padding = 0;
	newSPAN.style.margin = 0;
	newSPAN.style.position = "absolute";
	newSPAN.style.display = "none";
	HTMLElement.appendChild(newSPAN);
	objMenu.subContainer = newSPAN;
	
	return false;
}

function fnResetHovers()
{
	for(i=0;i<aMenus.length;i++)
	{
		aMenus[i].HTMLElement.style.color = objMenu.menuColour;
		aMenus[i].HTMLElement.style.background = objMenu.menuBackground;
		aMenus[i].HTMLElement.style.borderColor = objMenu.menuBorder;
	}
	objMenu.subContainer.innerHTML = "";
}

function fnMenu_onMouseOver()
{
	bHovering = true
	// Clear the hover effect on all menu items.
	if(objPrevHover)
	{
		objPrevHover.style.color = objMenu.menuColour;
		objPrevHover.style.background = objMenu.menuBackground;
		//objPrevHover.style.width = objMenu.menuWidth;
		objPrevHover.style.borderColor = objMenu.menuBorder;
		objMenu.subContainer.innerHTML = "";
	}
	
	// Change the style of the item being hovered.
	var src = event.srcElement;
	src.style.color = objMenu.menuColourHover;
	src.style.background = objMenu.menuBackgroundHover;
	//src.style.width = objMenu.menuWidth;
	src.style.borderColor = objMenu.menuBorderHover;

	
	var aMenuItem = fnGetMenuItem(src);
	
	if(aMenuItem)
	{
		objMenu.subContainer.innerHTML = "";
		//alert(objMenu.subContainer);
		var subs = aMenuItem.subItems;
		for(i=0;i<subs.length;i++)
		{
			var newDIV = document.createElement("DIV")

			newDIV.innerText = subs[i].text;

			newDIV.onmouseover = fnSubMenu_onMouseOver;
			newDIV.onclick = fbSubMenu_onClick;

			newDIV.style.fontSize = objMenu.menuSubFontSize;
			newDIV.style.fontFamily = objMenu.menuFont;
			newDIV.style.color = objMenu.subMenuColour;
			newDIV.style.background = objMenu.subMenuBackground;
			//newDIV.style.width = objMenu.subMenuWidth;
			newDIV.style.width = aMenuItem.width
			//alert(aMenuItem.width);
			newDIV.style.paddingLeft = "10px";
			newDIV.style.paddingRight = "10px";
			newDIV.style.paddingTop = "2px";
			newDIV.style.paddingBottom = "2px";
			//newDIV.align = "center";
			newDIV.style.cursor = "hand";

			//newDIV.style.padding = objMenu.menuSpacing;
			newDIV.style.margin = 0;

			newDIV.style.borderStyle = "solid";
			newDIV.style.borderColor = objMenu.subMenuBorder;
			newDIV.style.borderLeftWidth = objMenu.menuBorderWidth;
			newDIV.style.borderRightWidth = objMenu.menuBorderWidth;
			if(i==0)
			{
				newDIV.style.borderTopColor = "white";
				newDIV.style.borderTopWidth = objMenu.menuBorderWidth;
				newDIV.style.borderBottomWidth = objMenu.menuBorderWidth * 0.5;
			}
			else if(i==subs.length-1)
			{
				newDIV.style.borderTopWidth = objMenu.menuBorderWidth * 0.5;
				newDIV.style.borderBottomWidth = objMenu.menuBorderWidth;
			}
			else
			{
				newDIV.style.borderTopWidth = objMenu.menuBorderWidth * 0.5;
				newDIV.style.borderBottomWidth = objMenu.menuBorderWidth * 0.5;
			}
		
			objMenu.subContainer.appendChild(newDIV);
			subs[i].HTMLElement = newDIV;

		}
		if(objMenu.subContainer.children.length>0)
		{
			if(objMenu.horizontal==true)
			{
				objMenu.subContainer.style.left = aMenuItem.HTMLElement.offsetLeft + document.getElementById("tdMain").offsetLeft + document.getElementById("tblMain").offsetLeft;
				objMenu.subContainer.style.top = aMenuItem.HTMLElement.offsetTop + aMenuItem.HTMLElement.offsetHeight - objMenu.menuBorderWidth + document.getElementById("tdMain").offsetTop + 16;

			}
			else
			{
				objMenu.subContainer.style.left = aMenuItem.HTMLElement.offsetWidth + aMenuItem.HTMLElement.offsetLeft - objMenu.menuBorderWidth;
				objMenu.subContainer.style.top = aMenuItem.HTMLElement.offsetTop + aMenuItem.HTMLElement.offsetHeight;
			}

			// Display the subs:
			fnSlideShowSubs();
		}
		else
		{
			objMenu.subContainer.style.display = "none";
		}
	}
	
	objPrevHover = src;
	
}

function fnGetMenuItem(objElement)
{
	for(i=0;i<aMenus.length;i++)
	{
		if(aMenus[i].HTMLElement==objElement)
		{
			var aMenuItem = new objMenuItem;
			aMenuItem = aMenus[i];
			return aMenuItem;
		}
	}
}
function fnGetSubMenuItem(objElement)
{
	for(y=0;y<aMenus.length;y++)
	{
		var aMenuItem = aMenus[y];
		for(i=0;i<aMenuItem.subItems.length;i++)
		{
			if(aMenuItem.subItems[i].HTMLElement==objElement)
			{
				var aSubMenuItem = new objSubMenuItem;
				aSubMenuItem = aMenuItem.subItems[i];
				return aSubMenuItem;
			}
		}
	}
}
function fbMenu_onClick()
{
	// Navigate to a link?
	var src = event.srcElement;
	var aMenuItem = fnGetMenuItem(src);
	if(aMenuItem && aMenuItem.href!="")
	{
		document.location.href=aMenuItem.href;
	}
}
function fbSubMenu_onClick()
{
	// Navigate to a link?
	var src = event.srcElement;
	var aSubMenuItem = fnGetSubMenuItem(src);
	if(aSubMenuItem && aSubMenuItem.href!="")
	{
		document.location.href=aSubMenuItem.href;
	}
}
function fnSubMenu_onMouseOver()
{
	if(objPrevSubHover)
	{
		objPrevSubHover.style.color = objMenu.subMenuColour;
		objPrevSubHover.style.background = objMenu.subMenuBackground;
		//objPrevSubHover.style.width = objMenu.subMenuWidth;
		objPrevSubHover.style.borderColor = objMenu.subMenuBorder;
		objPrevSubHover.style.borderTopColor = "white";
	}
	var src = event.srcElement;
	src.style.color = objMenu.subMenuColourHover;
	src.style.background = objMenu.subMenuBackgroundHover;
	//src.style.width = objMenu.subMenuWidth;
	src.style.borderColor = objMenu.subMenuBorderHover;
	src.style.borderTopColor = "white";
	objPrevSubHover = src;
}

function fnClear()
{
	var src = event.srcElement;
	if(objMenu){
	if(src.tagName!="SPAN"&&src.tagName!="DIV"){objMenu.resetHovers()}
	}
}

function fnSlideShowSubs()
{
	if(objMenu.horizontal==true)
	{
		objMenu.subContainer.style.display = "";
	}
	else
	{
		for(i=0;i<objMenu.subContainer.children.length;i++)
		{
			objMenu.subContainer.children[i].style.width = 0;
			objMenu.subContainer.children[i].style.overflow = "hidden";
		}
		objMenu.subContainer.style.display = "";
		//fnSlideShowSubs_Expand();
	}
}

//function fnTesting()
//{
//objMenu.subContainer.style.display = "";
//}

function fnSlideShowSubs_Expand()
{
	var subs = objMenu.subContainer.children;
	if(subs.length > 0)
	{
		for(i=0;i<subs.length;i++)
		{
			subs[i].style.width = subs[i].offsetWidth + 5;
		}
		if(subs[0].offsetWidth<objMenu.subMenuWidth)
		{
			setTimeout(fnSlideShowSubs_Expand,10);
		}
	}
}
