// JavaScript Document
/***********************************************
*             Written by Alex Komoroske
*             ThinkFun
*             07/05
*             Unauthorized use of this code is prohibited.
*             For instructions on adding menus, see README.txt
***********************************************/

/*****Global Switches*****/
 var doLockMenus=false;
 var showAlternateStyles=true;
/************************/



 var popUpMenuTop=new Array(); //Array of where each popup menu's top should be when collapsed
 var popUpSlideTarget = new Array(); //Used only when closing a popupmenu--where it should stop.
 var popUpIntervalId= new Array(); //The ID of the interval that was created to close a particular popup.
 var popUpMenuNames= new Array(); //The names of the various popup menus
 var popUpMenuData=new Array(); //The data that describes the type, title, and URL of a given popupmenu
 var popUpMenuRoot=new Array(); //The 'home page' associated with a given popupmenu.  If a page is found to be within that homepage's directory, the popupmenu is styled differently.
 var popUpMenuHeight=new Array(); //How tall a given popupmenu is.  set programattically.
 var subMenuNames= new Array();
 var subMenuData = new Array();
 var subMenuRoot = new Array();
 var contentMenuHeight=0;
 var lockedMenu=new Array();
 var contentMenuData="";
 var currentPathIndex=-1;
 var urlSuf = ".png";
 var pathToRoot="";
 var pathFromRoot="";
 var pageLocationFromRoot="";
 var browser= new BrowserType();


