// Dynamic Breadcrumbs
// Written by Harry Love
// Email: hlove@u.washington.edu
// Use at your own risk.
// Feel free to use, personalize, improve, and distribute.
// Last update: September 2, 2003

// To place breadcrumbs in your page, save this
// file to a directory that makes sense to you.
// Link to it in the <head> portion of your web page.
// In the <body> portion of your page,
/* paste the following line of code:
<script type="text/javascript">createBreadcrumbs();</script>
DO NOT COPY THIS LINE */

// ***************************
// BEGIN BASIC CUSTOMIZATION *
// ***************************

// Starting Point: 1 = domain, 2 = 1st directory, 3 = 2nd directory, etc.
// Must be specified as an integer; no floating point numbers.
// ***Note*** If the starting number is greater than the number of 
// directories you have, the URL will be undefined.
var startPoint = 1; // idea from Hassan Schroeder

// Separator: set the character(s) that will be used to
// separate each of the breadcrumb links.
// Set the space in between each link by 
// specifying a number of non-breaking spaces (&nbsp;).
// Theoretically, the separator could be any valid HTML.
// The default separator is a "greater than(>)" symbol (&gt;)
// surrounded by two non-breaking spaces.
var sep = "&nbsp;&gt;&nbsp;"

// Starting Name: setting startName to "domain" will 
// use the full domain name (www.yourdomain.com).
// Any other text will be used literally.
// E.g., specifying "Home" will use "Home" as the first
// link in the chain.
// ***Note*** if startPoint is greater than 1, it will start
// the chain at a point beyond the domain.  startName will
// be used for the name of the first link, regardless of where it
// starts.
var startName = "Massachusetts Website Directory";

// Uppercase or lowercase directory names?  If
// uppercase is set to yes, the first character
// in each of the directory names will be capitalized.
// If allUppercase is set to yes, the entire directory
// name will be capitalized for each directory.
var uppercase = "yes";
var allUppercase = "no";

// Uppercase, Part 2: If your directory and document title names contain
// special characters to separate text (like an underscore) and you would like to replace
// those with white spaces (or some other character) when the text is displayed in the
// Web page, set replaceSpecialCharacters to yes.  The default is "yes."
// If you would like the character immediately following the
// special character to be capitalized, set specialCharactersUpper to yes.
// The default is "yes."
// ***Note***: If uppercase is set to "no" above, the
// specialCharactersUpper variable is ignored.
// You can specify any number of characters or strings to be replaced
// by naming them in the array "charactersToReplace" listed below.  The first
// string in quotes is the string to be replaced.  The second string of characters
// is what will be used to replace the first string.  The default array below
// replaces the underscore character (_) with a non-breaking space (&nbsp;).  To
// replace any other characters, simply copy/paste the first bracketed line of the array,
// including the brackets and the trailing comma.
// Then, replace the first and second strings with your strings.
// Advanced users can define regular expressions and replace matches with other strings.
// As stated below, leave the last line of the array alone.
// Also, do not replace the backslash character (/).  Doing so will break the script.  You're
// welcome to try it, of course, but don't say I didn't warn you.
var replaceSpecialCharacters = "yes";
var specialCharactersUpper = "yes";
var charactersToReplace = new Array (
	[ "_" , "&nbsp;" ],
	[ "/\s/g" , " " ],
	[ "Harry Love: UW Staff Member: " , " " ],
	[ "leave this alone" , "leave this alone" ]);

// Endpoint: how do you want the script to handle a
// URL that ends in a directory name? E.g., if the URL
// is http://www.mydomain.com/start/, do you want the
// script to write the name of the directory or the
// title of the default document in this directory?
// You have 2 choices: directory or title.
var endPoint = "title";

// *************************
// END BASIC CUSTOMIZATION *
// *************************

var d=document;
var url = d.location.href;
var ttl = d.title;
var endChar = url.substr(url.length-1);

// Replace the "//" in "http://www.mydomain.com" with "/" and store that in the url variable. 
url=url.replace("//","/");
var urlText = url;
	
// Split the URL into segments and store them in an array using "/" as a delimiter.
var urlLinkArray=url.split('/');

// If startName is set to domain, get the domain name here
if(startName=="domain")
{
	var y;
	startName="";
	for(x=0;x<startPoint;x++)
	{
		y=x+1;
		if(y>=1&&y<startPoint)
		{
			startName = startName + urlLinkArray[y] + "/";
		}

		else
		{
			startName = startName + urlLinkArray[y];
		}
	}
}

// Split the URL into segments again.  We do this twice in order to
// separate the link text from the display text.
// This allows us to replace characters in the text (like underscores in directory names)
// while leaving the URL link text alone.
// To make it easier to customize, a flag is created in the basic customization features
// above that looks for a "yes" to replace characters.  An array is provided to give
// users an easy way to replace several different strings.  Those strings are replaced
// here by running through a for loop.
if(replaceSpecialCharacters == "yes")
{
	for(x=0;x<charactersToReplace.length;x++)
	{
		var myRegExp = new RegExp(charactersToReplace[x][0], "g");
		urlText = urlText.replace(myRegExp,charactersToReplace[x][1]);
		ttl = ttl.replace(myRegExp,charactersToReplace[x][1]);
	}
}

