var nodeList = new Array();
var breadcrumb = new Array();

var map_loaded = false;
var bc_loaded = false;

// 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 getNodeById(id) {
	for (x=0; x < nodeList.length; x++)
		if (nodeList[x].id == id) return nodeList[x];
		
	return null;
}

function getNodeByName(name) {
	for (x=0; x < nodeList.length; x++)
		if (nodeList[x].name == name) return nodeList[x];
	
	return null;
}	

function setParentNode(x) {
	for(p = x - 1; p > 0; p--) {						// Search up the site structure node list.
		if ((nodeList[p].children > 0) &&				// If the node has children,
			(nodeList[p].level < nodeList[x].level)) {	//   and is at a higher level in the tree, then you have found the parent
			nodeList[x].parent = nodeList[p].id;		// Set the parent asset id
			nodeList[p].children--;						// Decrease its number of children
			break;										// Stop looking for the parent
		}

	}
}

function initSiteNav() {	

	if (map_loaded == false) {
		var i = 0;


		nodeList[i++] = new Node(67,"Home","/index.html",1,50,0);

		nodeList[i++] = new Node(3862,"Grid Information","/gridinfo/index.html",1+1,7,0);
		nodeList[i++] = new Node(295145,"Current System Conditions","/gridinfo/csc/index.html",2+1,0,0);
		nodeList[i++] = new Node(291991,"Emerging Technology Tracking System","/gridinfo/etts/index.html",2+1,7,0);
		nodeList[i++] = new Node(291961,"Battery Storage","/gridinfo/etts/battery/index.html",3+1,0,0);
		nodeList[i++] = new Node(291975,"Compressed Air Energy Storage","/gridinfo/etts/compressedair/index.html",3+1,0,0);
		nodeList[i++] = new Node(291968,"Flywheel","/gridinfo/etts/flywheel/index.html",3+1,0,0);
		nodeList[i++] = new Node(292881,"Solar","/gridinfo/etts/solar/index.html",3+1,0,0);
		nodeList[i++] = new Node(291989,"Vehicle to Grid","/gridinfo/etts/vehicle/index.html",3+1,0,0);
		nodeList[i++] = new Node(291982,"Wind-Powered Generation Resource","/gridinfo/etts/wind/index.html",3+1,0,0);
		nodeList[i++] = new Node(291993,"Emerging Technology Submission Process","/gridinfo/etts/submission_form.html",3+1,0,0);
		nodeList[i++] = new Node(4029,"Generation","/gridinfo/generation/index.html",2+1,1,0);
		nodeList[i++] = new Node(296995,"Wind Integration Reports","/gridinfo/generation/windintegration/index.html",3+1,0,0);
		nodeList[i++] = new Node(4027,"Load","/gridinfo/load/index.html",2+1,0,0);
		nodeList[i++] = new Node(4030,"Transmission","/gridinfo/transmission/index.html",2+1,1,0);
		nodeList[i++] = new Node(190091,"Network Operations Model Change Schedule","/gridinfo/transmission/opsys-change-schedule.html",3+1,0,0);
		nodeList[i++] = new Node(99994,"Market Reports","/gridinfo/sysplan/index.html",2+1,1,0);
		nodeList[i++] = new Node(131461,"Availability of Resources for Medium-Term PASA","/gridinfo/sysplan/resourcesmpasa/index.html",3+1,0,0);
		nodeList[i++] = new Node(291130,"Zonal Grid Information Report Archives","/gridinfo/zonal_archives.html",2+1,0,0);

		for(x = 0; x < nodeList.length; x++)
			if (nodeList[x].level > 1) {
				setParentNode(x);
			}

		map_loaded = true;
	}

}

function highlightTopNav(id) {
	var currentNode = nodeList[1];
	if (currentNode != null) {
		var objTopNavSelItem = document.getElementById("s" + currentNode.id);
		if (objTopNavSelItem != null) {
			objTopNavSelItem.id = "active";
			objTopNavSelItem.firstChild.id = "current";
		}
	}
}

function buildSiteNav(id) {

	initSiteNav();

	var currentNode = getNodeById(id);
	if (currentNode != null)
	{
		/*	for sections not listed within left-nav,
			find the parent that is listed within the left-nav */
		while (currentNode.level > 4)
		{
			currentNode = getNodeById(currentNode.parent);
			id = currentNode.id;
		}
		
		var parentId = currentNode.parent;
		var topId = parentId;
		var shtml = "";
		var ihtml = "";

		
		shtml += "<div id=\"navcontainer\"><ul id=\"navlist\">";
		if (currentNode.level >= 2) {
			
			if (currentNode.level == 2)
			{
				topId = id;
			}
				
			if (currentNode.level >= 4) 
				topId = getNodeById(parentId).parent;
				
			highlightTopNav(topId);

			// outer loop through siblings
			for (x = 0; x < nodeList.length; x++) {
				if (nodeList[x].parent == topId) {
					if ((nodeList[x].id == id) || (nodeList[x].id == parentId)) { 
						var childSelected = false;
						// inner loop through children
						for (y = x + 1; y < nodeList.length; y++) {
							if (nodeList[y].parent == nodeList[x].id) {
								if (nodeList[y].id == id) {
									ihtml += "<li id=\"subactive\"><a href=\"" + nodeList[y].url + "\" id=\"subcurrent\">" + nodeList[y].name + "</a></li>";
									childSelected = true;
								}
								else {
									ihtml += "<li><a href=\"" + nodeList[y].url + "\">" + nodeList[y].name + "</a></li>";
								}
							}
						}
						// determine whether child is selected or not
						if (childSelected)
							shtml += "<li id=\"active\"><a href=\"" + nodeList[x].url + "\" id=\"selchild\">" + nodeList[x].name + "</a>";
						else
							shtml += "<li id=\"active\"><a href=\"" + nodeList[x].url + "\" id=\"current\">" + nodeList[x].name + "</a>"; 

						if (ihtml != "")
							shtml += "<ul id=\"subnavlist\">" + ihtml + "</ul>";
						shtml += "</li>";
					}
					else {
						shtml += "<li><a href=\"" + nodeList[x].url + "\">" + nodeList[x].name + "</a></li>";
					}
				}
			}
		}
		
		shtml += "</ul></div>"
		return shtml;
	}
	
	return "";
}