function init() {
	 //This function simply initalizes many values.  
	 //This is where the menu structure--that is, the menu items, but not their styling--is changed.
	 //to change how the menu is rendered (NOT what the CSS rules say, but which css rules are used where, check out writeNavBar.  This is only if you need to really make a a big deal change.)
										   
	
	
//*** THESE ARE THE "IN THIS SECTION MENUS" *** 

subMenuNames[0]="HowToRunMathDice";
subMenuData[0]="How To Play MathDice,AMN,teachers/howtorunmathdice/howtoplay.html;How To Teach MathDice,AMN,teachers/howtorunmathdice/teachingmathdice.html;Running MathDice in the Classroom,AMN,teachers/howtorunmathdice/mainseason.html;Running an In-Class MathDice Tournament,AMN,teachers/howtorunmathdice/tournaments.html"
subMenuRoot[0]="";

subMenuNames[1]="TeacherClassroomGuide";
subMenuData[1]="Overview,AMN,teachers/howtorunmathdice/program-intro.html;Preseason,AMN,teachers/howtorunmathdice/program-preseason.html;Main Season,AMN,teachers/howtorunmathdice/program-mainseason.html;Games,AMN,teachers/howtorunmathdice/program-games.html;Tournament Season,AMN,teachers/howtorunmathdice/program-tournamentseason.html;Where and When to Play,AMN,teachers/howtorunmathdice/program-wheretoplay.html;Point System,AMN,teachers/howtorunmathdice/program-pointsystem.html;End of Season Celebration,AMN,teachers/howtorunmathdice/program-celebration.html;The Math behind MathDice,AMN,teachers/mathbehindmathdice.html;Resources/Downloads,AMN,teachers/downloads.html;Online Problem Sets,AMN,teachers/onlineproblems.html"
subMenuRoot[1]="";

subMenuNames[2]="KidsGettingStartedGuide";
subMenuData[2]="How To Play MathDice,AMN,kids/gettingstarted/howtoplay.html;Play MathDice Online,AMN,kids/newtrainingroom.html;MathDice Online Arcade,AMN,kids/archive.html;How the MathDice School Program Works,AMN,kids/gettingstarted/schoolprogram.html;The Different Ways to Play MathDice,AMN,kids/gettingstarted/variants.html";
subMenuRoot[2]="";

subMenuNames[3]="TeacherOnlineProblems";
subMenuData[3]="Introduction,AMN,teachers/onlineproblems.html;Featured Sets,AMN,teachers/onlineproblems/featured.html;Contribute,AMN,teachers/onlineproblems/contribute.html;Table of Contents,AMN,teachers/onlineproblems/contents.html;Beginner,AMN,teachers/onlineproblems/beginner.html;Intermediate,AMN,teachers/onlineproblems/intermediate.html;Advanced,AMN,teachers/onlineproblems/advanced.html;Expert,AMN,teachers/onlineproblems/expert.html";
subMenuRoot[3]="";

subMenuNames[4]="HowPTAParentsCanHelp";
subMenuData[4]="Arlington VA Spring MathDice Tournament,AMN,parents/arlingtontournament.html;Barnes and Noble Bookfairs,AMN,parents/bnbookfair.html;ThinkFun School Strategy Lab,AMN,parents/strategylab.html;Help Celebrate!,AMN,parents/celebrate.html";
subMenuRoot[4]="";

subMenuNames[5]="BarnesAndNobleBookFairs";
subMenuData[5]="Why Would You Want To Participate?,AMN,parents/gamenightbenefits.html;Planning Your Fundraiser Event,AMN,parents/gamenightpreparation.html;Game Night Program Description,AMN,parents/gamenight.html;Turn The Games Into a School Strategy Lab,AMN,parents/strategylab.html";
//subMenuData[5]="Why Would You Want To Participate?,AMN,parents/barnesandnoble/gamenightbenefits.html;Planning Your Fundraiser Event,AMN,parents/barnesandnoble/gamenightpreparation.html;Game Night Program Description,AMN,parents/barnesandnoble/gamenight.html;Turn The Games Into a School Strategy Lab,AMN,parents/barnesandnoble/strategylab.html";
subMenuRoot[5]="";

//*** THESE ARE THE "LEFT MENUS" *** 

popUpMenuNames[0]="KidPopUpMenu";
popUpMenuNames[1]="TeacherPopUpMenu";
popUpMenuNames[2]="ParentPopUpMenu";

popUpMenuRoot[0]="kids/";
popUpMenuRoot[1]="teachers/";
popUpMenuRoot[2]="parents/";

popUpMenuData[0]="Kids,ABS,kids/gettingstarted/howtoplay.html";
popUpMenuData[1]="Teachers,ABS,teachers/howtorunmathdice/program-intro.html";
popUpMenuData[2]="Parents/PTA/PTO,ABS,parents/overview.html"

contentMenuData="Back To Top,AUS,#HomeLink;#KidPopUpMenu;#TeacherPopUpMenu;#ParentPopUpMenu;Play MathDice,AMS,kids/newtrainingroom.html;Gallery,AMS,teachers/gallery.html;Buy MathDice,AMS,http://www.thinkfun.com/PRODUCT.ASPX?PageNo=PRODUCT&Catalog=By%20Category&Category=6EDUCATION&ProductId=1510;ThinkFun.com,AMS,http://www.thinkfun.com;Home,ABS,index.html";
contentMenuHeight=0;
currentPathIndex=-1;

  for (i=0;i<popUpMenuNames.length;i++) { //This line defines the number of menu entries as the number of defined popupMenuNames
	 
	 popUpSlideTarget[i]="-1";
	 popUpIntervalId[i]=0;
	 popUpMenuHeight[i]=0;
	 //each menu starts out unlocked.
	 lockedMenu[i]=0;
	 
	  if (pathFromRoot.indexOf(popUpMenuRoot[i])>=0) {
		  currentPathIndex=i;
	  }
	
  }
 
   

 }


 function writeGameTableOfContents(tableInfo) {
	//tableInfo formatted like so:
	//Game Title~Game Description~Game Link (normally an anchor)|...
	document.write("<table cellspacing='0' cellpadding='0' border='0' class='GameTable'>");
	
	tableInfo=tableInfo.split("|");
	
	for(b=0;b<tableInfo.length;b++) {
		tableInfo[b]=tableInfo[b].split("~");
		
		document.write("<tr><td>");
		document.write("<table cellpadding='0' cellspacing='0' border='0' width='100%'>");
		
			document.write("<tr height='10px'><td width='10%'></td>");
			if (b>0) {
				document.write("<td width='80%' class='GameTableBorderCell'></td>");
			}
			else {
				document.write("<td width='80%'></td>");
			}
			document.write("<td width='10%'></td></tr>");
		document.write("<tr>");
		document.write("<td colspan='3'>");
		document.write("<a href='" + tableInfo[b][2] + "' class='GameTableTitle'>" + tableInfo[b][0] + "</a>");
		document.write("</td></tr>")
		document.write("<tr height='10px'><td colspan='3'></td></tr>");
		document.write("<tr><td colspan='3'>");
		document.write("<p>" + tableInfo[b][1] + "</p>");
		document.write("</td></tr><tr height='10px'><td colspan='3'></td></tr>");
		document.write("</table>");
		document.write("</td></tr>");
		
		
	}
	 
	 document.write("</table>");
 }
 
 function writeGameTable(baseURLPDF, tableInfo) {
	 //base url is base relative URL, as in ../../media/PDF/Problem Sets/
	 document.write("<table cellpadding='0' cellspacing='0' border='0' class='GameTable'>");
	 var gameTitle, gameName, gameDescription, gamePage, levelName;
	 tableInfo=tableInfo.split("#");
	 
	 tableInfo[0]=tableInfo[0].split("~");
			gameTitle=tableInfo[0][0];
			levelName=tableInfo[0][1];
			gameName=tableInfo[0][2];
			gameDescription=tableInfo[0][3];
			if(gameDescription.indexOf("$$")>=0) {
				gameDescription=gameDescription.split("$$").join("");
				gamePage="";
			}
			else {
				gamePage=gameName;
			}
			document.write( '<tr>');
			document.write("<td>");
			document.write("<table cellpadding='0' cellspacing='0' border='0' class='GameTableIndividual'>");
			document.write("<tr height='10px'>");
			document.write("<td width='10%'>");
			document.write("</td>");
			document.write("<td width='80%'>");
			document.write("</td>");
			document.write("<td width='10%'>")
			document.write("</td>");
			document.write("</tr>");
			document.write("<tr>");
			document.write("<td colspan='3' class='GameTableTitleRow'>");
			document.write("<a href='" + baseURLPDF + "/" + gameName + ".pdf" + "' class='GameTablePrintLink' name='" + gameName + "'></a>");
			document.write("<a href='" + pathToRoot + "newplay/mathdiceGame.html?file=problemsets/" + gameName +"' class='GameTablePlayLink'></a>");
			document.write("<span class='GameTableTitle'>" + gameTitle + "</span>");
			
			
			document.write("</td></tr>");
			document.write("<tr><td colspan='3'>");
			document.write("<span class='GameTableLevelTitle'>" + levelName + "</span>");
			document.write("</td></tr>");
			document.write("<tr height='75px'>");
			document.write("<td class='GameTableDescription' colspan='3'>");
			if(gamePage) {
			document.write(gameDescription + "&nbsp;&nbsp;&nbsp;&nbsp;<a href='" + gamePage + ".html' class='ReadMoreLink'>Read More...</a>");
			}
			else {
				document.write(gameDescription);
			}
			document.write("</td></tr>");
			document.write("<tr height='10px'><td colspan='3'></td></tr>");
			document.write("</table>");
			document.write("</td>");
			document.write("</tr>");
	 if (tableInfo.length>1) {
		 for(p=1;p<tableInfo.length;p++) {
			 tableInfo[p]=tableInfo[p].split("~");
			gameTitle=tableInfo[p][0];
			levelName=tableInfo[p][1];
			gameName=tableInfo[p][2];
			gameDescription=tableInfo[p][3];
			if(gameDescription.indexOf("$$")>=0) {
				gameDescription=gameDescription.split("$$").join("");
				gamePage="";
			}
			else {
				gamePage=gameName;
			}
			document.write( '<tr>');
			document.write("<td>");
			document.write("<table cellpadding='0' cellspacing='0' border='0' class='GameTableIndividual'>");
			document.write("<tr height='10px'>");
			document.write("<td width='10%'>");
			document.write("</td>");
			document.write("<td width='80%' class='GameTableBorderCell'>");
			document.write("</td>");
			document.write("<td width='10%'>")
			document.write("</td>");
			document.write("</tr>");
			document.write("<tr>");
			document.write("<td colspan='3' class='GameTableTitleRow'>");
			document.write("<a href='" + baseURLPDF + "/" + gameName + ".pdf" + "' class='GameTablePrintLink'  name='" + gameName + "'></a>");
			document.write("<a href='" + pathToRoot + "newplay/mathdiceGame.html?file=problemsets/" + gameName +"' class='GameTablePlayLink'></a>");
			document.write("<span class='GameTableTitle'>" + gameTitle + "</span>");
			
			
			document.write("</td></tr>");
			document.write("<tr><td colspan='3'>");
			document.write("<span class='GameTableLevelTitle'>" + levelName + "</span>");
			document.write("</td></tr>");
			document.write("<tr height='75px'>");
			document.write("<td class='GameTableDescription' colspan='3'>");
			if(gamePage) {
			document.write(gameDescription + "&nbsp;&nbsp;&nbsp;&nbsp;<a href='" + gamePage + ".html' class='ReadMoreLink'>Read More...</a>");
			}
			else {
				document.write(gameDescription);
			}
			document.write("</td></tr>");
			document.write("<tr height='10px'><td colspan='3'></td></tr>");
			document.write("</table>");
			document.write("</td>");
			document.write("</tr>");
			 
		 }
	 }
	 document.write("</table>");
 }
 
  
  function commonHeader(thisPathToRoot, thisPathFromRoot) {
	 
	  if (thisPathToRoot) {
		  if(thisPathFromRoot) {
			  //we're given both
		  pathToRoot=thisPathToRoot;
		  pathFromRoot=thisPathFromRoot;
		  }
		  else {
				  //we're being given one arg. this means that we're only getting thisPathFromRoot.
				  pathFromRoot=thisPathToRoot;
				  thisPathToRoot=thisPathToRoot.split("/");
				  //the number of "/" is one less than the lenght of the array.
				  //create our own thisPathToRoot string.
				  pathToRoot="";
			  
				  for (h=1;h<thisPathToRoot.length;h++) {
					
					  pathToRoot=pathToRoot + "../";
				  }
			  
			  }
		
	  }
	
	  /**********************************/
	  
	  //we're going to construct the pageLocationFromRoot using some fancy footwork.
		
		var workingURL=window.location.href;
		
			workingURL=workingURL.split("/");
			//now the page filename should be the last element of the array.
			pageLocationFromRoot=pathFromRoot + workingURL[workingURL.length-1];
			
		
		
		
		/****************************************/
	   
	  
	//this function simply writes the common header to all the pages it is invoked on.
	//this achieves pseudo-server functionality without server-side scripting.
	//the down-side is that it requires javascript to be enabled to work correctly.
	//the base style
	document.write("<link rel=\"stylesheet\" href=\"" + pathToRoot + "mathdice_style_BASE.css\" type=\"text/css\">");
	//the base style changing part:
	document.write("<link rel=\"stylesheet\" href=\"" + pathToRoot + "mathdice_style_FIXEDMENU.css\" type=\"text/css\" title=\"Floating Menu\">");
	//the alternate style changing part:
	if (showAlternateStyles) {
	document.write("<link rel=\"alternate stylesheet\" href=\"" + pathToRoot + "mathdice_style_ABSOLUTEMENU.css\" type=\"text/css\" title=\"Stationary Menu\">");
	document.write("<link rel=\"alternate stylesheet\" href=\"" + pathToRoot + "mathdice_style_TABS.css\" type=\"text/css\" title=\"Tabs\">");
	}
	//shortcut icon
	
	//ineffective
	//document.write("<link rel=\"shortcut icon\" href=\"" + pathToRoot + "favicon.ico\">");
	//load IE base css if it's neccesary
	   	if (browser.canAlphaPNG==false) {
	  		switchToGIF();
  		}
  }
  
  
  function writeNavBar() {
	//This function writes the whole nav bar, also writing menus.
	//write nav bar opening
	//write menus
	//this function calls init automatically.
	init();
	document.write("<div id=\"TopNavBar\" class=\"NavigationBar\">");
	document.write("<div id=\"TopNavBarGraphic\">");
	document.write("<a href=\"" + pathToRoot + "index.html\" name=\"HomeLink\">");
	document.write("<img id=\"TopNavBarLeftGraphic\" src=\"" + pathToRoot + "media/images/blank.gif\" border=\"0\" alt=\"MathDice Online\">");
	document.write("</a>");
	document.write("<a href=\"http://www.thinkfun.com\">");
	document.write("<img class=\"ThinkFunLogo\" src=\"" + pathToRoot + "media/images/blank.gif\" border=\"0\" alt=\"ThinkFun Logo\">");
	document.write("</a>");
	document.write("</div>");
	writeMenus();
	//close up div tag for the entire nav bar
	document.write("</div>");
	//call any closing functions that are neccesary
	finalizeNavBar();
  }
  
  function finalizeNavBar() {
	  //currently, the main point of this function is to set the top and height properties of the popUpMenus.
	  //it also automatically calls unlockOnlyMenu with the argument set to whichever menu was detected as being the one we're in.
	  //if you want to change whether menus are locked, do not remove the unlockOnlyMenu from this function; simply set the 
	  //global var doLockMenus to false (default).
	  if (currentPathIndex==-1) {
		  //we're in the root.  lock all menus.
		 
		  lockAllMenus();
	  }
	  else {
		  //unlock whatever the currentPathIndex is.
		  unlockOnlyMenu(popUpMenuNames[currentPathIndex]);
	  }
	  
	  contentMenuHeight=contentMenuHeight * 25; //multiply number of rows times the height of each row.
	  
	  for(j=0;j<popUpMenuNames.length;j++) {
		  popUpMenuHeight[j]=popUpMenuHeight[j] * 25; //multiply number of rows times the height of each row.
		  //set that object's height to this, overriding CSS.
		  document.getElementById(popUpMenuNames[j]).style.height=popUpMenuHeight[j] + "px"; 
		  
		  popUpMenuTop[j]=(-1 * popUpMenuHeight[j]) + 100; //set resting top so that the bottom row (height of 25 px)'s top is at 75 px.
		   document.getElementById(popUpMenuNames[j]).style.top=popUpMenuTop[j] + "px"; 
	}
	  
  }
  
  function writeMenus() {
	
	
	writeContentMenu();
	for (j=0;j<popUpMenuNames.length;j++) {
		writePopUpMenu(popUpMenuNames[j],popUpMenuData[j]);
	}
  }
	  
	  
	function writeContentMenu() {
		//Like writePopUpMenu, except that it also accepts whole popUpMenus as an item.  
		//menu id's are denoted by a)one arg, and b) first char is #.
		var menuData=contentMenuData.split(";");
		var menuItem;
		var menuIndex=-1;
		var subMenu;
		//write prelimaries.
		document.write("<div id=\"ContentPopUpMenu\" class=\"NavBarPopUpMenu\">");
		document.write("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" align=\"center\" width=\"100%\">");
		
		//start loop of args
		for (k=0;k<menuData.length;k++) {
			//check if first char is #
			if (menuData[k].charAt(0)=="#") {
				// IF:
					//get rid of the #
					menuData[k]=menuData[k].replace("#","");//this will only replace the first character.
					//figure out the index of this menu
					menuIndex=returnMenuVar(menuData[k]);
					
					if (menuIndex==-1 || menuIndex>(popUpMenuNames.length-1)) {
						alert("Error, unknown menu shortcut: " + menuData[k] + ".  Returned index: " + menuIndex);
					}
					
					//check to see if we're off the root.  if we are, check to see if we're in that menu's branch.
					if (pathFromRoot.indexOf(popUpMenuRoot[menuIndex])>=0) {
						//IF:
							//reproduce the whole menu, doing the last item first, then starting at 0 and going to length-1
							
							subMenu=popUpMenuData[menuIndex].split(";");
							//last item first
							menuItem=subMenu[subMenu.length-1];
							menuItem=menuItem.split(",");
							menuItem[2]=renderLink(menuItem[2],pathToRoot,pathFromRoot);
							menuItem[1]=menuItem[1].toUpperCase();
							//shift from major to minor
							menuItem[1]=menuItem[1].replace("A","I");
							//the item is likely of type B right now.
							menuItem[1]=menuItem[1].replace("B","M");
							//write code
							contentMenuHeight=contentMenuHeight + 1;
							writeMenuCell(menuItem[1],menuItem[2],menuItem[0],"onMouseOver=\"closeAll();showPopUpMenu(\'" + popUpMenuNames[menuIndex] + "\')\"");
							//now, loop through from 0 to length-1, rendering links.
							for(l=0;l<(subMenu.length-1);l++) {
								menuItem=subMenu[l].split(",");
								menuItem[2]=renderLink(menuItem[2],pathToRoot,pathFromRoot);
								menuItem[1]=menuItem[1].toUpperCase();
								//shift from major to minor
								menuItem[1]=menuItem[1].replace("A","I");
								//write code
								contentMenuHeight=contentMenuHeight + 1;
								writeMenuCell(menuItem[1],menuItem[2],menuItem[0]);
								
							}
							
							
					}
					else {
						//IF NOT:
							//reproduce only the last line of the specified menu.
							//and make sure it's major
							subMenu=popUpMenuData[menuIndex].split(";");
							//last item first
							menuItem=subMenu[subMenu.length-1];
							menuItem=menuItem.split(",");
							menuItem[2]=renderLink(menuItem[2],pathToRoot,pathFromRoot);
							menuItem[1]=menuItem[1].toUpperCase();
							//shift from minor to major
							menuItem[1]=menuItem[1].replace("I","A");
							//ensure that this is a section.
							menuItem[1]=menuItem[1].replace("N","S");
							//ensure that it's middle type
							menuItem[1]=menuItem[1].replace("B","M");
							//write code
							contentMenuHeight=contentMenuHeight + 1;
							writeMenuCell(menuItem[1],menuItem[2],menuItem[0], "onMouseOver=\"closeAll();showPopUpMenu(\'" + popUpMenuNames[menuIndex] + "\')\"");
					}
			}
			else {
				//IF NOT:
					//this code will be just like what's used for WritePopUpMenu
					menuItem=menuData[k].split(",");
					
					menuItem[2]=renderLink(menuItem[2],pathToRoot,pathFromRoot);
					menuItem[1]=menuItem[1].toUpperCase();
					//we won't worry about setting it as minor automatically if it is in the same branch.
					contentMenuHeight=contentMenuHeight + 1;
					writeMenuCell(menuItem[1],menuItem[2],menuItem[0]);
			}
		//end loop of args
		}
		//finish up code
		document.write("</table>");
		document.write("</div>");
		
	}
  
  function writePopUpMenu(menuID, menuList) {
	  //this function is a test that will help auto-generate menus.
	
	  //link rendering is handled with renderLink.
	  //menulist comes in the format Name,TypeFlag,Link;Name,TypeFlag,Link ...
	  var menuIndex=returnMenuVar(menuID);
	  
	  document.write("<div class=\"NavBarPopUpMenu\" id=\"" + menuID + "\" onMouseOver=\"showPopUpMenu('" + menuID + "')\" onMouseOut=\"hidePopUpMenu('" + menuID + "')\">");
	  document.write("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" align=\"center\" width=\"100%\">");
	  
	  var menuItem;
	  var isBranch=false;
	  if (pathFromRoot.indexOf(popUpMenuRoot[menuIndex])>=0) {
		  //check to see if this menu's root is contained within our current path; if it is, we should be minor.
		  //this includes pages in folders contained within the section root, like teachers/gallery/
		  isBranch=true;
	  }
	
	  
	  //Typeflag is a string, and corresponds to the PopUpMenu_XYZ argument.
	  //Expect it to soon accept just A and I as well, and it will figure out which needs to be which.
	  menuList=menuList.split(";");
	  for (i=0;i<menuList.length;i++) {
		popUpMenuHeight[menuIndex]=popUpMenuHeight[menuIndex] + 1;
		menuItem=menuList[i].split(",");
		//0: name
		//1: typeflag
		//2: link ("*" if not a link; "javascript://" if empty link)
		menuItem[2]=renderLink(menuItem[2],pathToRoot,pathFromRoot);
		menuItem[1]=menuItem[1].toUpperCase();
		
		if (isBranch) {
			//isBranch is set at the beginning of this function.
			//we should use minor type.
			menuItem[1]=menuItem[1].replace("A","I");
		}
		
		writeMenuCell(menuItem[1],menuItem[2],menuItem[0]);
		
		
		
	  }
	document.write("</table>");
	document.write("</div>");
	  
	  //done
  }
  
  function writeSectionMenu(menuID) {
	  //this function only accepts subMenus.
	  //if a menu is sent here, then it is assumed to be valid section menu type.
	  	//that is, it does not contain any references to popupmenus (which are only valid in contentMenus)
	  var index=returnSubMenuVar(menuID);
	  var subMenuList=subMenuData[index];
	  var subMenuPart;
	  subMenuList=subMenuList.split(";");
	  //write header
	  document.write("<table cellpadding=\"5px\" cellspacing=\"0\" border=\"0\" class=\"ThisSectionBox\">");
	  document.write("<tr>");
	  document.write("<td class=\"ThisSectionTitle\">In This Section</td>");
	  document.write("</tr>");
	  
	  //now, loop through and write each cell
	  for(t=0;t<subMenuList.length;t++) {
		  subMenuPart=subMenuList[t].split(",");
		  //0:name 1:Formatting 2:link
		  //standardize formatting string:
		  subMenuPart[1]=subMenuPart[1].toUpperCase();
		   //we need to figure out if this is the current section.
		   //this is different from popupmenus, where the whole menu is turned minor if we are in the same directory path
		   //this should only switch if we ARE the same link.
		 
		   if(pageLocationFromRoot.toUpperCase().indexOf(subMenuPart[2].toUpperCase())>=0) {
			   //we should be current section
			   //change any A in formatting string to I
			   subMenuPart[1]=subMenuPart[1].replace("A","I");
		   }
		  
		  //prepare the link:
		  subMenuPart[2]=renderLink(subMenuPart[2], pathToRoot, pathFromRoot);
		 
		 //now, write the cell:
		 writeSectionMenuCell(subMenuPart[1],subMenuPart[2],subMenuPart[0]);
		  
		  
		  
		  
	  }
	  //now, write tailing tag
	  document.write("</table>");
	  
  }
  
  function writeSectionMenuCell(menuFormat,menuLink,menuText, extraTag) {
	  if (!extraTag) {
		  extraTag="";
	  }
	  //Menu format comes to us as the three letter string type. 
	  //however, for section menus, we're only interested in whether it's mAjor or mInor--non-current or current, respectively.
	  if(menuFormat.indexOf("I")>=0) {
		  //it's current
		  menuFormat="ThisSectionCurrentRow"
		  //which also means that the link should be non-functioning
		  menuLink="";
	  }
	  else {
		  //it's non-current
		  menuFormat="ThisSectionRow";
	  }
	  
	  menuFormat=menuFormat;
	  
	  document.write("<tr>");
	  document.write("<td class=\"" + menuFormat + "\">");
	  	if (menuLink=="") {
			//this item shouldn't be a link, but still make the cursor look like it is
			document.write("&bull; <span class=\"CurrentSection\" style=\"cursor:pointer\" " + extraTag +">" + menuText + "</span>");
		}
		else {
			//this item should be a link.
			
		document.write("&bull; <a class=\"BodyLink\" href=\"" + menuLink + "\" " + extraTag + ">" + menuText + "</a>");
		}
		document.write("</td>");
		document.write("</tr>");
	  
  }
  
  function writeMenuCell(menuFormat, menuLink, menuText, extraTag) {
	  //the extraTag argument is optional.  it simply adds extra tag to the link or span tag, and so is useful for adding new attribute=value pairs, like mouseOver events.
	  //(remember, the changing of the images is handled by CSS, not javascript)
	 
	  if(!extraTag) {
		  extraTag="";
	  }
	
	  	document.write("<tr>");
		document.write("<td class=\"PopUpMenuCell\">");
		if (menuLink=="") {
			//this item shouldn't be a link, but still make the cursor look like it is
			document.write("<span class=\"PopUpMenu_" + menuFormat + "\" style=\"cursor:pointer\" " + extraTag +">" + menuText + "</span>");
		}
		else {
			//this item should be a link.
			
		document.write("<a class=\"PopUpMenu_" + menuFormat + "\" href=\"" + menuLink + "\" " + extraTag + ">" + menuText + "</a>");
		}
		document.write("</td>");
		document.write("</tr>");
  }
  
  function renderLink(thisLinkFromRoot, thisPathToRoot, thisPathFromRoot) {
	//check to see if the link from root includes the pathfromroot.
	//this would mean that the link was in the same branch.

	if (thisLinkFromRoot.charAt(0)=="*") {
		//not a link
		return "";
	}
	if (thisLinkFromRoot.indexOf("javascript:")>=0 || thisLinkFromRoot.charAt(0)=="#") {
		//this link is a javascript link or an anchor. return as is.
		
		return thisLinkFromRoot;
	}
	if(thisLinkFromRoot.indexOf("http://")>=0) {
		//this is an external link.
		
		return thisLinkFromRoot;
	}
	if (thisLinkFromRoot.indexOf(thisPathFromRoot)>=0) {
		//link is in same branch as current.
		
		
		return thisLinkFromRoot.replace(thisPathFromRoot,"");
		
	}
	else {
	//if it's NOT in the same branch, then we just append the pathToRoot (which may very well be "") to the linkFromRoot.
	
	return thisPathToRoot + thisLinkFromRoot;
	}
  }
  
  function switchToGIF() {
	  
	//currently, urlSuf isn't used by anything.  However, it is available to be used.
	urlSuf=".gif"
	//use the alternate style sheet, which only overrides picture locations.
	
	
	document.write("<link rel=\"stylesheet\" href=\"" + pathToRoot + "mathdice_style_IE.css\" type=\"text/css\">");
	
 }
 


