/* 

breadcrumbs.js: dynamically generate a "breadcrumb trail" - style navigation 
menu (Home > Directory > Structure > You > Are > Here) for any web page

Copyright 2001 by Kevin Foster (fostek@rpi.edu) - All Rights Reserved

*/

// -------------------------------------------------------------------------- //

/* 
	NOTES:
	
	1) for this script to function properly, all directories in the path from 
	root to the calling file must have a default page that your server is 
	configured to recognize (i.e.; "index.html")
*/
	var DefaultPage = 'index.shtml';
/*
	if your server is set to use another default page name (such as 
	"default.html", or "*.htm, *.shtm or *.shtml"), put that name into the 
	"DefaultPage" variable above (between the quotes)
	 
	--
	
	2) other variables you may want to change are the style of separator used 
	in the breadcrumb trail and the text used for the link to your main index 
	page. here they are:
*/
	var Separator = ' > ';
/*
	if you want to use a separator other than the default (" > "), put it in 
	the "Separator" variable (between the quotes). you can use an image instead 
	of plain text if you want to: just put a regular <img> tag in place of the 
	default separator text - for example:
	
	var Separator = '<img src="image_name.gif">'
*/
	var Home = 'EWP Homepage';
/*
	if you want your main index page referred to as something other than "Home" 
	(i.e.; '[your_website/school/company_name] Home'), change the "Home" variable
	
	--
	
	3) if you use directory names that don't 'translate' well into links in the 
	trail (i.e.; directory names with underscores or directory names that are 
	abbreviations or acronyms that won't be immediately obvious to your visitors),
	use the following array to define names to be displayed as link text in the 
	trail for those directories - the keys are the actual directory names and the 
	values are the 	names you want displayed for those directories in the trail -
	
	readable_directories["directory name"] = "name to display in the trail";
	
*/
	var readable_directories = new Array();
		readable_directories["academic_advising"] = "Academic Advising";
		readable_directories["bi"] = "Bioinformatics";
		readable_directories["biol"] = "Biology";
		readable_directories["chem"] = "Chemistry";
		readable_directories["comm"] = "Language, Literature, and Communication";
		readable_directories["course_catalog"] = "Course Catalog";
		readable_directories["csci"] = "Computer Science";
		readable_directories["dses"] = "Decision Sciences and Engineering Systems";
		readable_directories["ecse"] = "Electrical, Computer and Systems Engineering";
		readable_directories["engr"] = "Nuclear Engineering";
		readable_directories["epow"] = "Electric Power Engineering";
		readable_directories["meae"] = "Mechanical Engineering, Aeronautical Engineering, and Mechanics";
		readable_directories["mane"] = "Mechanical, Aerospace, and Nuclear Engineering";
		readable_directories["mediadev"] = "Media Development";
		readable_directories["mf"] = "Multiphase Flow";
		readable_directories["mgmt"] = "Lally School of Management and Technology";
		readable_directories["mtle"] = "Materials Engineering";
		readable_directories["nva"] = "Northern Virginia";
		readable_directories["sis_info"] = "SIS Info";
		readable_directories["utc"] = "UTC";
		readable_directories["wbt"] = "Web Based Training";
/*
	
	--
	
	4) call this script from any web page by adding this script tag wherever you 
	want the trail to appear:
	
	<script language="JavaScript" type="text/javascript" src=""></script>
	
	where the "src" attribute is the path (or url) to "breadcrumbs.js"
	
	--
	
	5) you should add a ".crumbs" class to your style sheet or take the 
	'<div class="crumbs">' tag out of the opening "document.write" statement of 
	the trail and its closing '</div>' tag out of the document.write statements 
	in the last "if/else" block.
*/

// -------------------------------------------------------------------------- //

/* 
	the methods - declare method prototypes to duplicate certain Perl functions 
	(shift, pop, push) and the "x" operator ('repeat')
*/

// -------------------------------------------------------------------------- //

// shift(): remove the first element of an array, shorten the array.length
// attribute by one and return the value of the element that was removed

function shift(array) {
	var val = this[0];
	for (var i = 1; i < this.length; ++i) {
		this[i-1] = this[i];
	}
	this.length--;
	return val;
}

// declare "shift" a prototype of the array object
Array.prototype.shift = shift;

// -------------------------------------------------------------------------- //

// pop(): remove the last element of an array, shorten the array.length attribute
// by one and return the value of the element that was removed

function pop(array) {
	var val = this[this.length - 1];
	this.length--;
	return val;
}

// declare "pop" a prototype of the array object
Array.prototype.pop = pop;

// -------------------------------------------------------------------------- //

// push(): add an element to the end of an array

