// Written by Harry Love
// Email: hlove@u.washington.edu
// Use at your own risk.
// Feel free to use, personalize, improve, and distribute.
// Last update: November 7, 2002

// Updated June 24, 2003
// William L. Graham, WL-Graham@csu.edu
// Added hooks for styles; simplified syntax
// Modification History
//
// 20070216 wlg - added fix for linkname= in cgi-bin remote references
// 20061007 wlg - added fix for admin/event nested subdirectories in cgi-bin
// 20060901 wlg - added bcc to span breadcrumb class on last entry
// 20060711 wlg - added "meetings" and other directories to <places> dictionary
// 20060710 wlg - readded "serials" to <places> dictionary; 
//                corrected use of "cgi-bin" as directory (creates undefined)
// 20040424 wlg - corrected use of page title when paging back in dropCrumb
//              - used document.URL rather than location.href
// 20040602 wlg - added use of title of default document when a directory 
//                rather than directory name, and corrected failure to
//                print the title of a document when the file is not the
//                default document name-stem, and is specified.

// 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">dropCrumbs('StyleValue');</script> */
//  StyleValue is null or is the value in a style= expression
// /* 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 ().
// The default separator is a "greater than(>)" symbol (>).
// var sep = "> "
var sep='<span style="color:#FEF0DB;font-weight:bold;">></span> '

// 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 = "Home Page";

// 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 = "yes";

// 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";

var replaceSpecialCharacters = 'no';

// END BASIC CUSTOMIZATION
var d=document;
var url = d.URL; // wlg from d.location.href
var endChar = url.substr(url.length-1);
var debugstory='';

var places = new Array();

function namedPlaces (printname,acronym) {
	this.printname=printname;
	this.directory=acronym;
}
places['ACCESS']='Access Services';
places['ACQUISITIONS']='Acquisitions';
places['ADMIN']='Administration';
places['ANALOG']='Web Server Log Analysis';
places['ARCHIVES']='University Archives';
places['ASRS']='ROVER, Automated Storage and Retrieval System';
places['ASSOCIATIONS']='Professional Associations';
places['BLOGS']='Web Logs';
places['BOOKS']='Books';
places['CALLNUMBERS']='Call Numbers';
places['CATALOGING']='Cataloging';
places['CATALOGS']='Catalogs';
places['CENTERS']='Library Centers';
places['CGI-BIN']='*';
places['COLLECTIONS']='Collection Development';
places['CIRCULATION']='Circulation';
places['CSUHISTORY']='CSU History';
places['DATABASES']='Databases';
places['DESKS']='Service Desks';
places['DIGITALLIBRARIES']='Digital Collections';
places['DIRECTORY']='Directory';
places['EBOOKS']='Electronic Books';
places['ERC']='Education Resource Center';
places['ERESOURCES']='Electronic Resources';
places['EVENT']='Event';
places['FAQS']='Frequently Asked Questions';
places['FINDING']='Finding Aids';
places['FRIENDS']='Friends of the Library';
places['GRANTS']='Grants';
places['GUIDES']='Guides';
places['HELPCENTER']='Help';
places['INSTRUCTION']='Information Literacy';
places['ILL']='Interlibrary Loan';
places['INTERNET']='Internet Finding Aids';
places['JOURNALS']='Journals';
places["LISAS"]='Administrative Services';
places['LISTS']='Technical Services';
places['LISPS']='Public Services';
places['LISASC']='Archives and Records, Special Collections';
places['LISMS']='Media Services';
places['LISTLR']='LIS Technology and Learning Resources';
places['MYLIBRARY']='My Library';
places['TLR']='Technology and Learning Resources';
places['HELP']='Help';
places['HOWTO']='Help and How-Tos';
places['ILPP']='Information Law and Public Policy';
places['MAPS']='Maps and Locations';
places['MEDIA']='Media';
places['MEDIAPRODUCTIONS']='Media Production';
places['MEETINGS']='Meetings';
places['MULTMEDIASERVICES']='Multimedia Services';
places['MUSIC']='Music and Performing Arts';
places['NEWS']='New Library News';
places['ORGANIZATION']='Organization';
places['PCS']='Personal Computers and Workstations';
places['POLICIES']='Policies';
places['POSITIONS']='Positions and Jobs';
places['PRESENTATIONS']='Presentations';
places['PURPOSE']='Purpose';
places['RECORDSMANAGEMENT']='Records Management';
places['RIS']='Reference and Instruction Services';
places['REPORTING']='Reports';
places['RESERVES']='Reserves';
places['ROOMS']='Rooms';
places['SCHEDULES'] ='Schedules';
places['SCHEDULING'] ='Event Scheduling';
places['SERIALS']='Serials';
places['SPECIALCOLLECTIONS']='Special Collections';
places['SPECIALSERVICES']='Special Services';
places['SUBJECTS']='Subjects';
places['SYSTEMS']='Systems';
places['TEMPLATES']='Templates';
places['TEXTHELP']='Help Using';
places['TLR']='Technology and Learning Resources';
places['TUTORIALS']='Tutorials';
places['USE']='Using the Site';
places['USING']='Using';
places['VISION']='Vision';
places['VIDEOS']='Videos';
places['XGUIDES']='Discovery Guides';
places['WEB']='Digitization and Web Services';
places['WEBSITES']='Web Sites';

