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(2527,"Services","/services/index.html",1+1,8,0);
		nodeList[i++] = new Node(25144,"Client Services","/services/client_svcs/index.html",2+1,3,0);
		nodeList[i++] = new Node(2530,"Account Management","/services/client_svcs/acctmgmt/index.html",3+1,0,0);
		nodeList[i++] = new Node(25084,"Market Support","/services/client_svcs/mktsupport/index.html",3+1,0,0);
		nodeList[i++] = new Node(109479,"MarkeTrak Information","/services/client_svcs/mktrk_info/index.html",3+1,0,0);
		nodeList[i++] = new Node(2534,"Market Participant Communications","/services/comm/index.html",2+1,4,0);
		nodeList[i++] = new Node(253979,"ERCOT RSS Feeds","/services/comm/feeds.html",3+1,0,0);
		nodeList[i++] = new Node(11342,"Market Notices and Bulletins","/services/comm/mkt_notices/index.html",3+1,4,0);
		nodeList[i++] = new Node(231228,"Market Notice Archive","/services/comm/mkt_notices/notice_archives/index.html",4+1,0,0);
		nodeList[i++] = new Node(23225,"Market Operations Bulletins","/services/comm/mkt_notices/mobs/index.html",4+1,0,0);
		nodeList[i++] = new Node(247359,"Operations Messages","/services/comm/mkt_notices/opsmessages/index.html",4+1,0,0);
		nodeList[i++] = new Node(23227,"Power Operations Bulletins","/services/comm/mkt_notices/pobs/index.html",4+1,0,0);
		nodeList[i++] = new Node(19822,"Retail Market Call","/services/comm/rmc/index.html",3+1,0,0);
		nodeList[i++] = new Node(231352,"System Change and Outage Notices","/services/comm/sys_notices/index.html",3+1,2,0);
		nodeList[i++] = new Node(23223,"System Change Notices","/services/comm/mkt_notices/scns/index.html",4+1,0,0);
		nodeList[i++] = new Node(163030,"System Outage Notices","/services/comm/mkt_notices/outage_notices/index.html",4+1,0,0);
		nodeList[i++] = new Node(2535,"Programs","/services/programs/index.html",2+1,5,0);
		nodeList[i++] = new Node(10322,"Load Participation Program","/services/programs/load/index.html",3+1,4,0);
		nodeList[i++] = new Node(10424,"Balancing Up Load","/services/programs/load/bul/index.html",4+1,0,0);
		nodeList[i++] = new Node(195460,"Emergency Interruptible Load Service","/services/programs/load/eils/index.html",4+1,0,0);
		nodeList[i++] = new Node(10425,"Load Acting as a Resource","/services/programs/load/laar/index.html",4+1,0,0);
		nodeList[i++] = new Node(10546,"Voluntary Load Reduction Programs","/services/programs/load/vlrp/index.html",4+1,0,0);
		nodeList[i++] = new Node(59601,"Load Research Sampling","/services/programs/lrs/index.html",3+1,0,0);
		nodeList[i++] = new Node(120414,"QSE Services Available on Short Notice","/services/programs/qse/index.html",3+1,0,0);
		nodeList[i++] = new Node(19883,"Renewable Energy Credit","/services/programs/rec/index.html",3+1,0,0);
		nodeList[i++] = new Node(19882,"Transmission Congestion Rights","/services/programs/tcr/index.html",3+1,0,0);
		nodeList[i++] = new Node(53570,"Projects","/services/projects/index.html",2+1,2,0);
		nodeList[i++] = new Node(9103,"Current Projects","/services/projects/current.html",3+1,0,0);
		nodeList[i++] = new Node(115020,"Project Priority Planning Process","/services/projects/pp/index.html",3+1,0,0);
		nodeList[i++] = new Node(2532,"Registration and Qualification","/services/rq/index.html",2+1,5,0);
		nodeList[i++] = new Node(146640,"Congestion Revenue Rights Account Holders","/services/rq/crr/index.html",3+1,0,0);
		nodeList[i++] = new Node(39399,"Load Serving Entities","/services/rq/lse/index.html",3+1,1,0);
		nodeList[i++] = new Node(4722,"Texas Retail Market Testing","/services/rq/lse/trt/index.html",4+1,1,0);
		nodeList[i++] = new Node(66550,"Retail Market Test Flight Information","/services/rq/lse/tfi/index.html",5+1,0,0);
		nodeList[i++] = new Node(39412,"Qualified Scheduling Entities","/services/rq/qse/index.html",3+1,0,0);
		nodeList[i++] = new Node(39419,"Resource Entities","/services/rq/re/index.html",3+1,0,0);
		nodeList[i++] = new Node(39424,"Transmission / Distribution Service Providers","/services/rq/tdsp/index.html",3+1,0,0);
		nodeList[i++] = new Node(197992,"Service Level Agreements","/services/sla/index.html",2+1,0,0);
		nodeList[i++] = new Node(21105,"Training","/services/training/index.html",2+1,0,0);
		nodeList[i++] = new Node(224986,"Market Data Transparency","/services/mdt/index.html",2+1,3,0);
		nodeList[i++] = new Node(224987,"DDLs","/services/mdt/ddls/index.html",3+1,0,0);
		nodeList[i++] = new Node(226102,"User Guides","/services/mdt/userguides/index.html",3+1,0,0);
		nodeList[i++] = new Node(224990,"XSDs","/services/mdt/xsds/index.html",3+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 "";
}