function push() {
	var sub = this.length;
	for (var i = 0; i < push.arguments.length; ++i) {
		this[sub] = push.arguments[i];
		sub++;
	}
}

// declare "push" a prototype of the array object
Array.prototype.push = push;

// -------------------------------------------------------------------------- //

// repeat() (replaces Perl's "x" operator): repeat a string "n" times (actually,
// concatenate the string with itself "n" times) and return the new string

function repeat(n) {
	var text = "";
	var str = this.toString();
	while (n > 0) {
		text += str;
		n--;
	}
	return text;
}

// declare "repeat" a prototype of the string object
String.prototype.repeat = repeat;

// -------------------------------------------------------------------------- //

/* 
	the functions:
*/

// -------------------------------------------------------------------------- //

// parseDirectories(): take the url of the page this script is being called from
// and return an array containing the names of any directories between root and
// the calling file (the last element of the array will be the file name)

function parseDirectories(url) {
	var domainEnd = (url.indexOf("/", 7) + 1);
	var structure = url.substring(domainEnd, url.length);
	var directories = structure.split("/");
	return directories;
}

// -------------------------------------------------------------------------- //

// readable(): if this directory name is a key in readable_directories, return
// its corresponding "display name"; otherwise, title case it, remove any
// underscores, and return it

function readable(str) {
	var newString = readable_directories[str] ? readable_directories[str] : "";
	if (newString == "") {
		var first = str.charAt(0);
		var rest = str.substring(1, str.length);
		var cap = first.toUpperCase();
		var newString = (cap + rest); // capitalize it
		newString = newString.split("_"); // if there are underscores separating 
						// multiple words in the directory name, get rid of them
		newString = newString.join(" "); // and replace them with spaces
	}	
	return newString;
}

// -------------------------------------------------------------------------- //

// parents(): take the length of the "directories" array and generate a new array
// with the "directories" array's indices reversed (i.e.; if directories.length
// is 4, the "jumps"  array will be (4,3,2,1))

/* 
	- the number of repetitions of "../" needed to reach the root directory from 
	any given page is equal to the 'array.length'parameter of "directories" -
*/

function parents(data) {
	var jumps = new Array();
	for (var i = data.length; i > 0; i--) {
		var count = i;
		var newArray = jumps.push(count);
	}
	return jumps;
}

// -------------------------------------------------------------------------- //

// jumpers(): spit out the directory jump sequence ("../") 'n' times, where 'n' 
// is the number passed in in 'data'

function jumpers(data) {
	var num = parseInt(data);
	var jumper = "../";
	if (num == 1) {
		var jump = jumper;
	} else {
		var jump = jumper.repeat(num);
	}
	return jump;
}

// -------------------------------------------------------------------------- //

// get the specific variable values for the calling page
var page = document.title; // used as the last element of the breadcrumb trail
var url = location.toString(); // the full url, w/protocol
if (url.substr(url.length-1) == '/') {
	url = url.substr(0,url.length-1) + '/' + DefaultPage;
}
var directories = parseDirectories(url); // construct the "directories" array
// take the file name off of the end of the directories array and save it
var filename = directories.pop();
// construct a reversed numeric index of the directories array
var folders = parents(directories);
// take the first element off the folders array and use it to generate the 
// number of jumpers needed to reach the root directory
var topFolder = folders.shift();

// and write the trail
document.write("<NOINDEX><div class=\"crumbs\"><a href=\"" + jumpers(topFolder) + "\">" + Home + "</a>");
// loop through the remaining directories and generate links and link text for each
for (var i = 0; i < directories.length; i++) { 
	if (folders[i] != null) {   
		/* if the folders array has an element for the current index value of directories[i], use 
		that element's index to generate the number of jumpers needed to reach the current directory */
		document.write(Separator + "<a href=\"" + jumpers(folders[i]) + DefaultPage + "\">" + readable(directories[i]) + "</a>");
	} else if (filename != DefaultPage) { 
		/* when folders[i] returns null, that means we're in the same directory as the calling file, 
		so no directory jumpers are needed; but, if the calling file is the index page of the current directory, 
		we don't want a link to it in the trail, so we'll skip the next step and go directly to the final element */	 
		document.write(Separator + "<a href=\"" + DefaultPage + "\">" + readable(directories[i]) + "</a>");
	}
}
if (page) { // if the current document has a title, print it as the final element of the trail
	document.write(Separator + "<strong>" + page + "</strong><hr></div></NOINDEX>");
} else { // otherwise, print a generic final element for the trail
	document.write(Separator + "<strong>You Are Here</strong><hr></div></NOINDEX>");
}
