// Set up variables
var inBreadcrumb;
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

// 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(divId){
	for(j=0; j < nodeList.length; j++) {		// Loop through all the nodes in the array
		var li = document.createElement('li');
		var liShadow = 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 2 link, display it as bold and apply a style
		if (nodeList[j].level == 2 && inBreadcrumb == 1) {
			a.href = nodeList[j].url;
			a.innerHTML = nodeList[j].name;
			a.className = "subactive";
			li.appendChild(a);
			li.className = "sub level-2 color" + color;
			document.getElementById(divId).appendChild(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 == 3 && inBreadcrumb == 1) {
			a.href = nodeList[j].url;
			a.innerHTML = nodeList[j].name;
			a.className = "subactive";
			li.appendChild(a);
			li.className = "sub level-3 color" + color;
			document.getElementById(divId).appendChild(li);
		}
		// If the node is in the breadcrumb, and is a level 1 link, display it as bold and apply a style
		else if (nodeList[j].level == 1 && inBreadcrumb == 1) {
			color++; // we hit a new category so increase the color count
			coloractive = color;
			
			a.href = nodeList[j].url;
			a.innerHTML = nodeList[j].name;
			li.appendChild(a);
			li.className = "level-1 color" + color + " coloractive" + coloractive;
			document.getElementById(divId).appendChild(li);
			
			liShadow.className = "shadow";
			liShadow.innerHTML = "&nbsp;";
			document.getElementById(divId).appendChild(liShadow);
		}
		
		else {
			// Display the node only if it shares a common parent with a node from the breadcrumb
			for(s=0; s < crumbLevel; s++) {
				// Grab sublevel links are one level deep 
				if (nodeList[j].level == 2 && breadcrumb[s].parent == nodeList[j].parent) {
					a.href = nodeList[j].url;
					a.innerHTML = nodeList[j].name;
					li.appendChild(a);
					li.className = "sub level-2 color" + color;
					document.getElementById(divId).appendChild(li);
				}
				// Grab sublevel links are two levels deep 
				else if (nodeList[j].level == 3 && breadcrumb[s].parent == nodeList[j].parent) {
					a.href = nodeList[j].url;
					a.innerHTML = nodeList[j].name;
					li.appendChild(a);
					li.className = "sub level-3 color" + color;
					document.getElementById(divId).appendChild(li);
				}
				// Grabs level 1 links that are not in the breakcrumbs AKA user's path
				else if (breadcrumb[s].parent == nodeList[j].parent) {
					color++; // we hit a new category so increase the color count
					a.href = nodeList[j].url;
					a.innerHTML = nodeList[j].name;
					li.appendChild(a);
					li.className = "level-1 color" + color;
					document.getElementById(divId).appendChild(li);
					break;
				}
			}
		
		}
	}
}

function determineSiteMapParents(array) {
	for(var i = 0; i < array.length; i++){
		if (array[i].level != 1) {					// If this node is not the top level, look for it's parent.
			for(p = i - 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[i].parent = array[p].id;	// Set the parent asset id
		}
	}
}