/* breadcrumb.js :: MattH :: 03.03.2008
* Usage:
* Just include this script in the <head> of your page, 
* and create an element on the page with an id of "breadcrumb."
* This is where the breadcrumb nav will go.
*
* requires jquery for document.ready
*/
makeCrumb = {
	init : function() {
		if (!document.getElementById) { return; };
		makeCrumb.buildURL();
	},
	addSpaces : function(str) {
		//turn any underscores in the URL into spaces
		str = str.replace(/_/g," ");
		return str;
	},
	stripExtension : function(str) {
		//strip the extension off of the name of a resource in the URL
		var fileParts = new Array();
		fileParts = str.split(".");
		return fileParts[0];
	},
	buildURL : function(){
		// Assemble the breadcrumb pieces
		var URL = location.href;
		var URLparts = new Array();
		var crumbURL = '/';
		var crumbString = '<a href="/">Home</a> > ';
		var breadcrumb = document.getElementById("breadcrumb"); //find where the breadcrumb goes in the DOM

		URLparts = URL.split("/"); //break up the URL by the slashes

		var URLlength = URL.length-1 == URL.lastIndexOf('/') ? URLparts.length-1 : URLparts.length;
		var currentPage = URLparts.pop(); //get the name of the current page from the URL				

		for (i=3;i<URLlength-1;i++){
			crumbURL += URLparts[i] + '/';	
			crumbString += '<a href="' + crumbURL + '">' + makeCrumb.addSpaces(URLparts[i]) + '</a> > ';
		};

		if (URL.length-1 == URL.lastIndexOf('/')){
			crumbString += makeCrumb.addSpaces(URLparts.pop()); // If this is an index page, use the name of the directory.
		} else {
			currentPage = makeCrumb.addSpaces(currentPage);
			crumbString += makeCrumb.stripExtension(currentPage); // otherwise, use the name of the file
		};

		breadcrumb.innerHTML = crumbString; //put the breadcrumb where it's supposed to go
		breadcrumb.style.textTransform="capitalize"; //Upcase the text in the breadcrumb
		
		//add tertiary nav
		/*if (URLparts.length >= 5) {
			var tertiaryText = makeCrumb.addSpaces(URLparts[4]);		
			$('#c2 div').prepend('<div id="tertiary">' + tertiaryText + '</div>');
		}*/
	}
} //TODO: figure out the best way to do the event handling

$(document).ready(function() {
	makeCrumb.init();
});