/**This script creates a 'breadcrumb' trail for a file directory tree, such as a webpage.(See documentation for a
more complete description).

By Craig Allen with help from Harry Love
**/


	
/*
CUSTOMIZATION SECTION
The following are variables that can be customized 
*/

var linkSeparator = "&nbsp;>&nbsp;"; //the character between the links in the breadcrumb
var homePageTitle = "Libraries Home"; //the title of for the domain link or homepage
var startLevel = 0; //the starting level for the breadcrumbs; 0 includes the homepage
var firstLetterUpperCase = true; //if you want to capitalize the first letter in the title
var trimCharacter = " "; //this character is trimmed off the beginning of the page title

var titleSeparator = new Array ([":"],[": "],["- "],[", "]); //Any character appearing before one of these in the title will not be shown

//Any page title appearing in the zeroth index of the array will be replaced by the string in the first index
var titleSubstitution = new Array (
	[ "dbtw-wpd" , "Inmagic Databases" ],
	[ "exec" , "" ],
	[ "staffdir" , "Staff Directory" ]
	);

/**END OF CUSTOMIZATION SECTION**/

var doc = document; //the current html page

/**
The main function of this script. Seperates the URL into sub-URLs and gives the sub-URLs to the two other main functions:
getTitle and printCrumb
**/
function createBreadcrumbs(){


var currentURL = doc.URL;
var subURL;  //holds the sub-URLs
var level = startLevel;

var domain = findDomain(currentURL); //finds the current domain based off the string URL using a findDomain helper function I created
subURL = currentURL.substring(currentURL.lastIndexOf(domain), domain.length + 1); //subURL gets the domain string URL


var length = currentURL.lastIndexOf(domain) + domain.length + 1;
var totalLength = currentURL.length;

//if level starts at 0 print the domain breadcrumb
if(startLevel == 0){

printCrumb(subURL, homePageTitle, level);
level++;
}


		//Find all subURLs in the string URL, get their title, and print them
		for(var j = length; j < totalLength; j++){
		
		if( currentURL.charAt(j) == '/' || j == currentURL.length -1){
		subURL = (currentURL.substring(0, j)).toLowerCase();
		
		if( (j == currentURL.lastIndexOf("/") && j == currentURL.totalLength) || j == currentURL.length -1){
		level = -1;  //-1 indicates the last level
		}
		
		//get the page title
		var crumbTitle = getTitle(subURL, level);
		
		//Capitalizes first letter
		if(firstLetterUpperCase == true){
		crumbTitle = capitalizeFirstLetter(crumbTitle);
		}
		
		//print the page title and the URL
		printCrumb(subURL, crumbTitle, level);

		}
		
	level++;
	
	}
}

//This function gets the title corresponding to the string subURL it is given
function getTitle(subURL, level){

var crumbTitle;

//This is the end level, so return the html page title
if(level == -1){
crumbTitle = doc.title;
}

//Otherwise take the title from the URL token and search for substitutions from the array
else{

	crumbTitle = getURLToken(subURL);
	for (i = 0; i < titleSubstitution.length; i++){
		
		tempSub = titleSubstitution[i][0];
		tempSub = new String(tempSub);
		
		if(crumbTitle == tempSub){
		crumbTitle = titleSubstitution[i][1];
		}
	}

}

crumbTitle = separateTitle(crumbTitle); //separate the title using the titleSeparator array characters and the separateTitle function I created
crumbTitle = trim(crumbTitle, trimCharacter); //trim any 'trimCharacter' from the beginning of the title using a function I created
return crumbTitle;

}

//Prints the breadcrumb on the html page
function printCrumb(subURL, crumbTitle, level){

//domain level
if(level == 0){
doc.write ('<a href="' + subURL + '">' + crumbTitle + '</a>');
}
//any level except domain or last level
else if(level > 0) {
doc.write(linkSeparator + '<a href="'+ subURL +'">'+ crumbTitle +'</a>');
}
//last level
else if(level == -1){
doc.write(linkSeparator + '<strong>'+ crumbTitle +'</strong>');
}

}
//a helper function to return a string of characters after the last "/" in the URL

function getURLToken(subURL){

var i = subURL.lastIndexOf("/");
subURL = subURL.substring(i + 1);
return subURL;
}

//a helper function to return a crumbTitle without any characters before a title separator, 
//which are specified by the titleSeparator array
function separateTitle(crumbTitle){

var shouldSeparate = false;

var separatorPosition = Number.MIN_VALUE;
	
	//cycles through searching for any title separators 
	for (i = 0; i < titleSeparator.length; i++){

	tempSeparator = titleSeparator[i];
	tempSeparator = new String(tempSeparator);
	var n = crumbTitle.lastIndexOf(tempSeparator) + tempSeparator.length;

	if( n > 0){
	//n = crumbTitle.lastIndexOf(tempSeparator) + tempSeparator.length;
	shouldSeparate = true;
	
	//finds the greatest index of a separator
	if (n >= separatorPosition){
	separatorPosition = n;
		}
	}	
	}
	
if (shouldSeparate == true){
crumbTitle = crumbTitle.substring(separatorPosition -1);
}

return crumbTitle;
}

//a helper function that trims any 'trimChar' from the 'string'
function trim(string, trimChar){

var i = 0;
while(string.charAt(i) == trimChar){
string = string.substring(i + 1);
i++;
}
return string;
}

//a helper function that finds the domain of the currentURL string assumes that
// the domain has alteast two '/'s separated by atleast one character
function findDomain(currentURL){

var tempURL = currentURL;

var domainFound = false;

while(!domainFound){

var i = tempURL.indexOf("/");


var nondashFound = false;
while(!nondashFound){
if( tempURL.charAt(i+1) == "/"){
i++;

}
else{

nondashFound = true;
}
}

i = tempURL.indexOf("/", i + 1);
tempURL = tempURL.substring(0, i);
domainFound = true;

}
return tempURL;
}

//a helper function that capitalizes the first letter of a string
function capitalizeFirstLetter(string){

uppercaseText = string.substr(0,1).toUpperCase();
lowercaseText = string.substr(1, string.length);
string = uppercaseText+lowercaseText;
return string;	
}