// Split the segment into an array.  Make sure all characters have been
// replaced before this line, as the replace() method does not work on arrays.
var urlTextArray=urlText.split('/');

var urlL = urlLinkArray.length;
var uppercaseText;
var lowercaseText;
var lcase;
var linkName=new Array();

// According to the variables, specify upper- or lowercase directory names.
if(uppercase=="yes"&&allUppercase=="no")
{
	if(replaceSpecialCharacters=="yes"&&specialCharactersUpper=="yes")
	{
		for(x=0;x<charactersToReplace.length;x++)
		{
			var myRegExp = new RegExp(charactersToReplace[x][1]+"[a-z]", "g");
			for(y=2;y<urlL;y++)
			{
				if(urlTextArray[y].search(myRegExp)!=-1)
				{
					var regExpArray = new Array();
					regExpArray = urlTextArray[y].match(myRegExp);
					for(z=0;z<regExpArray.length;z++)
					{
						// If the array segment contains the regular expression...
						if(urlTextArray[y].search(myRegExp)!=-1)
						{
							// Split the segment into the last letter (the one to be capitalized)
							// and the characters being replaced.
							var lastLetter = regExpArray[z].substr(regExpArray[z].length-1,regExpArray.length).toUpperCase();
							// Add the characters being replaced to the new capitalized last letter
							var newText = charactersToReplace[x][1]+lastLetter;
							
							// Replace the regular expression with the new text
							urlTextArray[y] = urlTextArray[y].replace(regExpArray[z],newText);
						}
					}
				}
			}
		}
	}
	// Take the array elements one at a time and separate the first character
	// from the rest of the characters.  Capitalize the first character.
	for(x=2;x<urlL;x++)
	{
		uppercaseText=urlTextArray[x].substr(0,1).toUpperCase();
		lowercaseText=urlTextArray[x].substr(1, urlTextArray[x].length);
		linkName[x]=uppercaseText+lowercaseText;
	}
}
else if(allUppercase=="yes"||uppercase=="yes"&&allUppercase=="yes")
{
	for(x=2;x<urlL;x++)
	{
		linkName[x]=urlTextArray[x].toUpperCase();
	}
}
else{linkName=urlTextArray;lcase="yes";}

// Begin the start output variable; get the first segment (the protocol)
// of the urlLinkArray and store that in the start variable.
var start=urlLinkArray[0]+"//";

// If the startPoint is less than 1, reset to 1.
if(startPoint<1){startPoint=1;}

// Loop through all of the segments up to the
// starting point and create the opening link.
for(y=1;y<=startPoint;y++)
{
	start=start+urlLinkArray[y]+"/";
}

function createBreadcrumbs()
{
	if(lcase!="yes")
	{
		// Write the first crumb.  Use startName for the text.
		d.write('<a href="'+start+'">'+startName+'</a>');
	}

	else
	{
		// Write the first crumb.  Use startName for the text.
		// Make it lowercase
		startName = startName.toLowerCase();
		d.write('<a href="'+start+'">'+startName+'</a>');
	}
	
	
	// If there are more directories, use this code.
	// Otherwise, exit.
	if(urlL>2)
	{	
		// Loop through the remaining segments of the array.
		for(x=startPoint+1;x<urlL;x++)
		{			
			// If this segment is not the last segment...
			if(x<urlL-1)
			{
				// If this is the penultimate segment, and the
				// last segment is blank or is an anchor id...
				if(x==urlL-2&&linkName[urlL-1]=="" || x==urlL-2&&linkName[urlL-1].toString().search("#")>-1)
				{
					// If we are using the directory name
					// for the last segment...
					if(endPoint=="directory")
					{
						// Write the directory name to the page, then exit.
						d.write(sep+'<strong>'+linkName[x]+'</strong>');
						break;
					}
					
					else
					{
						if(lcase!="yes")
						{
							// This is the last segment.
							// Write it to the page, then exit.
							d.write(sep+'<strong>'+ttl+'</strong>');
							break;
						}

						else
						{
							// This is the last segment.
							// Make it lowercase.
							// Write it to the page, then exit.
							ttl = ttl.toLowerCase();
							d.write(sep+'<strong>'+ttl+'</strong>');
							break;
						}						
					}
				}
				
				// Otherwise, use this code...
				else
				{
					// Add the current start segment to the previous start segment
					// and add a backslash to the end.
					start=start+urlLinkArray[x]+"/";
					
					// Write the link to the page with sep as a separator.
					d.write(sep+'<a href="'+start+'">'+linkName[x]+'</a>');
				}
			}
			
			// Otherwise, this is the last segment...
			else
			{
				// Add the current segment to the previous segment
				// but do not add a backslash.
				start=start+urlLinkArray[x];
				
				// If this segment is a directory...
				if(endChar=="/")
				{
					// Write the last segment to the page with sep as a separator.
					d.write(sep+'<strong>'+linkName[x]+'</strong>');
				}
				
				// Otherwise this segment is a filename...
				else
				{
					// Write the link to the page with sep as a separator.
					// Use the document title for the segment name.
					d.write(sep+'<strong>'+ttl+'</strong>');
				}
			}
		}
	}
}