//Code to detect user's browser type:
//it has no methods because the information it checks never changes.
function BrowserType () {
	this.isIE=false;
	this.isNN=false;
	
	this.canAlphaPNG=true;
	this.version="0.0";
	var userAgent, counter;
	userAgent=navigator.userAgent;
	counter=userAgent.indexOf("MSIE");
	if (counter>=0) {
		this.isIE=true;
		this.canAlphaPNG=false;
		this.version = parseFloat(userAgent.substr(counter + 4)); //Parse the first float that comes after the location of MSIE (which is four characters)
		//We've set everything, we're done
		return;
	}
	counter=userAgent.indexOf("Gecko");
	if (counter>=0) {
		this.isNN=true;
		this.version=6.1;
		return;
	}
	counter=userAgent.indexOf("Netscape6/");
	if(counter>=0) {
		this.isNN=true;
		this.version=parseFloat(userAgent.substr(counter + 10));
		return;
	}
	//IF it's anything else, then we won't know it, but we'll assume they have support for Alpha PNG
}


function lockAllMenus() {
	//this function simply locks all menus so they don't move.
	
	//to override lock functionality, simply return here before setting lockedMenu[i]=1;
	//no other funciton sets it.
	if (!doLockMenus) {
	
	return; //without locking the menus
	}
	for (i=0;i<lockedMenu.length;i++) {
		lockedMenu[i]=1;
		
	}
	
}

