// Dynamic Breadcrumbs
// Written by Harry Love
// Email: hlove@u.washington.edu
// Feel free to use, personalize, improve, and distribute
// Last update: August 23, 2002

/* To create breadcrumbs in your web page, save this file
// to a directory that makes sense to you (and rename the file if you like),
// link to it in the <head> section of your web page, then
// paste the following line of code in the <body> of your web page
// where you want the breadcrumbs to appear:
<script type="text/javascript">createBreadcrumbs()</script>
DO NOT PASTE THIS LINE */

// BEGIN BASIC CUSTOMIZATION *******
// Feel free to customize any part of the script as you see fit.
// The basic customization options are contained herein.

// To customize the first link in the breadcrumb chain,
// replace the text in FirstLinkName with your text.
var FirstLinkName = "Emma Norton Home";

// To customize the last segment in the breadcrumb chain,
// use the following 3 variables:
// (1) if useDocumentTitle is set to "yes" and useFileName
// is set to "no," the last segment will be the text used
// in the <title> portion of your web page;
// (2) if useFileName is set to "yes" and useDocumentTitle
// is set to "no," the last segment will be the file name of your web page;
// (3) if variables useDocumentTitle and useFileName are set to "no,"
// the last segment will be the text you set in the LastLinkName variable.
var useDocumentTitle = "yes";
var useFileName = "no";
var LastLinkName = "Current Page";
// END BASIC CUSTOMIZATION *******

// Get the URL of the current document and store it in the loc variable 
var d = document;
var loc = d.location.href;

// Replace the "//" in "http://currentURL.com" with "/" and store that in the loc variable 
var newLoc = loc.replace("//", "/");

// **** CTG ****
// Replace the "default.htm" with "" and store that in the loc variable
var newLoc = newLoc.trim=newLoc.replace("/default.htm", "");

// **** CTG ****
// Strip the last character off the HREF if it is a slash
function stripSlash()
{
	if(newLoc.length == (newLoc.lastIndexOf("/") + 1))
	{
		var newHREF = newLoc.substr(0,(newLoc.length - 1));
	}
	else
	{
		var newHREF = newLoc;
	}
	return newHREF;
}
var newHREF = stripSlash();
// *************

// Split the URL into segments and store them in an array using "/" as a delimiter 
var linkArray = newHREF.split('/');

