/*
********************ALL BELOW CODE IS THE PROPERTY OF QRUSH TECHNOLOGIES, INC. AND MAY NOT BE USED WITHOUT APPROVAL********************
Do not forget to include this in your HTML...
	<script language=JavaScript src=Menu.js></script>
Syntax: (All inside JavaScript, in place of Optional put either "null" or do not get to that object with ,s)
	To create a menu:
		MyMenu=new Menu(Optional BackColor, Optional HighlightColor, Optional TextHighlightColor, Optional BorderWidth, Optional BorderColor);
		MyMenu.AddItem(InnerHTML, Optional BackColor, Optional SubMenu);
		
	Extra functions:
		MyMenu.Display(Optional x, Optional y);
		MyMenu.Hide();
	At end of document HTML... 
		InitMenus();
	To create a main menu item/button
		WriteMainMenuButton(InnerItemHTML, MenuToShow, Optional NotSelectedBorderColor, Optional MyBorderWidth);
		
	Example:
		MyMenu=new Menu("#000000", "#00ff00", "#ffffff", 3, "#ff0000");
		MyMenu.AddItem("<a href=cnn.com>CNN</a>", "#ffff00");
		
		MySubMenu=new Menu();
		MySubMenu.AddItem("<a href=google.com>Google</a>");
		MySubMenu.AddItem("<a href=lycos.com>Lycos</a>");
		
		MyMenu.AddItem(Search Engines, , MySubMenu);
		
		WriteMainMenuButton("Links!", MyMenu);
Notes:
	Do NOT nest MenuA in MenuB in MenuA (recursion)
	Do NOT nest MenuB twice in MenuA
	
	Example:
		MenuA
		  Item1->MenuB
			Item1->MenuA	X	
		  Item2->MenuC
		  Item3->Link
		  Item4->MenuB		X
********************SET THE DEFAULT USER VARIABLES BELOW********************
*/
UserVariables=new Object();
//Default Menu Options
UserVariables.BackColor=			"#6D417E";
UserVariables.HighlightColor=			"#C09ECD";
UserVariables.BorderColor=			"#85509A";
UserVariables.BorderWidth=			"1";
//Default Menu Button Options
UserVariables.BorderNotSelectedColor=	"#000000";
UserVariables.BorderMouseOverUpperLeft=	"#777777"; //Also Selected Lower Right
UserVariables.BorderMouseOverLowerRight=	"#FFFFFF"; //Also Selected Upper Left
UserVariables.BBorderWidth=			"2";
//********************EVERYTHING AFTER HERE IS CONTENT AND IT IS RECOMMENDED YOU DO NOT CHANGE IT********************
Menus=new Array();
TopItems=new Array();
function Menu(BackColor, HighlightColor, TextHighlightColor, BorderWidth, BorderColor)
{
	this.MyHTML="<table style='position:absolute;top:0;left:0;visibility:hidden;border:"+(BorderWidth ? BorderWidth:UserVariables.BorderWidth)+"px ridge;' onMouseOver='InMenu=1;' onMouseOut='InMenu=0;' id=MenuNum"+Menus.length+" bgcolor="+(BackColor?BackColor:UserVariables.BackColor)+" bordercolor="+(BorderColor ? BorderColor:UserVariables.BorderColor)+" cellpadding=1 cellspacing=0><tr><td><table border=0 cellpadding=3 cellspacing=0>";
	this.SubMenus=new Array();
	this.AboveMenus=new Array();
	this.NumItems=0;
	this.showing=false;
	this.id=Menus.length;
	this.HighlightColor=(HighlightColor ? HighlightColor:UserVariables.HighlightColor);
	this.TextHighlightColor=(TextHighlightColor ? TextHighlightColor:"");
	
	this.AddItem=MenuAddItem;
	this.Display=MenuDisplay;
	this.Hide=MenuHide;
	Menus[Menus.length]=this;
}
function MenuAddItem(InnerHTML, BackColor, SubMenu)
{
	if(SubMenu)
	{
		this.SubMenus[this.SubMenus.length]=SubMenu;
		this.SubMenus[this.SubMenus.length]=this.NumItems;
		SubMenu.AboveMenus[SubMenu.AboveMenus.length]=this;
		SubMenu.AboveMenus[SubMenu.AboveMenus.length]=this.NumItems;
	}
	this.MyHTML+="<tr id=MenuTR"+this.id+"D"+this.NumItems+(BackColor ? " bgcolor="+BackColor : "")+"><td onMouseOver=MenuMouseOver("+this.id+",this,"+(this.NumItems++)+") onMouseOut=MenuMouseOut(this) onClick=MenuClick(this,event)>"+InnerHTML+"</td></tr>";
}
function MenuMouseOver(MenuID, Element, SubItemNum)
{
	MyMenu=Menus[MenuID];
	Element.bgColor=MyMenu.HighlightColor;
	for(var Child in Element.childNodes)
		if(Element.childNodes[Child].tagName!=undefined)
			Element.childNodes[Child].style.color=MyMenu.TextHighlightColor;
	for(var i=0;i<MyMenu.SubMenus.length;i+=2)
		if(MyMenu.SubMenus[i].showing && MyMenu.SubMenus[i+1]!=SubItemNum)
			MyMenu.SubMenus[i].Hide();
		else if(MyMenu.SubMenus[i+1]==SubItemNum && !MyMenu.SubMenus[i].showing)
			MyMenu.SubMenus[i].Display();
}
function MenuMouseOut(Element)
{
	Element.bgColor="";
	for(var Child in Element.childNodes)
		if(Element.childNodes[Child].tagName!=undefined)
			Element.childNodes[Child].style.color="";
}
function MenuClick(Element,event)
{
	for(var Child in Element.childNodes)
		if(Element.childNodes[Child].tagName!=undefined)
		{
			var NewElement=Element.childNodes[Child];
			var NewTag=NewElement.tagName;
			if(NewTag=="button")
				NewElement.click();
			else if (NewTag=="A")
			{
				if(!NewElement.target)
					document.location = NewElement.href;
				else if(NewElement.target.charAt(0)!="_")
					eval(NewElement.target+".location=NewElement.href");
				else
				{
					window.open(NewElement.href,NewElement.target);
					window.event.cancelBubble=true;
				}
			}
			else if (NewElement.onclick)
				NewElement.onclick();
		}
}
function MenuDisplay(x, y)
{
	var FoundParent=(x!=null && y!=null);
	if(!FoundParent)
		for(var i=0;i<this.AboveMenus.length;i+=2)
			if(this.AboveMenus[i].showing)
			{
				eval("ParentTR=MenuTR"+this.AboveMenus[i].id+"D"+this.AboveMenus[i+1]+";");
				var ParentPos=FindPos(ParentTR);
				ParentPos.x+=ParentTR.offsetWidth-10;
				ParentPos.y-=5;
				if(x==null) x=ParentPos.x;
				if(y==null) y=ParentPos.y;
				FoundParent=true;
				break;
			}
	if(!FoundParent)
		for(i in TopItems)
			if(TopItems[i].id==this.id)
			{
				eval("ParentItem=TM"+i+";");
				var ParentPos=FindPos(ParentItem);
				ParentPos.x+=10;
				ParentPos.y+=ParentItem.offsetHeight;
				if(x==null) x=ParentPos.x;
				if(y==null) y=ParentPos.y;
				FoundParent=true;
				break;
			}
	if(!FoundParent)
		return false;
	eval("CurrentMenuTable=MenuNum"+this.id+";");
	CurrentMenuTable.style.left=x;
	CurrentMenuTable.style.top=y;
	CurrentMenuTable.style.visibility="visible";
	this.showing=true;
}
function MenuHide()
{
	this.showing=false;
	eval("MenuNum"+this.id).style.visibility="hidden";
	for(var i=0;i<this.SubMenus.length;i+=2)
		if(this.SubMenus[i].showing)
			this.SubMenus[i].Hide();
}
function InitMenus()
{
	for(var i=Menus.length-1;i>=0;i--)
		document.write(Menus[i].MyHTML+"</table></td></tr></table>");
	if(document.captureEvents)
		document.captureEvents(Event.MOUSEDOWN);
	if(document.onmousedown==null)
	{
		document.onmousedown= CloseAllMenus;
		document.onfocusout= function() {if(!document.hasFocus()) CloseAllMenus(); }
	}
	else
	{
		window.onmousedown=CloseAllMenus;
		window.onblur = CloseAllMenus;
	}
	
	InMenu=0;
}
function CloseAllMenus()
{
	if(InMenu)
	{
		if(InMenu==2)
			InMenu=0;
		return;
	}
	for(i in TopItems)
		if(TopItems[i].showing)
		{
			TopItems[i].Hide();
			ItemBorder(eval("TM"+i), 0);
		}
}
function WriteMainMenuButton(InnerItemHTML, MenuToShow, NotSelectedBorderColor, MyBorderWidth)
{
	document.write("<table bordercolor="+(NotSelectedBorderColor ? NotSelectedBorderColor : UserVariables.BorderNotSelectedColor)+" cellpadding=0 cellspacing=0 id=TM"+TopItems.length+" onMouseOver=MBV("+TopItems.length+") onMouseOut=MBT("+TopItems.length+") onMouseDown=MBD("+TopItems.length+") style='border:"+(MyBorderWidth ? MyBorderWidth : UserVariables.BBorderWidth)+"px solid;'><tr><td>"+InnerItemHTML+"</td></tr></table>");
	TopItems[TopItems.length]=MenuToShow;
}
function MBV(MyNum)
{
	if(TopItems[MyNum].showing)
		return;
	for(var i in TopItems)
		if(TopItems[i].showing && i!=MyNum)
		{
			TopItems[i].Hide();
			ItemBorder(eval("TM"+i), 0);
			ItemBorder(eval("TM"+MyNum), 1);
			TopItems[MyNum].Display();
			return;
		}
	ItemBorder(eval("TM"+MyNum), 2);
}
function MBT(MyNum)
{
	InMenu=0;
	if(!TopItems[MyNum].showing)
		ItemBorder(eval("TM"+MyNum), 0);
}
function MBD(MyNum)
{
	if(TopItems[MyNum].showing)
		return;
	InMenu=2;
	ItemBorder(eval("TM"+MyNum), 1);
	TopItems[MyNum].Display();
}
function ItemBorder(TheItem, Style)
{
	var UV=UserVariables, UV1=UserVariables.BorderNotSelectedColor, UV2=UserVariables.BorderMouseOverUpperLeft, UV3=UserVariables.BorderMouseOverLowerRight;
	MyColors=Array(UV1,UV1,UV2,UV3,UV3,UV2);
	TheItem.style.borderTopColor=TheItem.style.borderLeftColor=MyColors[Style*2];
	TheItem.style.borderBottomColor=TheItem.style.borderRightColor=MyColors[Style*2+1];
}
if(navigator.appName=="Microsoft Internet Explorer")
	GetParent=function(element) { return element.parentElement; };
else
	GetParent=function(element) { return element.parentNode; };
function FindPos(element)
{
	ReturnPos=new Object();
	if(element==undefined || element==null || element==NaN || element.tagName=="BODY")
	{
		ReturnPos.x=ReturnPos.y=0;
		return ReturnPos;
	}
	AddPos=FindPos(GetParent(element));
	ReturnPos.x=AddPos.x+(element.tagName!="TR" && element.tagName!="TBODY" ? element.offsetLeft : 0);
	ReturnPos.y=AddPos.y+(element.tagName!="TD" ? element.offsetTop : 0);
	return ReturnPos;
}