function unlockMenu(menuID) {
	//this function simply unlocks one menu at a time.
	//check to see if we've been supplied an index (0-3) or a string ID
	var indexIs=-1;
	
	if (isNaN(menuID)==true) {
		//given a string:
		indexIs=returnMenuVar(menuID);
	}
	else {
		//given an index
		indexIs=menuID;
	}
	
	//check for invalid index:
	if(indexIs<0) {
		alert ("Invalid Menu ID: " + menuID);
		return;
	}
	
	//now, unlock the index
	lockedMenu[indexIs]=0;
}

function unlockMenus(menuID) {
	//accepts a list of id's delimited by commas
	//just like unlockOnlyMenu, only without lockAllMenus;
	menuID=menuID.split(",");
	for (i=0;i<menuID.length;i++){
		
		unlockMenu(menuID[i]);
	}
}

function unlockOnlyMenu(menuID) {
	//accepts a list of id's delimited by commas.
	//simply calls lock all menus, then unlocks the menus specified.
	lockAllMenus();
	menuID=menuID.split(",");
	for (i=0;i<menuID.length;i++){
		
		unlockMenu(menuID[i]);
	}
}




 
 

 
function showPopUpMenu(id) {
	
	cancelClose(id);
	
	var index = returnMenuVar(id);
	
	if (lockedMenu[index]==0) {
		//show menu
		document.getElementById(id).style.top="75px";
		
	}
	else {
		//don't show; it's locked
		
	}
	
}

