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(4045,"Market Information","/mktinfo/index.html",1+1,11,0);
		nodeList[i++] = new Node(277204,"Congestion Revenue Rights","/mktinfo/crr/index.html",2+1,0,0);
		nodeList[i++] = new Node(279604,"Day-Ahead Market","/mktinfo/dam/index.html",2+1,0,0);
		nodeList[i++] = new Node(279605,"Real-Time Market","/mktinfo/rtm/index.html",2+1,0,0);
		nodeList[i++] = new Node(4040,"Market Prices","/mktinfo/prices/index.html",2+1,0,0);
		nodeList[i++] = new Node(4042,"Retail","/mktinfo/retail/index.html",2+1,0,0);
		nodeList[i++] = new Node(4041,"Load Profiling","/mktinfo/loadprofile/index.html",2+1,1,0);
		nodeList[i++] = new Node(121290,"Backcasted (Actual) Load Profiles","/mktinfo/loadprofile/alp/index.html",3+1,0,0);
		nodeList[i++] = new Node(43017,"Metering","/mktinfo/metering/index.html",2+1,3,0);
		nodeList[i++] = new Node(20703,"Competitive Metering","/mktinfo/metering/competitive/index.html",3+1,0,0);
		nodeList[i++] = new Node(20302,"ERCOT-Polled Settlement Metering","/mktinfo/metering/eps/index.html",3+1,0,0);
		nodeList[i++] = new Node(248747,"TDSP Distribution Loss Factors - Methodology","/mktinfo/metering/dlfmethodology/index.html",3+1,0,0);
		nodeList[i++] = new Node(42451,"Data Aggregation","/mktinfo/data_agg/index.html",2+1,4,0);
		nodeList[i++] = new Node(16283,"IDR Protocol Compliance Verification - Historical","/mktinfo/data_agg/idr_pcv.html",3+1,0,0);
		nodeList[i++] = new Node(57374,"Planned Service Four Coincident Peak Calculations","/mktinfo/data_agg/4cp/index.html",3+1,0,0);
		nodeList[i++] = new Node(84111,"Shadow Settlement Instruction Guides","/mktinfo/data_agg/ssig/index.html",3+1,0,0);
		nodeList[i++] = new Node(190632,"UFE Analysis Reports","/mktinfo/data_agg/ufe/index.html",3+1,0,0);
		nodeList[i++] = new Node(4043,"Settlements","/mktinfo/settlements/index.html",2+1,0,0);
		nodeList[i++] = new Node(4044,"Market Reports","/mktinfo/reports/index.html",2+1,0,0);
		nodeList[i++] = new Node(291539,"Zonal Market Information Report Archives","/mktinfo/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 "";
}

