//################################################################
//	This script creates a menu using div tags.  It is built using
//	the custom menuitem object.  The text divs never move, they
//	just turn their visibility on and off, while the image divs
//	move to accommodate the text when it is made visible.
//	Script written by JOsh Powell, Web Developer EA.com
//################################################################
netscape	= (document.layers) ? 1:0;
goodIE		= (document.all)? 1:0;
netscape6   = ((document.getElementById) && (!goodIE)) ? 1:0;
var menupos = 200;
var MENU_LENGTH = 0;
var OPEN_MENU = null;
var ROLLED_OVER = 0;

//################################################################
//	Build the menuitem objects and build them into the menu array
//  IE 4.x wasn't supporting pushing them onto the array
//################################################################
var home = new addmenuitem("text1", "home",0,0);
var news = new addmenuitem("text2", "news",30,56);
var game = new addmenuitem("text3", "game",50,0);
var feature = new addmenuitem("text4", "feature",70,0);
var gallery = new addmenuitem("text5", "gallery",90,65);
var system = new addmenuitem("text6", "system",110,110);
var download = new addmenuitem("text7", "download",130,25);
var qanda = new addmenuitem("text8", "qanda",150,25);
var faq = new addmenuitem("text9", "faq",170,0);
var language = new addmenuitem("text10", "language",200,0);


var menu = new Array(home,news,game,feature,gallery,system,download,qanda,faq,language);

//################################################################
//	This function creates an object containing the name of the divs
//	holding the information about the menu.  Pass in the name of
//	the div with the text on it, the name of the div with the image
//	on it, the starting position of the image with the div on it, and
//	the height of the menu items. It also assosciates images with
//  an open and closed state based on the divname, and the position
//  in the menu, equal to MENU_LENGTH after it is incrimented.
//################################################################
function addmenuitem(textdiv,divname,startpos,wordheight)	{
	this.text = textdiv;  // identifies div holding this menuitems text
	this.name = divname;  // identifies div holding this menuitems image
	this.startpos = startpos + menupos;  // used with style.top to set location
	this.wordheight = wordheight; // used to determine how far to open emnu
	this.opensrc = new Image(); // opened image
	this.opensrc.src = "nav/mnu_" + divname + "-open.gif";
	this.closedsrc = new Image(); // closed image
	this.closedsrc.src = "nav/mnu_" + divname + "-closed.gif";
	this.positioninmenu = MENU_LENGTH++; // identifies position in menu, used to record and check if
};										 // this is the image that is open, and to start opening menuitmes
										 // from this point in the menu on.
										 								
//################################################################
//	Hides all of the "text" div tags named in menu array
//  IE, netscape4, W3C compliant
//################################################################
function hideWords()	{
	if(goodIE)	{
		for(a=0;a<menu.length;a++)	{
			divtag = eval('document.all.' + menu[a].text);
			divtag.style.visibility = "hidden";
		}
	}
	if(netscape)	{
		for(a=0;a<menu.length;a++)	{
			divtag = eval('document.' + menu[a].text);
			divtag.visibility = "hidden";
		}
	}
	if(netscape6)	{
		for(a=0;a<menu.length;a++)	{
		document.getElementById(menu[a].text).style.visibility="hidden";
		}	
	}
};
//################################################################
//	hides all "text" div tags then takes the one with the name
//	passed in and makes it visible
//  IE, netscape4, W3C compliant
//################################################################
function toggleWords(menuitem)	{
	hideWords();
	if(goodIE)	{
		divtag = eval('document.all.' + menuitem.text);
		divtag.style.visibility = "visible";
	}
	if(netscape)	{
		divtag = eval('document.' + menuitem.text);
		divtag.visibility = "visible";
	}
	if (netscape6)	{
		document.getElementById(menuitem.text).style.visibility="visible";
	}
};

