






// Set up variables
var breadcrumb = new Array();	// Array of all nodes in the site path
var crumbLevel = 0;				// Index for breadcrumb array
var inBreadcrumb;				// Boolean to keep track of whether an object is in the breadcrumb
var levelTrigger = 1;
var color = 0; // variable for count, which correlates with the stylesheet's list link colors
var coloractive = 0; // variable for marking the active color which is in the breadcrumbs AKA the user's path

// Prepare the 2nd level UL
var secondLevelUl = document.createElement('ul');
secondLevelUl.className = "subnav";

// Prepare the 3rd level UL
var thirdLevelUl = document.createElement('ul');

// This function creates asset Node objects
function Node(id, name, url, level, children, parent) {
	this.id = id;
	this.name = name;
	this.url = url;
	this.level = level;
	this.children = children;
	this.parent = parent;
}

// This function creates breadcrumb objects
function breadcrumbNode(id, parent) {
	this.id = id;
	this.parent = parent;
}

function buildSiteMapNav(ulId) {
	for(j=0; j < nodeList.length; j++) {		// Loop through all the nodes in the array
		var li = document.createElement('li');
		var a = document.createElement('a');
	
		// If the node is in the breadcrumb, set flag
		for(c=0, inBreadcrumb = 0; c < crumbLevel; c++) { 
			if ((nodeList[j].id == breadcrumb[c].id)) 
				inBreadcrumb = 1;
			}
			
		// If the node is in the breadcrumb, and is a level 1 link
		if (nodeList[j].level == 1 && inBreadcrumb == 1) {
			color++; // we hit a new category so increase the color count
			coloractive = color;
			
			// If the level is still triggered to level 2, but node is back to level one... append all the previous work that's been done
			if (levelTrigger == 2) {
				heldLi.appendChild(secondLevelUl);
				heldLi.className = "opensub color" + color;
				document.getElementById(ulId).appendChild(heldLi);
				levelTrigger--;
			}
			// Else just append the held li
			else if (heldLi) {
				document.getElementById(ulId).appendChild(heldLi);
			}
			
			a.href = nodeList[j].url;
			a.innerHTML = nodeList[j].name;
			a.className = "subnav_active";
			li.className = "opensub color" + color;
			li.appendChild(a);
			
			var heldLi = li;
		}
		// If the node is in the breadcrumb, and is a level 2 link, display it as bold and apply a style
		else if (nodeList[j].level == 2 && inBreadcrumb == 1) {
			
			// If second group has never been triggered, start the ul
			if (levelTrigger == 1) {
				levelTrigger++;
			}
			
			// If the level is still triggered to level 3, but node is back to level one... append all the previous work that's been done
			if (levelTrigger == 3) {
				secondHeldLi.appendChild(thirdLevelUl);
				secondLevelUl.appendChild(secondHeldLi);
				levelTrigger--;
			}
			// If the level is still triggered to level 2, but node is back to level one... append all the previous work that's been done
			else if (secondHeldLi) {
				secondLevelUl.appendChild(secondHeldLi);
			}
			
			a.href = nodeList[j].url;
			a.innerHTML = nodeList[j].name;
			a.className = "subnav_active";
			li.className =  "color" + color;
			li.appendChild(a);
			
			var secondHeldLi = li;
		}
		// If the node is in the breadcrumb, and is a level 3 link, display it as bold and apply a style
		else if (nodeList[j].level == 3 && inBreadcrumb == 1) {
			
			// If third group has never been triggered, start the ul
			if (levelTrigger == 2) {
				levelTrigger++;
			}
			
			a.href = nodeList[j].url;
			a.innerHTML = nodeList[j].name;
			a.className = "subnav_active";
			li.className =  "color" + color;
			li.appendChild(a);
			thirdLevelUl.appendChild(li);
		}
		
		else {
			// Display the node only if it shares a common parent with a node from the breadcrumb
			for(s=0; s < crumbLevel; s++) {
				// Grabs level 1 links that are not in the breakcrumbs AKA user's path
				if (nodeList[j].level == 1 && breadcrumb[s].parent == nodeList[j].parent) {
					color++; // we hit a new category so increase the color count
					
					// If the level is still triggered to level 2, but node is back to level one... append all the previous work that's been done
					if (levelTrigger == 2) {
						secondLevelUl.appendChild(secondHeldLi);
						heldLi.appendChild(secondLevelUl);
						heldLi.className = "opensub color" + (color - 1);
						document.getElementById(ulId).appendChild(heldLi);
						levelTrigger--;
					}
					// Else just append the held li
					else if (heldLi) {
						document.getElementById(ulId).appendChild(heldLi);
					}
					
					a.href = nodeList[j].url;
					a.innerHTML = nodeList[j].name;
					li.className =  "color" + color;
					li.appendChild(a);
					
					var heldLi = li;
				}
				// Grab level 2 links that are not in the breakcrumbs AKA user's path
				else if (nodeList[j].level == 2 && breadcrumb[s].parent == nodeList[j].parent) {				
					// If second group has never been triggered, start the ul
					if (levelTrigger == 1) {
						levelTrigger++;
					}
					
					// If the level is still triggered to level 3, but node is back to level one... append all the previous work that's been done
					if (levelTrigger == 3) {
						secondHeldLi.appendChild(thirdLevelUl);
						secondLevelUl.appendChild(secondHeldLi);
						levelTrigger--;
					}
					// If the level is still triggered to level 2, but node is back to level one... append all the previous work that's been done
					else if (secondHeldLi) {
						secondLevelUl.appendChild(secondHeldLi);
					}
					
					a.href = nodeList[j].url;
					a.innerHTML = nodeList[j].name;
					li.className =  "color" + color;
					li.appendChild(a);
					
					var secondHeldLi = li;
				}
				// Grab level 3 links that are not in the breakcrumbs AKA user's path
				else if (nodeList[j].level == 3 && breadcrumb[s].parent == nodeList[j].parent) {
					
					// If third group has never been triggered, start the ul
					if (levelTrigger == 2) {
						levelTrigger++;
					}
					
					a.href = nodeList[j].url;
					a.innerHTML = nodeList[j].name;
					li.className =  "color" + color;
					li.appendChild(a);
					thirdLevelUl.appendChild(li);
				}
			}
		}
	}
	
	// We're all done, finish up the last list items if they exist.
	if (levelTrigger == 3) {
		secondHeldLi.appendChild(thirdLevelUl);
		levelTrigger--;
	}
	if (levelTrigger == 2) {
		secondLevelUl.appendChild(secondHeldLi);
		heldLi.appendChild(secondLevelUl)
		levelTrigger--;
	}
	if (levelTrigger == 1) {
		document.getElementById(ulId).appendChild(heldLi);
	}
}

function determineSiteMapParents(array) {
	for(var c = 0; c < array.length; c++){
		if (array[c].level != 1) {					// If this node is not the top level, look for it's parent.
			for(p = c - 1; p > 0; p--) {			// Search up the site structure node list.
				if (array[p].children > 0) {		// If the node has children, you have found the parent
					array[p].children--;			// Decrease its number of children
					break;							// Stop looking for the parent
				}
			}
			array[c].parent = array[p].id;	// Set the parent asset id
		}
	}
}