function returnSubMenuVar(id) {
	//returns the index number associated with the id string.
	for (i=0;i<subMenuNames.length;i++) {
		if(id.toUpperCase()==subMenuNames[i].toUpperCase()) {
			return i;
		}
	}
	//well, there doesn't exist a named ID that matches.  Maybe they sent the actual id?
	if (!isNaN(id) && id<subMenuNames.length && id>=0) {
		//it's probably just the number.
		return parseInt(id);
	}
	//if we get to here, then the id string is not valid.
	return -1;
}

function returnMenuVar(id) {
	//Returns the index number associated with the id string.
	
	for(i=0 ; i<popUpMenuNames.length; i++) {
		if (id.toUpperCase()==popUpMenuNames[i].toUpperCase()) {
			return i;
		}
	}
	//well, there doesn't exist a named ID that matches.  Maybe they sent the actual id?
	if (!isNaN(id) && id<popUpMenuNames.length && id>=0) {
		//it's probably just the number.
		return parseInt(id);
	}
	//If we get to here then the id string is not valid.
	return -1;
	
}

function hidePopUpMenu(id) {
	try {
		
		var index = returnMenuVar(id);
			//If it's already got an interval going, clear it
			cancelClose(id);
			popUpSlideTarget[index]=popUpMenuTop[index];
			popUpIntervalId[index]=setInterval("closingPopUp()", 10);
						
		
	}
	catch(er) {
		alert(er);
	}
}

