/*
Based on MPBackLinks.js by: Nate Baldwin :: http://www.mindpalette.com
Ref.: http://javascript.internet.com/navigation/breadcrumb-links.html

Note: You can customize the styling of the breadcrumbs using CSS.
The breadcrumbs are created inside a <div> with a class assigned
named "breadcrumbs". You can create a new CSS class style
named "breadcrumbs" and assign whatever style settings you want.
*/

// ============================================================

/* homePage determines what the home page, or the root directory,
of your site is named. The default is "Home". To use the address of your
site (www.yoursite.com), leave the value blank.
*/
var homePage = "Jetex.org Homepage";

/* sepChars controls the character(s) that the script places between
the link levels. The default is set to be the > (greater than) HTML entity
with a space on either side.
*/
var sepChars = " > ";

/* linkHome tells the script what all the link addresses will start with.
'/' can be replaced with the fully qualified http://www.yoursite.com address.
*/
var linkHome = "/";

/* hideIndex: true if you want the script to not list your
index page name (index.html) at the end of the links path.
*/
var hideIndex = true;

/* UToSpace:true if you want the script to convert any
underscores in directory or names to spaces.
*/
var UToSpace = true;

/* DToSpace: true if you want the script to convert any dashes
in directory or page names to spaces.
*/
var DToSpace = true;

/* changeCaps controls the capitalization of the directory and
pages names in your menu. Options are:
0 = no change; 1 = initial caps; 2 = all upper case; 3 = all lower case
*/
var changeCaps = 1;

/* hideExt: true if display of the file's extension is not wanted for
any file names displayed in the menu.
*/
var hideExt = true;

// buildBreadcrumbs()
// Construct breadcrumb links string
//
function buildBreadcrumbs() {
	var linkHTML = '';
	var thisURL = window.location + '';
	var urlPair = thisURL.split('//');
	
	if (urlPair.length > 1)
		thisURL = urlPair[1];
		
	var dirArray = thisURL.split('/');
	var linkArray = dirArray.slice(1);
	var linkDir = '/';
	var currentPage = '';
	
	if (linkHome != '' && linkHome != '/') {
		var thisTest = linkHome.split('//');
		
		if (thisTest.length > 1) linkHome = thisTest[1];
		startArray = linkHome.split('/');
		var backCount = 0;
		
		for (var n=0; n<startArray.length; n++) {
			if (startArray[n] == '..') backCount++;
				else break;
			}
		if (backCount > 0) {
			var part1 = dirArray.slice(0, (dirArray.length - backCount - 1));
			var part2 = startArray.slice(backCount);
			startArray = part1.concat(part2);
			}
			else {
			var newStart = new Array(dirArray[0]);
			
			for (var n=1; n<startArray.length; n++) {
				var thisTest = (typeof dirArray[n] != "undefined") ? dirArray[n] : false;
				if (thisTest && thisTest == startArray[n]) newStart[n] = startArray[n];
					else break;
				}
			startArray = newStart;
			}
		if (startArray.length > 1) {
			var lastOne = startArray[startArray.length - 1];
			if (lastOne != '') {
				var thisTest = lastOne.split('.');
				if (thisTest.length > 1) startArray[startArray.length - 1] = '';
					else startArray[startArray.length] = '';
				}
			if (homePage == '') homePage = startArray[startArray.length-2];
			linkArray = dirArray.slice(startArray.length - 1);
			if (startArray[0] != '') startArray[0] = "http://"+startArray[0];
			linkDir = startArray.join('/');
			} else linkArray = dirArray.slice(1);
		} else {
		linkArray = dirArray.slice(1);
		if (homePage == '') homePage = dirArray[0];
		}
		
	var backTrack = 1;
	
	if (linkArray[linkArray.length - 1] != '') {
		var lastOne = linkArray[linkArray.length - 1];
		var testName = lastOne.split('.');
		
		if (testName[0] == 'index' || testName[0] == 'default') {
			backTrack = 2;
			currentPage = linkArray[linkArray.length - 2];
			}
			else if (hideExt)
				currentPage = testName[0]
			else
				currentPage = lastOne;
		}
		else {
			backTrack = 2;
			currentPage = linkArray[linkArray.length - 2];
	}

	var html = '';

	if (linkArray.length >= backTrack) {
		linkArray = linkArray.slice(0, linkArray.length - backTrack);

		var links = new Array();

		if (homePage != '') {
			homePage = MPBCParseText(homePage, UToSpace, DToSpace, changeCaps);
			links[links.length] = '<a href="' + linkDir + '">'+homePage+'</a>';
		}

		var baseDir		= linkDir;
		var indexPage = ''; //#[jmc] added to provide link to index page within dir (assumed to be same as dir name + '.html')

		for (var n = 0; n < linkArray.length; n++) {
			baseDir += linkArray[n] + '/';
			indexPage = linkArray[n] + '.html';

			var thisText = MPBCParseText(linkArray[n], UToSpace, DToSpace, changeCaps);

			links[links.length] = '<a href="' + baseDir + indexPage + '">' + thisText + '</a>';
		}


		//#[jmc] added getElementById('Subject') to give better current page descriptor
		//
		if (typeof document.getElementById != 'undefined')

			if (document.getElementById('Subject'))
				links[links.length] = '<font color="#000066">' + document.getElementById('Subject').content + '</font>';
		else if (currentPage != '')
			links[links.length] = MPBCParseText(currentPage, UToSpace, DToSpace, changeCaps);

		html = '<div class="breadcrumbs">' + links.join(sepChars) + '<\/div>';
	}
	return html;
}

// Parse displayed string through text filters
//
function MPBCParseText(thisText, UToSpace, DToSpace, changeCaps) {
	if (typeof thisText != "undefined" && thisText) {
		if (DToSpace)
			thisText = MPBCReplaceChar('-', ' ', thisText);
		if (UToSpace)
			thisText = MPBCReplaceChar('_', ' ', thisText);
		if (changeCaps)
			thisText = MPBCFixCaps(thisText, changeCaps);
	}
	else
		thisText = '';
	
	return thisText;
}

// Find and replace single character in string
//
function MPBCReplaceChar(oldChar, newChar, thisString) {
	var newString = '';
	
	for (var n=0; n<thisString.length; n++) {
		newString += (thisString.charAt(n) == oldChar) ? newChar : thisString.charAt(n);
	}
	return newString;
}
// determine changes in capitalization...
function MPBCFixCaps(thisString, changeCaps) {
	if (changeCaps == 1)
		thisString = MPBCUCWords(thisString);
		else
			if (changeCaps == 2)
				thisString = thisString.toUpperCase();
		else
			if (changeCaps == 3)
				thisString = thisString.toLowerCase();
	return thisString;
}

// capitalize the first letter of every word...
function MPBCUCWords(thisString) {
	var thisArray = thisString.split(' ');
	var newString = '';
	for (var n=0; n<thisArray.length; n++) {
		var firstChar = thisArray[n].charAt(0).toUpperCase();
		var theRest = thisArray[n].substring(1, thisArray[n].length);
		newString += firstChar+theRest+' ';
		}
	return newString.substring(0, newString.length - 1);
}

document.write(buildBreadcrumbs());

