/*********************************************
makeNav

This function populates the navigation bar.
Id-th entry is marked as active
This function was written to avoid code 
duplication across individual pages.
*********************************************/
function makeNav(id,sub_id)
{
	/*first set corner picture*/	
	setCornerPicture(id);

	/*create arrays containing nav menu items*/

	var links=new Array();
	var names=new Array();

	names[0]="Home";
	names[1]="Projects";
	names[2]="People";
	names[3]="Papers";
	names[4]="Facility";
	names[5]="Gallery";
	names[6]="Links";
	names[7]="News";

	links[0]="index.html";
	links[1]="projects.html";
	links[2]="people.html";
	links[3]="papers.php";
	links[4]="facility.html";
	links[5]="gallery.html";
	links[6]="links.html";
	links[7]="news.html";

	/*make arrays for submenus - projects and people*/
	var people=new Array();
	var people_links=new Array();
	
	people[0]="Dr. Keidar";
	people[1]="Alex";
	people[2]="Lubos";
	people[3]="Jarrod";
	people[4]="Madhu";
	people[5]="Jian";
	people[6]="Olga";
	people[7]="TaiSen";

	people_links[0]="people_keidar.html";
	people_links[1]="people_shashurin.html";
	people_links[2]="people_brieda.html";
	people_links[3]="people_fenstermacher.html";
	people_links[4]="people_kundrapu.html";
	people_links[5]="people_li.html";
	people_links[6]="people_volotskova.html";
	people_links[7]="people_zhuang.html";

	/*make arrays for submenus - projects and people*/
	var projects=new Array();
	var projects_links=new Array();
	
	projects[0]="Ablation";
	projects[1]="Nanotubes";
	projects[2]="HET";
	projects[3]="Vacuum Arc";
	projects[4]="Biological"
	
	projects_links[0]="projects_ablation.html";
	projects_links[1]="projects_nanotubes.html";
	projects_links[2]="projects_het.html";
	projects_links[3]="projects_arcjet.html";
	projects_links[4]="projects_bio.html";

	/*get reference to the nav bar*/
	var nav=document.getElementById("nav");
	string=new String();
	
	/*populate links*/
	for (i in links)
	{
		/*is the link selected? Apply selected class and do not hyperlink*/
		if (sub_id<0 && i == id)
		{
			string+="<h5 class=\"selected\"";
		}
		else	/*add hyperlink*/
		{
			string+="<h5> <a href=\"" + links[i] + "\"";
		}
		
		string+=">" + names[i] + "</a></h5>";
		
		/*---- PROJECTS TAG--------------------------*/
		if (id == 1 && i==1)
		{
			for (j in projects)
			{
				if (sub_id>=0 && sub_id == j)
					string+="<h6 class=\"selected\"";
				else
					string+="<h6> <a href=\"" + projects_links[j] + "\"";
	
				string+=">" + projects[j] + "</a></h6>";
			}
		}

		/*---- PEOPLE TAG--------------------------*/
		if (id == 2 && i==2)
		{
			for (j in people)
			{
				if (sub_id>=0 && sub_id == j)
					string+="<h6 class=\"selected\"";
				else
					string+="<h6> <a href=\"" + people_links[j] + "\"";
	
				string+=">" + people[j] + "</a></h6>";
			}
		}

	}

	nav.innerHTML=string;
}

/*********************************************
setCornerPicture

This function places a random image in the 
top-left corner. The image data is parsed
from images.xml

Parsing code was modified from w3schools.org
*********************************************/

function setCornerPicture(nav_id)
{
var xmlDoc, index, file_name, text;
var xmlname="images.xml";

	try
	{
		if (window.ActiveXObject)
		{
			var errorHappendHere = "Check Browser and security settings";
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async=false;
			xmlDoc.load(xmlname);
		}
		else if(window.XMLHttpRequest)
		{
			var errorHappendHere = "Error handling XMLHttpRequest request";
			var d = new XMLHttpRequest();
			d.open("GET", xmlname, false);
			d.send(null);
			xmlDoc=d.responseXML;
		} else {
			var errorHappendHere = "Error.";
			xmlDoc = document.implementation.createDocument("","",null);
			xmlDoc.async=false;
			xmlDoc.load(xmlname);
		}
	}	
	catch(e)
	{
		alert(errorHappendHere);
		//alert(e.message);
		document.getElementById("nav_pic").src="default.jpg";
		return;
	}

	/*if this is the home page, pick a random entry, otherwise
	  try to retrieve cookie*/
	index=getCookie()

	if (nav_id==0 || index<0)
	{
		/*pick a random entry*/
		n_pictures=xmlDoc.getElementsByTagName('item').length;
		index=Math.floor(Math.random()*n_pictures);
		setCookie(index);
	}
	
	/*get attribute values*/
	file_name="images/"+xmlDoc.getElementsByTagName("src")[index].childNodes[0].nodeValue;
	text=xmlDoc.getElementsByTagName("text")[index].childNodes[0].nodeValue;


	/*update image data*/
	document.getElementById("nav_pic").src=file_name;
	document.getElementById("nav_pic").title=text;
}

/*********************************************
setCookie

This function sets a cookie containing the 
index of the selected corner picture
*********************************************/
function setCookie(index)
{
	/*have cookie expire in a day*/
	exp_date=new Date();
	exp_date.setDate(exp_date.getDate()+1);
	document.cookie="npnl_image=" + index + ";expires="+exp_date.toGMTString();
}

/*********************************************
getCookie

This function reads cookie containing the 
index of the selected corner picture. It returns
the index or -1 if cookie not found
*********************************************/
function getCookie()
{
	index=-1;
	if (document.cookie.length>0)
  	{
  		/*here I am assuming we have only one cookie set*/

		cookie_string="npnl_image=";
		c_start=document.cookie.indexOf(cookie_string);

  		if (c_start!=-1)
    		{
   			c_start=c_start+cookie_string.length; 
  			c_end=document.cookie.indexOf(";",c_start);
   		 	if (c_end==-1) c_end=document.cookie.length;
    			index= parseInt(document.cookie.substring(c_start,c_end));
		}		 
  	}

	return index;
}