//################################################################
// Closes menu from open item on by changing open image to closed
// image and placing the open menu item and all menu items after
// that to their closed states.
//################################################################
function closemenu()	{
	if((goodIE) && (OPEN_MENU!=null))	{
		for(a=OPEN_MENU;a<menu.length;a++)	{
			divname = eval('document.all.' + menu[a].name);
			divname.style.top = menu[a].startpos;
		}
	}
	if((netscape) && (OPEN_MENU!=null))	{
		for(a=OPEN_MENU;a<menu.length;a++)	{
			divname = eval('document.' + menu[a].name);
			divname.top = menu[a].startpos;
		}
	}
	if((netscape6) && (OPEN_MENU!=null))	{
		for(a=OPEN_MENU;a<menu.length;a++)	{
			document.getElementById(menu[a].name).style.top = menu[a].startpos;
		}		
	}
	closeOpenImage();
	hideWords();
	OPEN_MENU = null;
};

//################################################################
//	Sets all menuitem objects back to their starting position, then
//	it moves the item after the menuitem passed in and all the ones after it,
//	down as far as its wordlength, and turns the correct words visible.
//  If the menu item is already open, then it closes it.
//  IE, netscape4, W3C compliant
//################################################################

function updateMenu(menuitem)	{
	if (menuitem.positioninmenu != OPEN_MENU)	{ //if this menu item is not open
		closemenu();  // closes open menu item
		openImage(menuitem); // swap image with open state image
		if(goodIE)	{  // open menuitem for IE
			for (a=menuitem.positioninmenu+1;a<menu.length;a++)	{
				divname = eval('document.all.' + menu[a].name);
				divname.style.top = menu[a].startpos + menu[menuitem.positioninmenu].wordheight;
			}
		}
		if(netscape)	{ // open menuitem for netscape 4
			for (a=menuitem.positioninmenu+1;a<menu.length;a++)	{
				divname = eval('document.' + menu[a].name);
				divname.top = menu[a].startpos + menu[menuitem.positioninmenu].wordheight;
			}
		}
		if(netscape6)	{ // open menuitem for netscape 6
			for (a=menuitem.positioninmenu+1;a<menu.length;a++)	{
				document.getElementById(menu[a].name).style.top = menu[a].startpos + menu[menuitem.positioninmenu].wordheight;
			}		
		}
		toggleWords(menuitem);  // make words for that menuitem visible, hide the rest
		OPEN_MENU = menuitem.positioninmenu; // record which menuitem is now open
	}
	else	{  // if this menu item is open
		closemenu();
	}
};

//################################################################
// 	Loads the "_open" version of the image of the name passed in.
//  Sets ROLLED_OVER to the menuitem passed in's positioninmenu.
// 	Netscape has a bug so that images in a div tag with and absolute
// 	position cannot be swapped, so we check for IE.  Works in
//  Netscape 6 as well.
//################################################################
function openImage(openItem)	{
	if(goodIE || netscape6)	{
		changethisimage = eval("document." + openItem.name + "_image");
		changethisimage.src = openItem.opensrc.src;
		ROLLED_OVER = openItem.positioninmenu;
	}	
};

//################################################################
// 	Loads the "_closed" version of the open image in the menu array
// 	Netscape has a bug so that images in a div tag with and absolute
// 	position cannot be swapped, so we check for IE.  This works in
//  Netscape 6 now too.
//################################################################
function closeImage()	{
	if(goodIE || netscape6)	{
		if(OPEN_MENU != menu[ROLLED_OVER].positioninmenu)	{
			closethisimage = eval("document." + menu[ROLLED_OVER].name + "_image");
			closethisimage.src = menu[ROLLED_OVER].closedsrc.src;
		}
	}
};

//################################################################
//  Closes a menu item that is open, because using the closeImages()
//  function does not work so that rolling over an open menu item
//  and then off of it does not work.
//################################################################
function closeOpenImage() 	{
	if(goodIE || netscape6)	{
		if(OPEN_MENU!=null)	{
			closethisimage = eval("document." + menu[OPEN_MENU].name + "_image");
			closethisimage.src = menu[OPEN_MENU].closedsrc.src;
		}
	}
};
