var allDivs = document.getElementsByTagName("div");
var allTables = document.getElementsByTagName("table");
var images = new Array("images/feature/accelerator.gif",
		     "images/feature/wirthlinCrocker.gif",
		     "images/feature/geiger.gif");
var captions = new Array("The SLAAC1-V board was used in a radiation test of the Xilinx Virtex 1000 FPGA. As shown in the figure, the host computer was shielded by aluminum with a small opening so that protons could reach one FPGA on the SLAAC1-V board, extended out of the host computer by a PCI riser card.",
    	       	   	 "Dr. Michael J. Wirthlin, a professor at BYU, takes notes during the accelerator test conducted at Crocker Nuclear Laboratories, UC Davis.",
			 "Paul Graham, a researcher from Los Alamos National Laboratory, holds a geiger counter to the SLAAC1-V board and host computer upon completion of a series of radiation tests.");


function hideAll()
{
	for(var j=0; j<allDivs.length; ++j)
	{
		var tempStr = allDivs[j].id.substr(0,4);
		
		if(tempStr == 'link')
		{
			allDivs[j].style.borderBottom = '0px';//link
		}
		else if(tempStr == 'menu')
		{
			allDivs[j].style.visibility = 'hidden';//menu
		}
	}
}

function changeText() {

	 alert(allDivs["text1"]);
	 alert(allDivs["text1"].name);

}


function showMenu(menuString)//ie. menuString = 1.2
{
	//split up menu
	var menuArray = menuString.split('.');
	//hide all menus
	hideAll();
	
	//show menu/link
	var tempMString = menuArray[0];
	var zIndex = menuArray.length + 1;
	var menuWidth = allDivs["basemenu"].style.width;
	menuWidth = menuWidth.replace(/\D/g,"");
	
	for(var i=0; i<menuArray.length; ++i)
	{
		//alert(allDivs["link" + tempMString]);

		//show link underline
		allDivs["link" + tempMString].style.borderBottom = '1px solid #c5af7d';//link
		
		//determine parent layer's height
		var numLinks = 3;
		var checkID = "";
		//setup the req expression
			var checkIDRegEx = "";
			if(i==0)//root level
				checkIDRegEx = new RegExp("link\\d+");
			else//sub menu
			{
				checkIDRegEx = new RegExp("link" + menuArray[i-1] + "\\.\\d+");
			}
			
		for(var j=0;j<allDivs.length;++j)
		{		
			checkID = allDivs[j].id.match(checkIDRegEx);//match the root level
			
			if(allDivs[j].id == checkID)
			{
				++numLinks;
			}
		}
		
		//position menu and show it
		var leftSpace = menuWidth * (i + 1);
		allDivs["menu" + tempMString].style.left = leftSpace + 'px';
		allDivs["menu" + tempMString].style.minHeight = numLinks * 20 + 'px';
		allDivs["menu" + tempMString].style.height = numLinks * 20 + 'px';
		allDivs["menu" + tempMString].style.visibility = 'visible';//menu
		allDivs["menu" + tempMString].style.zindex = zIndex--;//menu
		
		if(i + 1 < menuArray.length)
			tempMString += "." + menuArray[i+1];
	}
	
	//store current menu in cookie
	setIDinCookie(menuString);
	
	return false;
}

function autoForward()
{
	if(!document.all && navigator.appVersion.charAt(0) != "5")
	{
		alert("Pop-out menu navigation will not work properly with this browser.");
		//window.location = 'http://www.urlThatDoesntUsePopOutMenu.edu';
	}
	else
	{
		var menuString = getIDfromCookie();
		if(menuString.length > 0)
		{
			showMenu(menuString);
		}
	}
}

function getIDfromCookie()
{
	var NameOfCookie = "menuID";

	if (document.cookie.indexOf(NameOfCookie) != -1) 
	{ 
		begin = document.cookie.indexOf(NameOfCookie+"=");//look for cookie
		if (begin != -1)
		{ 
			begin += NameOfCookie.length+1; 
			end = document.cookie.indexOf(";", begin);
			if (end == -1)
			{
				end = document.cookie.length;
				return unescape(document.cookie.substring(begin, end));
			} 
			return unescape(document.cookie.substring(begin, end));
		}
		return '1'; 
	}
	return '1';
}

function setIDinCookie(idValue) 
{
	var NameOfCookie = "menuID";
	var expiredays = null;

	var ExpireDate = new Date ();
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
	
	document.cookie = NameOfCookie + "=" + escape(idValue) + 
	((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

function changeFeature(increment) {

//  images = new Array("images/feature/sittingGirl.gif",
//		     "images/feature/spring.gif",
//		     "images/feature/accelerator.gif",
//		     "images/feature/geiger.gif");
//  captions = new Array("this is a sitting girl",
//       	               "it's spring time",
//		       "proton accelerator",
//		       "geiger counter");
  imageCount = images.length;

  var imageIndex = 0;
  var cookievalue = getCookie("featureNumber");
  if (cookievalue == null) {
    imageIndex = Math.floor(Math.random()*imageCount);
  } else {
    imageIndex = parseInt(cookievalue);
  }
  featureTable = allTables["featureTable"];
  textField = allDivs["featureText"];
  imageIndex += increment;
  if ( imageIndex == imageCount ) {
    imageIndex=0;
  }
  if ( imageIndex < 0 ) {
    imageIndex = imageCount-1;
  }
  setCookie("featureNumber",imageIndex+"");
  featureTable.setAttribute("background",images[imageIndex]);
  textField.childNodes[0].nodeValue = captions[imageIndex];
}

function resetCookie() {

  var imageIndex = 0;
  setCookie("featureNumber",imageIndex+"");

}

//optional arguments as follows: name, value, expires, path, domain, secure
function setCookie(name, value) {
   var arg_value = setCookie.arguments;
   var arg_length = setCookie.arguments.length;
   var expires = (arg_length > 2) ? arg_value[2] : null;
   var path = (arg_length > 3) ? arg_value[3] : null;
   var domain = (arg_length > 4) ? arg_value[4] : null;
   var secure = (arg_length > 5) ? arg_value[5] : false;
   document.cookie = name + "=" + escape(value) +
     ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
     ((path == null) ? "" : ("; path=" + path)) +
     ((domain == null) ? "" : ("; domain="+domain)) +
     ((secure == true) ? "; secure" : "");
}

function getCookie(name) {
   name += "=";
   var length = name.length;
   var cookie_length = document.cookie.length;
   var i = 0;
   while (i < cookie_length) {
     var j = i + length;
     if (document.cookie.substring(i,j) == name)
       return getCookieValue(j);
     i = document.cookie.indexOf(" ",i) + 1;
     if (i == 0)
       break;
   }
   return null;
}

function getCookieValue(position) {
   var end = document.cookie.indexOf(";",position);
   if (end == -1)
     end = document.cookie.length;
   return unescape(document.cookie.substring(position,end));
}