// In your web page, use <script> tags to call this function
function createBreadcrumbs()
{	
	// When called, create hyperlinks for breadcrumb 
	function createLink()
	{
		var looper;
		for (looper = 0 ; looper <= counter ; looper++)
		{
			if(looper == 0 )
			{
				newLink = linkArray[looper] + "//";
			}
			else
			{
				newLink = newLink + linkArray[looper] + "/";
			}
		}
		return newLink;
	}

	// **** CTG ****
	// If there are only two elements to LinkArray, then you're home so we can leave.
	if(linkArray.length == 2)
	{
		return;
	}
	// *************
	
	// Loop through the array segments and write them to the web page with their corresponding links 
	for(counter=0 ; counter < linkArray.length ; counter++)
	{
		// Is this the last array segment? 
		if(counter == linkArray.length - 1)
		{
			// Do you want to use the document file name as the last link?
			if(useFileName == "yes")
			{
				// Is this segment free of periods (.)? 
				if(linkArray[counter].indexOf(".") == -1)
				{
					// Get the first character in the segment and make it uppercase 
					// Store that character in the upperCase variable 
					var upperCase = linkArray[counter].substr(0,1).toUpperCase();
					
					// Get the rest of the string after the first character 
					// Store that in the lowerCase variable 
					var lowerCase = linkArray[counter].substr(1, linkArray[counter].length);
					
					// Is the last array segment coming up blank? 
					// We will assume it is an "index" page and call it that 
					if(linkArray[counter] == "")
					{
						// Write the last segment of the breadcrumb to the web page 
						// Write it as plain text 
						d.write("<strong> Index</strong>");
					}
					
					// Otherwise, the last segment is not coming up blank 
					// So, name it according to the string provided by linkArray 
					else
					{
						// Write the last segment of the breadcrumb to the web page 
						// Write it as plain text 
						d.write("<strong> " + upperCase + lowerCase + "</strong>");
					}
				}
				
				// Otherwise, this segment contains 1 or more periods... 
				else
				{
					// Split this segment into further segments 
					// Use the period "." as a delimiter 
					// Store the new segments into the newArray array 
					var newArray = linkArray[counter].split('.');
					
					// Is the new array segment longer than 1 character? 
					if(newArray[0].length > 1)
					{
						// Get the first character in the segment and make it uppercase 
						// Store that character in the upperCase variable 
						var upperCase = newArray[0].substr(0,1).toUpperCase();
						
						// Get the rest of the string after the first character 
						// Store that in the lowerCase variable 
						var lowerCase = newArray[0].substr(1, newArray[0].length);
						
						// Write the last segment of the breadcrumb to the web page 
						// Write it as plain text 
						d.write("<strong> " + upperCase + lowerCase + "</strong>");
					}
					
					// Otherwise, this segment is shorter than 1 character... 
					else
					{
						// Write the last segment of the breadcrumb to the web page 
						// Write it as plain text 
						d.write("<strong> " + newArray[0] + "</strong>");
					}
				}
			}
			
			// Or, do you want to use the title of the current web document?
			// **** CTG **** Added logic to allow custom PageName extracted from title.
			// ************* Expects titles to be set up as "<Site Name> - <Page Name>"

			else if(useDocumentTitle == "yes")
			{
				
				var pagename = d.title
				var dlast = pagename.lastIndexOf("-");
				
				if(dlast == -1)
				{
					d.write("<strong> " + pagename + "</strong>");
				}
				else
				{
					d.write("<strong> " + pagename.substr((dlast + 2),(pagename.length - dlast)) + "</strong>");
				}
			}
			
			// Or, do you want to use the name you have specified?
			else
			{
				d.write("<strong> " + LastLinkName + "</strong>");
			}
		}
		
		// Otherwise, this is not the last array segment... 
		else
		{
			// Is this segment free of periods (.)? 
			if(linkArray[counter].indexOf(".") == -1)
			{
				// Is this segment longer than 1 character? 
				if(linkArray[counter].length > 1)
				{
				
					// **** CTG ****
					// Swap Directory Names as needed for "plain English" names
					var newSegment = linkArray[counter];
					if(newSegment == "AboutUs")
					{
						newSegment = "About Us";
					}
				
					// **** CTG ****
					// Commented out uppercase & lowercase logic... handling with
					// actual directory names.
					
					// ************************************
					// Get the first character in the segment and make it uppercase 
					// Store that character in the upperCase variable 
					// var upperCase = linkArray[counter].substr(0,1).toUpperCase();
					
					// Get the rest of the string after the first character 
					// Store that in the lowerCase variable 
					// var lowerCase = linkArray[counter].substr(1, linkArray[counter].length);
					// ************************************
					
					// Is this the protocol or drive letter of the URL? 
					if(linkArray[counter] == "http:" || linkArray[counter] == "https:" || 
					linkArray[counter] == "C:")
					{
						// Do nothing 
					}
					
					// Otherwise, this segment is somewhere in the middle of the URL... 
					else
					{
						// Write this segment of the breadcrumb to the web page 
						// Write it as a link 
						// **** CTG **** Commented out next line and replaced with new one
						// d.write(" <a href='" + createLink() + "'>"+upperCase+lowerCase+"</a> >");
						d.write(" <a href='" + createLink() + "'>"+newSegment+"</a> >");
					}
				}
				
				// Otherwise, this segment is shorter than 1 character... 
				else
				{
					// Is this the company name portion of the URL? 
					if(linkArray[1])
					{
						// Write this segment of the breadcrumb to the web page 
						// Write it as a link 
						d.write(" <a href='" + createLink() + "'>"+FirstLinkName+"</a>&nbsp>");
					}
					
					// Otherwise, this is a segment somewhere in the middle of the URL... 
					else
					{
						// Write this segment of the breadcrumb to the web page 
						// Write it as a link 
						d.write(" <a href='" + createLink() + "'>"+linkArray[counter]+"</a> >");
					}
				}
			}
			
			// Otherwise, this segment contains 1 or more periods
			// and is, therefore, the first link in the chain 
			else
			{
				// Write this segment of the breadcrumb to the web page
				// Write it as a link
				d.write(" <a href='" + createLink() + "'>"+FirstLinkName+"</a> >");
			}
		}
	}
}