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",0,1,0);

		nodeList[i++] = new Node(2442,"About ERCOT","/about/index.html",1,7,0);
		nodeList[i++] = new Node(2387,"Company Profile","/about/profile/index.html",2,2,0);
		nodeList[i++] = new Node(5494,"Vision and Mission","/about/profile/vision.html",3,0,0);
		nodeList[i++] = new Node(2443,"History","/about/profile/history/index.html",3,0,0);
		nodeList[i++] = new Node(2449,"Governance","/about/governance/index.html",2,5,0);
		nodeList[i++] = new Node(3244,"Board of Directors","/about/governance/directors.html",3,0,0);
		nodeList[i++] = new Node(3245,"Executives","/about/governance/executives.html",3,0,0);
		nodeList[i++] = new Node(3170,"Membership","/about/governance/members/index.html",3,0,0);
		nodeList[i++] = new Node(2450,"Legal Notices","/about/governance/legal_notices.html",3,0,0);
		nodeList[i++] = new Node(2452,"Ethics","/about/governance/ethics.html",3,0,0);
		nodeList[i++] = new Node(2453,"News and Publications","/news/index.html",2,3,0);
		nodeList[i++] = new Node(558,"Press Releases","/news/press_releases/index.html",3,0,0);
		nodeList[i++] = new Node(2604,"Media Kit","/news/mediakit/index.html",3,4,0);
		nodeList[i++] = new Node(2609,"ERCOT Organization Backgrounder","/news/mediakit/backgrounder.html",4,0,0);
		nodeList[i++] = new Node(2623,"Corporate Logos","/news/mediakit/logos.html",4,0,0);
		nodeList[i++] = new Node(2627,"Corporate Images","/news/mediakit/corp_images.html",4,0,0);
		nodeList[i++] = new Node(123791,"Maps","/news/mediakit/maps/index.html",4,0,0);
		nodeList[i++] = new Node(6202,"Reports and Presentations","/news/presentations/index.html",3,0,0);
		nodeList[i++] = new Node(2454,"Careers","/careers/index.html",2,0,0);
		nodeList[i++] = new Node(2456,"Procurement","/about/procurement/index.html",2,0,0);
		nodeList[i++] = new Node(2457,"Locations","/about/locations/index.html",2,0,0);
		nodeList[i++] = new Node(2458,"Contact Us","/about/contact/index.html",2,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 > 3)
		{
			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 >= 1) {
			
			if (currentNode.level == 1)
			{
				topId = id;
			}
				
			if (currentNode.level >= 3) 
				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 "";
}

