// <![CDATA[
var xhr = false;
var crumbItems = [];
var whereIAm;
var theFinalNode;
var x;

/************************
parseLocation FUNCTION:
called from the readyToGo function and returns to the whereIAm varible;
parses through the URL of the page we're on and strips off 
the "base URL" defined in setupBreadcrumbs.js
************************/

function parseLocation() {
	var numberOfTotalSlashes = numberOfSubdirectories + 1;
	var thisURL = window.location + '';
	var whereDoubleSlashIs = thisURL.indexOf("//", 0) + 2;
	noHttp = thisURL.substring(whereDoubleSlashIs, thisURL.length);
	var slashLocation;
	for (var k = 0; k < numberOfTotalSlashes; k++) {
		slashLocation = noHttp.indexOf("/", 0) + 1;
		noHttp = noHttp.substring(slashLocation, noHttp.length);
	}
	return noHttp;
}

/************************
crawlUpDOM FUNCTION:
iterative function that crawls up the DOM tree to find the next node up and adds
it to the crumbItems array
************************/

function crawlUpDOM(passedInNode) {
	var addDivider = " " + divider + " ";
	theParentNode = passedInNode.parentNode;
	if (theParentNode.nodeType == 1) {
		if (theParentNode.nodeName == "sublevel" || theParentNode.nodeName == "level" ) {
			if (theParentNode.getAttribute("url") !== null) {
				crumbItems.push(addDivider + "<a href='" + linkHome + theParentNode.getAttribute("url") + "'>" + theParentNode.getAttribute("title") + "</a>");
			}	
			else {
				crumbItems.push(addDivider + theParentNode.getAttribute("title"));
			}
			
		}
	}
	if (theParentNode.parentNode.nodeType == 1) {
		crawlUpDOM(theParentNode);
	}
}

/************************
getFinalNode FUNCTION:
crawls down through the DOM tree and matches the page we're on to a node's
url attribute in the XML document; returns that node in the XML document
************************/

function getFinalNode(passedInNode) {
	if (passedInNode == null) {
		return;
	}
//var subLevelChildren = atSublevel.getElementsByTagName('*');
	if (passedInNode.getAttribute("url") == whereIAm) {
		theFinalNode = passedInNode;
		return;
	}
	if (passedInNode.nodeName == "sublevel") {
		try {
			nextNode = getChildNodesElementNode(passedInNode);
		}
		catch(err) {
			return;
		}
		getFinalNode(nextNode);
	}
	if (passedInNode.nodeName == "page") {
		//alert("at page " + passedInNode.getAttribute("url"));
		nextNode = getNextSiblingElementNode(passedInNode);
		if (nextNode == null) {
			//try to climb up to the next parent which will be a sublevel
			try {
				checkNode = getNextSiblingElementNode(passedInNode.parentNode);
			}
			catch(err) {
				return;
			}
			getFinalNode(checkNode);
			
		}
		else {
			getFinalNode(nextNode);
		}
	}
}

/************************
getChildNodesElementNode FUNCTION:
gets the next child node that is an element node
************************/

function getChildNodesElementNode(passedInNode) {
	if (passedInNode.childNodes[0].nodeType !== 1) {
		nodeToCheck = passedInNode.childNodes[0].nextSibling;
	}
	else {
		nodeToCheck = passedInNode.childNodes[0];
	}
	return nodeToCheck;
}

/************************
getNextSiblingElementNode FUNCTION:
gets the next sibling node that is an element node
************************/
	
function getNextSiblingElementNode(passedInNode) {
	if (passedInNode.nextSibling !== null) {
		if (passedInNode.nextSibling.nodeType !== 1) {
			nodeToCheck = passedInNode.nextSibling.nextSibling;
		}
		else {
			nodeToCheck = passedInNode.nextSibling;
		}
		return nodeToCheck;
	}
}
	
/************************
showBreadcrumbs FUNCTION:
called on page load; loads in the XML and then starts the process of
displaying the breadcrumbs by calling the readyToGo function
************************/

function showBreadcrumbs() {
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	}
	else {
	//only applies for IE 6
		if (window.ActiveXObject) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {}
		}
	}
	
	if (xhr) {
		xhr.onreadystatechange = setDataArray;
		xhr.open("GET", xmlDocToLoad, true);
		xhr.send(null);
	}
	else {
		alert("Sorry, but I couldn't create an XMLHttpRequest.");
	}
}

function setDataArray() {
	if (xhr.readyState == 4) {
		if (xhr.status == 200) {
			readyToGo();
		}
	}
}

/************************
readyToGo FUNCTION:
main function; displays the breadcrumbs
************************/

function readyToGo() {
	x = xhr.responseXML.getElementsByTagName("level");
	whereIAm = parseLocation();
	//loop through the level groupings
	outerLoop: 
	for (var n = 0; n < x.length; n++) {
	//loop through and see how many child nodes there are in each level grouping
		if (x[n].getAttribute("url") == whereIAm) {
			theFinalNode = x[n];
			break outerLoop;
		}
		for (var m = 0; m < x[n].childNodes.length; m++) {
			var child = x[n].childNodes[m];
			//fudge for Mozilla to only see element nodes
			if (child.nodeType == 1) {
				getFinalNode(child);
			}
			// if we've found the page we're on
			if (typeof(theFinalNode) !== "undefined") {
				break outerLoop;
			}
		}
	}
	var breadCrumb = "<a href='" + linkHome + "'>Home</a>";
	//if we matched a final node
	if (typeof(theFinalNode) !== "undefined") {
		crawlUpDOM(theFinalNode);
		crumbItems.reverse();
		for	(i=0; i < crumbItems.length; i++) { 
   			breadCrumb += crumbItems[i];
		}
	//finally add the node you found, title only
	breadCrumb +=  " " + divider + " " + theFinalNode.getAttribute("title"); 
	}
	//write out the breakdcrumbs to the page
	document.getElementById("breadcrumbs").innerHTML= breadCrumb;
}
// ]]>