// settings
var titlePrefix = "Compliance Solutions | ";
var urlAddress = "http://www.cslnz.co.nz";
var urlName = "cslnz.co.nz";

// main
var prependCrumbs = new Array();

if (!(urlAddress == "")) {
     	prependCrumbs.push(new Array(urlName, urlAddress));
}

// true or false (1 or 0) if local site
var localSite = "0";
// true or false (1 or 0) to capitalize first letter of each crumb
var capitalizeCrumb = "0";	
// true or false (1 or 0) to capitalize all crumbs
var capitalizeAllCrumb = "0";	
// true or false (1 or 0) to display active page title
var displayPageTitle = "0";	
// text to include at the beginning of the trail
var displayPrepend = "";
// enter character to display as separator between crumbs or leave empty to use image separator
var separatorChar = "";	
// true or False (1 or 0) to include separator at the end of the trail
var separatorEnd = "1";
// full path of image separator file
var separatorFile = "/images/arrow_bread.gif";	
// array of strings containing common file extensions, to determine what part of the url to ignore
var fileExtensions = new Array(".html", ".shtml", ".htm", ".jsp", ".php", ".php4", ".php5", ".asp", ".aspx");
// string that separates parts of the breadcrumb path from each other
var pathSeparator = "/";
/////////////////////////////////////////////////////////////////////////////////////////////

// check what to include between crumbs
if (separatorChar == "") {
	var separatorStr = ' <img src="' + separatorFile + '" alt=""> ';
}
else {
	var separatorStr = " " + separatorChar + " ";
}
var displaySeparator = separatorStr;

// check what to include at the end of the trail path
if (displayPageTitle == "1") {
	var displayPostpend = separatorStr;
}
else if (separatorEnd == "1") {
	var displayPostpend = separatorStr;
}
else {
	var displayPostpend = "";
}

// check if to capitalize first letter of each crumb
function sentenceCase(string) {   
	string = string.toLowerCase();
	if (capitalizeCrumb == "1") {
		string = string.substr(0,1).toUpperCase() + string.substr(1);
	}
	if (capitalizeAllCrumb == "1") {
		string = string.toUpperCase();
	}
	return string;
}

// return an array containing the names of all directories in URL
function getDirectoriesInURL() {
	if (localSite == "1") {
		var trail = document.location.href.split(pathSeparator);
	}
	else {
		var trail = document.location.pathname.split(pathSeparator);
	}    	
	
	// check whether the last section is a file or a directory
	var lastcrumb = trail[trail.length-1];
	for (var i = 0; i < fileExtensions.length; i++) {
		if (lastcrumb.indexOf(fileExtensions[i])) {
			// if yes remove it and send trail
			return trail.slice(1, trail.length-1);
		}
	}
	// if not send trail unmodified
	return trail.slice(1, trail.length);
}

// return an array describing the breadcrumbs based on directories
function getBreadcrumbs(dirs) {
	var prefix = "/";
	var postfix = "/";

	// the array we will return
	var crumbs = new Array();

	if (dirs != null) {
		for (var i = 0; i < dirs.length; i++) {
			prefix += dirs[i] + postfix;
			crumbs.push(new Array(dirs[i], prefix));
		}
	}

	// preprend the prependCrumbs
	if(prependCrumbs.length > 0) {
		return prependCrumbs.concat(crumbs);
	}
	return crumbs;
}

// return a string containing a simple breadcrumb trail based on the array passed in
function getCrumbTrail(crumbs) {
	var xhtml = displayPrepend;

	for (var i = 0; i < crumbs.length; i++ ) {
		xhtml += '<a href="' + crumbs[i][1] + '" >' + sentenceCase(unescape(crumbs[i][0])) + '</a>';
		if ( i != (crumbs.length-1)) {
			xhtml += displaySeparator;
		}
	}
	xhtml += displayPostpend;
	return xhtml;
}

// write breadcrumbs / not used
function printBreadcrumbs() {
	// check if local; if yes only print the prependCrumbs
	if (document.location.href.toLowerCase().indexOf( "http://") == -1) {
		document.write(getCrumbTrail(getBreadcrumbs()));
		//document.write(getCrumbTrail(getBreadcrumbs(getDirectoriesInURL())));
	}
	else {
		document.write(getCrumbTrail(getBreadcrumbs(getDirectoriesInURL())));
	}
}

// write breadcrumbs
function breadcrumbs() {
	var crumbsPath = getCrumbTrail(getBreadcrumbs(getDirectoriesInURL()));
	
	document.write('You are here:&nbsp;');	
	
	document.write(crumbsPath);

	if (activeCrumb) {				
		activeCrumb = activeCrumb.toLowerCase();			
	}
	else {				
		var activeCrumb = document.title.toLowerCase();			
	}			
	document.write(activeCrumb);
	document.title = titlePrefix + document.title
}
breadcrumbs();

