
// this is the string corresponding to the top most directory of the site
rootString = "prl_site"

// cross compatibility function to get the previous sibling node
function realPreviousSibling(node){
  var tempNode=node.previousSibling;
  while(tempNode.nodeType!=1){
    tempNode=tempNode.previousSibling;
  }
  return tempNode;
}

// function to dynamically change the height of the displayed contents and center the contents vertically
function sizeContents()
{
	var totalHeight  = document.getElementById("wrapper").offsetHeight;
	var headerHeight = document.getElementById("header").offsetHeight;
	var footHeight   = document.getElementById("footer").offsetHeight;
	var wrapperHeight = totalHeight-headerHeight-footHeight;
	
	// constrain wrapper height to a minimum
	if (wrapperHeight < 395)
		wrapperHeight = 395;
	
	document.getElementById("wrapper2").style.height = wrapperHeight + "px";

}
/*
// function to show descriptions for the main shop areas
function showInfo(areaDescription) 
{
	document.getElementById("info").innerHTML = document.getElementById(areaDescription).innerHTML;
}
*/

// function to show descriptions for the main shop areas
function showInfo(area) 
{
	document.getElementById(area).style.visibility = "visible";
	realPreviousSibling(document.getElementById(area)).style.backgroundColor = "#62171b";
}

// function to hide descriptions for the main shop areas
function hideInfo(area) 
{
	document.getElementById(area).style.visibility = "hidden";
	realPreviousSibling(document.getElementById(area)).style.backgroundColor = "transparent";
}

// low level function to make an element visible
function showElement(elementName)
{
	document.getElementById(elementName).style.visibility = "visible";
}

// low level function to hide an element
function hideElement(elementName)
{
	document.getElementById(elementName).style.visibility = "hidden";
}
/*
// low level function to show a background image in an element
function showBackground(elementName,fileName)
{
	document.getElementById(elementName).style.backgroundImage = "url(" + fileName + ")";
}

// low level function to remove a background image in an element
function hideBackground(elementName) {
	document.getElementById(elementName).style.backgroundImage = "none";
}
*/
// function to switch out the actual content
// all the navigation remains the same, except the breadcrumbs which are updated in the process
function loadContent(fileName)
/*
{
	// first clone the content window -- this is contained in the <object> tag
	var contentArea = top.document.getElementById("content_window");
	var contentClone = contentArea.cloneNode(true);
	// assign a new html file to the clone
	contentClone.data = fileName;
	
	// now remove the original from the parent, then append the clone
	var placeHolder = top.document.getElementById("content");
	placeHolder.removeChild(contentArea);
	placeHolder.appendChild(contentClone);
	// update variable name for neatness
	contentArea = contentClone;
	
}
*/

{
	top.document.getElementById("content_window").src = fileName;
}


// function to shorten the main menu bar when content is accessed
function switchViewContent()
{
	top.document.getElementById("navbar").style.width = "109px";
	top.document.getElementById("content").style.width = "767px";
	document.getElementById("content_body").style.width = "707px"
	
	top.document.getElementById("machineshop_descr").style.display = "none";
	top.document.getElementById("modelshop_descr").style.display = "none";
	top.document.getElementById("foundry_descr").style.display = "none";
	top.document.getElementById("cadloft_descr").style.display = "none";
	top.document.getElementById("studentwork_descr").style.display = "none";
	top.document.getElementById("videos_descr").style.display = "none";
	
	top.document.getElementById("nav_machine").style.backgroundImage = "none";
	top.document.getElementById("nav_model").style.backgroundImage = "none";
	top.document.getElementById("nav_foundry").style.backgroundImage = "none";
	top.document.getElementById("nav_cadloft").style.backgroundImage = "none";
	top.document.getElementById("nav_studentwork").style.backgroundImage = "none";
	top.document.getElementById("nav_videos").style.backgroundImage = "none";
}

