// Set up variables
var breadcrumb = new Array();	// Array of all nodes in the breadcrumb trail
var crumbLevel = 0;				// Index for breadcrumb array
var indent = 20;				// Indent value for display


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, description, url, level, children, parent) {
	this.id = id;
	this.name = name;
	this.description = description;
	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 ocbBuildGeneralNav(divId, currentAssetId){
	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 2 link, display it as bold and apply a style
		if ((nodeList[j].level == 2 && inBreadcrumb == 1) && (nodeList[j].id == currentAssetId)) {
			a.href = nodeList[j].url;
			a.innerHTML = nodeList[j].description;
			li.appendChild(a);
			li.style.marginLeft = "10px";
			li.className = "selected";
			document.getElementById(divId).appendChild(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) && (nodeList[j].id == currentAssetId)) {
			a.href = nodeList[j].url;
			a.innerHTML = nodeList[j].description;
			li.appendChild(a);
			li.style.marginLeft = "20px";
			li.className = "selected";
			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) && (nodeList[j].id == currentAssetId)) {			
			a.href = nodeList[j].url;
			a.innerHTML = nodeList[j].description;
			li.appendChild(a);
			li.className = "selected";
			document.getElementById(divId).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++) {
				// 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].description;
					li.appendChild(a);
					li.style.marginLeft = "10px";
					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].description;
					li.appendChild(a);
					li.style.marginLeft = "20px";
					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) {
					a.href = nodeList[j].url;
					a.innerHTML = nodeList[j].description;
					li.appendChild(a);
					document.getElementById(divId).appendChild(li);
					break;
				}
			}
		
		}
	}
}

function ocbBuildListNav(divId){
	var j;
	
	if (j == null) {
		b = 0;
	} else {
		b = j;
	}
	
	for (j = b; 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 2 link, indent and apply "selected" class
		if (nodeList[j].level == 2 && inBreadcrumb == 1) {
			a.href = nodeList[j].url;
			a.innerHTML = nodeList[j].description;
			li.appendChild(a);
			li.style.marginLeft = "10px";
			li.className = "selected";
			document.getElementById(divId).appendChild(li);
		}
		// If the node is in the breadcrumb, and is a level 1 link, apply "selected" class
		else if (nodeList[j].level == 1 && inBreadcrumb == 1) {
			a.href = nodeList[j].url;
			a.innerHTML = nodeList[j].description;
			li.appendChild(a);
			li.className = "selected";
			document.getElementById(divId).appendChild(li);
		}
		// Finally, if the node is not in the breadcrumb, display it without selected or indented styles
		else if (nodeList[j].level == 1 && inBreadcrumb == 0) {			
			a.href = nodeList[j].url;
			a.innerHTML = nodeList[j].description;
			li.appendChild(a);
			document.getElementById(divId).appendChild(li);
		}
		
		else {
			// Next, display the nodes that share a common parent
			for(s=0; s < crumbLevel; s++) {
				// If node is level 2, then apply indent
				if (((breadcrumb[s].parent == nodeList[j].parent + 1) && nodeList[j].level == 2) || ((breadcrumb[s].parent == nodeList[j].parent) && nodeList[j].level == 2)) {
					a.href = nodeList[j].url;
					a.innerHTML = nodeList[j].description;
					li.appendChild(a);
					li.style.marginLeft = "10px";
					document.getElementById(divId).appendChild(li);
				}
				// If node is level 1, don't apply indent
				else if ((breadcrumb[s].parent == nodeList[j].parent + 1) && nodeList[j].level == 1) {
					a.href = nodeList[j].url;
					a.innerHTML = nodeList[j].description;
					li.appendChild(a);
					document.getElementById(divId).appendChild(li);
					break;
				}
			}
		
		}
	}
}

function ocbBuildOpenNav(divId, currentAssetId){
	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 2 link, indent and apply "selected" class
		if ((nodeList[j].level == 2 && inBreadcrumb == 1) && (nodeList[j].id == currentAssetId)) {
			a.href = nodeList[j].url;
			a.innerHTML = nodeList[j].description;
			li.appendChild(a);
			li.style.marginLeft = "10px";
			li.className = "selected";
			document.getElementById(divId).appendChild(li);
		}
		// If the node is NOT in the breadcrumb, and is a level 2 link, indent
		else if (nodeList[j].level == 2 && inBreadcrumb == 0) {
			a.href = nodeList[j].url;
			a.innerHTML = nodeList[j].description;
			li.appendChild(a);
			li.style.marginLeft = "10px";
			document.getElementById(divId).appendChild(li);
		}
		// If the node is in the breadcrumb, and is a level 1 link, apply "selected" class
		else if ((nodeList[j].level == 1 && inBreadcrumb == 1) && (nodeList[j].id == currentAssetId)) {
			a.href = nodeList[j].url;
			a.innerHTML = nodeList[j].description;
			li.appendChild(a);
			li.className = "selected";
			document.getElementById(divId).appendChild(li);
		}
		// Finally, if the node is not in the breadcrumb, display it without selected or indented styles
		else if (nodeList[j].level == 1 && inBreadcrumb == 0) {			
			a.href = nodeList[j].url;
			a.innerHTML = nodeList[j].description;
			li.appendChild(a);
			document.getElementById(divId).appendChild(li);
		}
		
		else {
			// Next, display the nodes that share a common parent
			for(s=0; s < crumbLevel; s++) {
				// If node is level 2, then apply indent
				if (nodeList[j].level == 2) {
					a.href = nodeList[j].url;
					a.innerHTML = nodeList[j].description;
					li.appendChild(a);
					li.style.marginLeft = "10px";
					document.getElementById(divId).appendChild(li);
				}
				// If node is level 1, don't apply indent
				else if (nodeList[j].level == 1) {
					a.href = nodeList[j].url;
					a.innerHTML = nodeList[j].description;
					li.appendChild(a);
					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
		}
	}
}