url=url.replace("//","/");

// This next set of "if" clauses removes the cgi-bin++ portion of the URL for
// pages that include the cgi-bin directory.

// NB: These are nested in the order they occur in the URL

if(url.search("cgi-bin") != -1)
{
url=url.substring(0,url.search("cgi-bin")); // remove the cgi-bin data
}

if(url.search("linkname=") != -1)
{
	var badReg2 = /\/hsl\/.+url=/i;
	url=url.replace(badReg2,"");
}

if(url.search("&url=") != -1)
{
	var badReg2 = /\/hsl\/.+url=/i;
	url=url.replace(badReg2,"");
}

// End cgi-bin removal clauses

var urlText = url;
	

var urlLinkArray=url.split('/');


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];
		}
		w('startName='+startName)
	}
}

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]);
	}
}


var urlTextArray=urlText.split('/');

var urlL = urlLinkArray.length;
var uppercaseText;
var lowercaseText;
var lcase;
var linkName=new Array();


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(urlTextArray[y].search(myRegExp)!=-1)
						{
							
							var lastLetter = regExpArray[z].substr(regExpArray[z].length-1,regExpArray.length).toUpperCase();
							
							var newText = charactersToReplace[x][1]+lastLetter;
							urlTextArray[y] = urlTextArray[y].replace(regExpArray[z],newText);
						}
					}
				}
			}
		}
	}

	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";}

var start=urlLinkArray[0]+"//";

if(startPoint<1){startPoint=1;}

for(y=1;y<=startPoint;y++)
{
	start=start+urlLinkArray[y]+"/";
}

/* global utilities */

function w (theMsg) {document.writeln (theMsg);}

function mapPlace (dir) {

	value=places[dir];
	return value;

}

function bcc (stmt) {
	value = '<span class="breadcrumb">'+stmt+'</span>';
	return value;
}

function dropCrumbs (anchorStyleStatement) {

	w('<div id="BreadCrumb">');
	
	function wa (msg) {
		w('<a style="' + anchorStyleStatement + '" ' + msg);
	}

	// Write the first crumb.  Use startName for the text.
	
	wa('class="breadcrumb" href="http://library.csu.edu/">'+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...
				if(x==urlL-2&&linkName[urlL-1]=="")
				{				
					// If we are using the directory name
					// for the last segment...
					if(endPoint=="directory")
					{
						// This is the last segment.
						// Write it to the page, then exit.
						w(sep + bcc(linkName[x]));
						break;
					}
					
					else
					{
						// This is the last segment.
						// Write the title to the page, then exit.

						if(lcase!="yes")
						{		
							w(sep + bcc(d.title));
							break;
						}
						else
						{
							var ttl = d.title.toLowerCase();		
							w(sep+bcc(ttl));
							break;
						}						
					}
				}
				
				else
				{
					start=start+urlLinkArray[x]+"/";
					w(sep);
					linkLabel= mapPlace(linkName[x]);
				
					wa(' class="breadcrumb" href="'+start+'">'+linkLabel+'</a>');
				}
			}
			
			else
			{
				start=start+urlLinkArray[x];
				if(endChar=="/")
				{		
					w(sep+bcc(linkName[x]));
				}
				
				else
				{		
					w(sep + bcc(d.title));
				}
			}
		}
	}

		d.writeln('</div>');
}