// function to extend the main menu bar in the Home screen
function switchViewMain()
{
	top.document.getElementById("navbar").style.width = "259px";
	top.document.getElementById("content").style.width = "617px";
	document.getElementById("content_body").style.width = "557px"
	
	top.document.getElementById("machineshop_descr").style.display = "block";
	top.document.getElementById("modelshop_descr").style.display = "block";
	top.document.getElementById("foundry_descr").style.display = "block";
	top.document.getElementById("cadloft_descr").style.display = "block";
	top.document.getElementById("studentwork_descr").style.display = "block";
	top.document.getElementById("videos_descr").style.display = "block";
	
	top.document.getElementById("nav_machine").style.backgroundImage = "url(./machining-color.jpg)";
	top.document.getElementById("nav_model").style.backgroundImage = "url(./modelshop-color.jpg)";
	top.document.getElementById("nav_foundry").style.backgroundImage = "url(./welding-color.jpg)";
	top.document.getElementById("nav_cadloft").style.backgroundImage = "url(./cadloft-color.jpg)";
	top.document.getElementById("nav_studentwork").style.backgroundImage = "url(./placeholder.jpg)";
	top.document.getElementById("nav_videos").style.backgroundImage = "url(./placeholder.jpg)";

}


// function to display the breadcrumbs
function breadcrumbs() 
{
	// get the location as a string
	var bcString = location.href;
	
	// get the index of the forward slash after the root directory
	var index = bcString.indexOf(rootString);
	index = index + rootString.length + 1;
	
	// get the full root directory
	var rootDir = bcString.slice(0,index);
	
	// slice the string to retain only what lies beyond the first forward slash after the root string
	// ie if the path of the document is ...../<rootString>/<everythingElse>, we only want <everythingElse>
	bcString = bcString.slice(index,bcString.length);
	
	// replace all underscores with a space
	bcString = bcString.replace(/_/g," ");
	
	// initialize a variable to 0
	var x = 0;
	
	// find the index of the next forward slash -- this marks a directory
	var nextIndex = bcString.indexOf("/")
	
	// initialize variables
	var output   = new String;	
	var branches = new Object;
	
	// loop through as long as we keep finding forward slashes
	while (nextIndex != -1)
	{
		// isolate the next folder name
		branches[x] = bcString.slice(0,nextIndex);
		// slice the string to retain only the rest of the path
		bcString = bcString.slice(nextIndex+1,bcString.length);
		// find the index of the next forward slash
		nextIndex = bcString.indexOf("/");
		// increment x
		x++;
	}
	// now bcString should be the final html file
	// convert this to the last element in branches
	nextIndex = bcString.indexOf(".")
	branches[x] = bcString.slice(0,nextIndex);
	
	
	// we now have a matrix "branches" whose elements are the names of the directories in the hierachy
	// last element is simple to name of the file
	// these correspond to the breadcrumb titles
	// now we need to create the links
	
	// loop through the breadcrumb titles
	for (var i in branches)
	{
		// this is the starting of the link
		output += "<a class=\"breadcrumb\" onClick=\"loadContent('" + rootDir;
		
		// the further along an element is in branches, the deeper in hierachy
		// march through a loop, with repititions equal to the position within branches
		for (var j=0;j<=i;j++)
		{
			// general case: append the next directory name to the string, converting spaces to underscores
			// it is only a directory if there is at least one more element in the matrix,
			// hence the j-1 construct
			if (j != 0)
					output += branches[j-1].replace(/ /g,"_") + "/";
			// if we are at the last element in the matrix, convert it to the actual filename
			if (j == i) 
			{
					output += branches[j].replace(/ /g,"_") + ".html";
			}
		}
		if (i == 0)
			output += "')\">Home</a>";
		else
			output += "')\">" + branches[i] + "</a>";
			
		if (i != x)
			output += " > ";
	}
	
	//output += "<span class=\"breadcrumb_last\">" + document.title + "</span>";
	
	top.document.getElementById("breadcrumbs").innerHTML = "<p>" + output + "</p>";
	

}

window.onresize = sizeContents;
//document.getElementById("mainbody").addEventListener('resize',sizeContents,true);
//document.getElementById("mainbody").onclick = sizeContents;
//document.getElementById("nav_model").addEventListener('mouseover',showInfo('modelshop_description'),true);
