/*
 * breadcrumb.js
 * 
 * njohnson@echoalley.com
 * adapted from source on WWW
 *
 * This file builds a breadcrumb trail based on the URL of
 * a page. It parses the string and takes everything between
 * the slashes (/) and assumes that the directory structure
 * mirrors the content structure. For instance,
 * 
 * http://www.echoalley.com/about_us/employee_profiles.php
 * 
 * becomes
 *
 * Home > About Us > Employee Profiles
 *
 * The last member of the breadcrumb trail is never a link because
 * it indicates the current page.
 *
 *
 *
*/

/*
 * Variables
 *
 * These control display of the breadcrumbs
 *
*/

var linkClass = ""; //style of the links
var textClass = "ui-crumb"; //style of plain text
var isGGJ = false;

// sld: extract query string (if there is one)
myURL = window.location.href;
queryString = myURL.substring(myURL.lastIndexOf("?") + 0);

//what is the file extension for the files on this site?
// comment all that are NOT used and add any new ones
var fileTypesAry = new Array (
	'.php',
	'.html',
	'.shtml',
	'.pl',
	'.cgi'
)

//do you want to display the filename on the page you are on?
// 1=yes, 0= no
var showFileName = 1;


/*
 * start the work
 *
*/

document.writeln('<!-- breadcrumb navigation -->');

//this is the top level page - the beginning of the breadcrumbs

document.writeln('<a href="/">Home</a> &gt;');

	var href = window.location.href;
	var pathName = window.location.pathname;
	var host = window.location.host;
	var c = 0;
	var u = 0;
	var ac = new Array();
	var acurl = new Array();
	var count = 0;
	var crumbs = Array();
	
/*
 * Here we build two arrays from the URL. ac is the 'crumb,' acurl is the
 * link associated with it.
 * 
*/
	for(;;)
	{
		URL = unescape(href.substring(0,(href.lastIndexOf("/")) + 1))
		var addcr = href.replace(URL,"")
		if(addcr == host) break;
		URL = URL.slice(0,-1)
		var addurl = href;
		href = URL;
		
		//hide filename if required
		if (showFileName==0)
		{
			var x = fileTypesAry.length;
			for (y=0;y<x;y++)
			{
				fileExt = fileTypesAry[y];
				if(addcr == fileExt) continue;
			}
		}
		// Skip '/' if present in the last charecter in the URL
		if(addcr == "") continue;

		// sld: Skip query string if present at end of URL
		if(addcr == queryString) continue;		
		
		if(addcr == "index.shtml") continue;

		if(addcr == "index.php") continue;
		
		//if you would like to replace certain items in the URL with
		//other content, do it like this:
	    //if(addcr == "XXXzzz") addcr = addcr.replace("XXX","NNn");
		// addcr now is 'NNnzzz'
		
		count++;
		ac[c++] = addcr;
		acurl[u++] =addurl;
		
		myVar = addcr;
	}

	i=0;
	crumbs[i++] = "Home";
	var msg;

	for(itemCount = ac.length-1; itemCount >=0; itemCount--)
	{

		// limit number of crumbs here
		if (i == 8) break;
		
		crumbs[i++] = ac[itemCount];
		setCrumbName();
		//display filename without link

		lnk = acurl[itemCount];
		ggjlnk = acurl[itemCount] + '/' + queryString;

		// See if the next link is GGJ related... if so great!
		nextLink = false;
		if( (ac[itemCount-1] == 'itinerary-and-map') || (ac[itemCount-1] == 'lodging') || (ac[itemCount-1] == 'journey-guides') || (ac[itemCount-1] == 'pricing-and-terms') ) {
			nextLink = true;
		}

		if(itemCount==0)
		{
			msg = ' ' + ac[itemCount];
		}
		//else display link
		else 
		{	
			// if this is a GGJ trip only add ggjid queryString when needed
			if(nextLink) {
				msg = '<a href="' + ggjlnk +  '" class="'+linkClass+'">' + ac[itemCount] + '</a> &gt;'; 			
			} else {
				msg = '<a href="' + lnk +  '" class="'+linkClass+'">' + ac[itemCount] + '</a> &gt;'; 
			}
		}
		document.writeln(msg);		
	}


	setCrumbName();

/*
 * function parseQueryString()
 */
	function parseQueryString() {
		var qs=location.search.substring(1);
		var s1=qs.split("&");
		for(var i=0;i<s1.length;i++){
			var s2=s1[i].split("=");
			window[s2[0]]=unescape(s2[1]);
		}
	}


