// Setup random banner images.
images = new Array;
images[1] = "/images/banner1.jpg";
images[2] = "/images/banner2.jpg";
images[3] = "/images/banner3.jpg";
images[4] = "/images/banner4.jpg";
images[5] = "/images/banner5.jpg";
images[6] = "/images/banner6.jpg";
var banner = images[Math.round( (5) * Math.random()) + 1];

// Function to goto a URL
function gotoURL(url) {
	window.location = url;
}

// Functions for showing/hiding elements.
function show(id) {
	var d = document.getElementById(id);
	for (var i = 1; i<=10; i++) {
		if (document.getElementById('submenu'+i)) {document.getElementById('submenu'+i).style.display='none';}
	}
	if (d) {d.style.display='block';}
}
function toggle(id) {
	var d = document.getElementById(id);
	if (d) {
		if (d.style.display=='block') {
			d.style.display='none';
		} else {
			d.style.display='block';
		}
	}	
}
function hideexcept(id) {
	sections = new Array("about","programs","participate","media","publications");
	for (x in sections) {
		document.getElementById(sections[x]).style.display='none';
	}
	document.getElementById(id).style.display='block';
}

function check_location(page_path){
	var temp=page_path;
	return temp.indexOf("/nexus/preview/");
}
function replace_(node_name){
	var temp = node_name;
	while (temp.indexOf("_")>-1){
		temp=temp.replace("_"," ");
	}
	return temp;
}
function getUL(){
	var ULnum = document.getElementsByTagName("UL");
	return ULnum.length;
}

function rewrite_URL(){
	var LI_obj= document.getElementsByTagName("LI");
	for(j=0;j<LI_obj.length;j++){
		var href_obj=LI_obj[j].getElementsByTagName("a")[0].getAttribute("href",2);
		LI_obj[j].getElementsByTagName("a")[0].setAttribute("href","/"+href_obj);

	}
}
function add_slash_to_folders(){
	var LI_obj= document.getElementsByTagName("LI");
	for(j=0;j<LI_obj.length;j++){
		var href_obj=LI_obj[j].getElementsByTagName("a")[0].getAttribute("href",2);
		if(href_obj.indexOf(".html")==-1 && href_obj.indexOf(".xml")==-1){href_obj=href_obj+"/";
			LI_obj[j].getElementsByTagName("a")[0].setAttribute("href",href_obj);
		}

	}
}


function rearrange(){
	var ULArray = getUL(); // Get the number UL elements on the menu.
	if(navigator.appName.indexOf("Microsoft")!=-1){// For browser compatibility.
		class_name="className";
	}
	if (ULArray>1){ // If UL element is more than one (sub menu)
		var ulIdArray = document.getElementsByTagName("UL");// Get the UL elements as an array.
		for (var i=(ULArray-1) ; i>0 ; i--){ // Loop thru the UL elements
			// Get the Id of the UL element
			var ulId = ulIdArray[i].getAttribute('id');
			// See if there's an UL id on an LI element inside another UL.
			var liId = document.getElementById(ulId+"_li");
			if(liId!=null){// If liId is not null means there is an LI equivalent
				// Put class on for the LI element where the UL's id matched.
				//liId.getElementsByTagName("a")[0].setAttribute(class_name,"on");
				// Append UL element to the LI equivalent.
				document.getElementById(ulId+"_li").appendChild(document.getElementById(ulId));
				

			}// End if liId!=null
			else {// If there is no LI equivalent, then UL element(s) must be hidden or from another site.
				if(ulId=="Website"){// If ulId is website, then this is another site.
					// Get the div object/node where the menu items are located.
					var main_menu_div =document.getElementById("nav");
				 	for(var j=i; j>0;j--){// Loop to the rest of the UL elements which are not part of the site.
						// Remove the UL elements.
						main_menu_div.removeChild(document.getElementById(ulIdArray[j-1].getAttribute('id')));
					}// End of For loop
					i=0;// To exit the main For loop.
				}// End of If ulId is Website
				else { // If ulId is not Website (this folder and anything inside are hidden)
					// Remove the UL elements.
					document.getElementById("nav").removeChild(document.getElementById(ulId));
				}// End of Else   
			}//End of Else
			
		}// End of For loop
	}// End of if there are more than one UL elements
	// Check if the page is visible. If it is, then put the class on attribute.
	if (document.getElementById(page_name+"_li")!=null){
			document.getElementById(page_name+"_li").getElementsByTagName("a")[0].setAttribute(class_name,"on");
	}// End if page_name_li is not null
	if	((node_name=="pagenotfound" | node_name=="pagemoved") && check_location(preview)==-1){ // If page is filenotfound and not on preview
		rewrite_URL(); // Rewrite URL.
	}
	add_slash_to_folders(); // Add slash '/' at the end if the link is a folder. 301 fix for folders link.
}
// To show/hide the Back to Top link
function show_toplink(){
 	if((document.documentElement.clientHeight+150)<document.documentElement.scrollHeight){
		document.getElementById("back2top").style.display="block";
		document.getElementById("top_anchor").style.display="block";
	}else{
    	document.getElementById("back2top").style.display="none";
		document.getElementById("top_anchor").style.display="none";
	}
}