function closeAll() {
	for (i=0; i<popUpMenuNames.length;i++) {
		//if it's already closing, cancel that interval.
		cancelClose(popUpMenuNames[i]);
		hidePopUpMenu(popUpMenuNames[i]);
	}
}
function cancelClose(id) {
	//This function cancels the closing of the specified id. 
	var index= returnMenuVar(id);
	clearInterval(popUpIntervalId[index]);
	popUpSlideTarget[index]=-1;
}

function closingPopUp() {
	//This function actually handles closing the pop up menu.
	
	//This currently calls this function a lot--if all four menus are retracting, then this is being called four times too much
	for(i=0;i<popUpIntervalId.length;i++) {
		var id = popUpMenuNames[i]
		if (popUpSlideTarget[i]==-1) {
			//don't do anything; this particular index is not supposed to be sliding
		}
		else {
			//var index = returnMenuVar(id);
			if (parseInt(document.getElementById(id).style.top,10)>popUpSlideTarget[i]) {
				document.getElementById(id).style.top= (parseInt(document.getElementById(id).style.top,10) - 10) + "px";
			}
			else {
				//we're done sliding
				clearInterval(popUpIntervalId[i]);
				//make sure that the menu rests where it's supposed to
				document.getElementById(id).style.top=popUpMenuTop[i] + "px";
				popUpSlideTarget[i]=-1;
			}
		}
	}
	
}