/*
 * function setCrumbName()
 *
 * this function takes the crumb and removes '_', '-', and file extensions
 * as well as capitalizing the first letter of the words.
*/
	function setCrumbName()
	{
		//ac[itemCount] = str.replace("and","&");
		//ac[itemCount] = ac[itemCount].replace("and","&");
		//ac[itemCount] = ac[itemCount].replace("obp","OBP");

		//Put function here to modify directory name values based on
		//hardcoded values.
		if (ac[itemCount] == "offthebeatenpath-news") {
			ac[itemCount] = "In The News";
			return;
		}
		if (ac[itemCount] == "request-a-catalog") {
			ac[itemCount] = "Request a Catalog";
			return;
		}
		if (ac[itemCount] == "jobs-at-obp") {
			ac[itemCount] = "Jobs at OBP";
			return;
		}		
		if (ac[itemCount] == "journeys-by-date") {
			ac[itemCount] = "Journeys by Date";
			return;
		}
                if (ac[itemCount] == "new-zealand") {
                        ac[itemCount] = "New Zealand";
                        return;
                }
		if (ac[itemCount] == "faqs") {
			ac[itemCount] = "Frequently Asked Questions";
			return;
		}		
                if (ac[itemCount] == "utah-canyon-lands") {
                        ac[itemCount] = "Utah Canyon Lands";
                        return;
                }


		// Motifs messed up urls
		if (ac[itemCount] == "wildlife-natural-history") {
			ac[itemCount] = "Wildlife & Nature";
			return;
		}		
		if (ac[itemCount] == "hiking-walking") {
			ac[itemCount] = "Hiking & Walking";
			return;
		}		
		if (ac[itemCount] == "culture-history") {
			ac[itemCount] = "Culture & History";
			return;
		}		
		if (ac[itemCount] == "deluxe-camping-rafting") {
			ac[itemCount] = "Deluxe Camping & Rafting";
			return;
		}		
		
		if (ac[itemCount] == "year-at-a-glance") {
			parseQueryString();
			ac[itemCount] = year + " Dates";
			return;
		}
		if (ac[itemCount] == "month-at-a-glance") {
			var monthTxt;
			parseQueryString();
			if(month == '01') { monthTxt = 'January'; }
			if(month == '02') { monthTxt = 'February'; }
			if(month == '03') { monthTxt = 'March'; }
			if(month == '04') { monthTxt = 'April'; }
			if(month == '05') { monthTxt = 'May'; }
			if(month == '06') { monthTxt = 'June'; }
			if(month == '07') { monthTxt = 'July'; }
			if(month == '08') { monthTxt = 'August'; }
			if(month == '09') { monthTxt = 'September'; }
			if(month == '10') { monthTxt = 'October'; }
			if(month == '11') { monthTxt = 'November'; }
			if(month == '12') { monthTxt = 'December'; }
			ac[itemCount] = year + " Journey Dates in " + monthTxt;
			return;
		}
		if (ac[itemCount] == "search") {
			ac[itemCount] = "Search Results";
			return;
		}

		if (itemCount!=-1)
		{
        	        // replace "obp" with "OBP"
	                regexp = eval("/" + "obp" + "/g")
                	ac[itemCount] = ac[itemCount].replace(regexp,"OBP");
        	        regexp = eval("/" + "and" + "/g")
	                ac[itemCount] = ac[itemCount].replace(regexp,"&");

			// replace "/" with the "-"
			regexp = eval("/" + "-" + "/g")
			ac[itemCount] = ac[itemCount].replace(regexp,"_")

			var strList = ac[itemCount].split("_");
			for(k=0;k<strList.length;k++)
			{
				var str = strList[k];
				var substr = (str.substring(0,1)).toUpperCase(); // Retrieves the first letter from the string list
				regexp = eval("/" + str.substring(0,1) + "/")
				str = str.replace(regexp,substr)
				regexp = eval("/" + strList[k] + "/")
				ac[itemCount] = ac[itemCount].replace(regexp,str)
		
			}
			
		// replace "_" with the blank space
		regexp = eval("/" + "_" + "/g")
		ac[itemCount] = ac[itemCount].replace(regexp," ");

		//take out filename extensions
		if (itemCount==0)
		{
			var x = fileTypesAry.length;
			for (y=0;y<x;y++)
			{
				fileExt = fileTypesAry[y];
				string = ac[itemCount];
				//alert(string.substring(5, string.length));
				var excerpt = (string.substring(string.indexOf(fileExt), string.length));
				//alert('is '+excerpt+' equal to '+fileExt+'?');
				if(excerpt==fileExt) break
			}
		regexp = eval("/" + fileExt + "/g");
		ac[itemCount] = ac[itemCount].replace(regexp,"");
		}
	}
}
