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(5922,"Committees and Groups","/committees/index.html",1+1,11,0);
		nodeList[i++] = new Node(4250,"Meeting Calendar","/calendar/index.html",2+1,0,0);
		nodeList[i++] = new Node(5059,"Board of Directors","/committees/board/index.html",2+1,4,0);
		nodeList[i++] = new Node(11688,"Finance and Audit Committee","/committees/board/finance_audit/index.html",3+1,1,0);
		nodeList[i++] = new Node(11693,"Credit Work Group","/committees/board/finance_audit/creditwg/index.html",4+1,0,0);
		nodeList[i++] = new Node(11697,"HR and Governance Committee","/committees/board/hr_governance/index.html",3+1,0,0);
		nodeList[i++] = new Node(291177,"Nominating Committee","/committees/board/bodnom/index.html",3+1,0,0);
		nodeList[i++] = new Node(295884,"Subcommittee for Unaffiliated Director Searches","/committees/board/director_search/index.html",3+1,0,0);
		nodeList[i++] = new Node(4885,"TAC","/committees/board/tac/index.html",2+1,1,0);
		nodeList[i++] = new Node(302676,"METF","/committees/board/tac/metf/index.html",3+1,0,0);
		nodeList[i++] = new Node(5060,"COPS","/committees/board/tac/cops/index.html",2+1,2,0);
		nodeList[i++] = new Node(302341,"CSWG","/committees/board/tac/cops/cswg/index.html",3+1,0,0);
		nodeList[i++] = new Node(5070,"PWG","/committees/board/tac/cops/pwg/index.html",3+1,0,0);
		nodeList[i++] = new Node(4903,"PRS","/committees/board/tac/prs/index.html",2+1,0,0);
		nodeList[i++] = new Node(5066,"RMS","/committees/board/tac/rms/index.html",2+1,6,0);
		nodeList[i++] = new Node(146850,"MarkeTrak TF","/committees/board/tac/rms/marketraktf/index.html",3+1,0,0);
		nodeList[i++] = new Node(201668,"MARSTF","/committees/board/tac/rms/marstf/index.html",3+1,0,0);
		nodeList[i++] = new Node(88431,"MCT","/committees/board/tac/rms/mct/index.html",3+1,0,0);
		nodeList[i++] = new Node(300073,"MIT","/committees/board/tac/rms/mit/index.html",3+1,0,0);
		nodeList[i++] = new Node(5104,"TDTWG","/committees/board/tac/rms/tdtwg/index.html",3+1,0,0);
		nodeList[i++] = new Node(5071,"Texas SET WG","/committees/board/tac/rms/txset/index.html",3+1,0,0);
		nodeList[i++] = new Node(5074,"ROS","/committees/board/tac/ros/index.html",2+1,12,0);
		nodeList[i++] = new Node(11510,"BSWG","/committees/board/tac/ros/bswg/index.html",3+1,0,0);
		nodeList[i++] = new Node(223993,"CIPWG","/committees/board/tac/ros/cipwg/index.html",3+1,1,0);
		nodeList[i++] = new Node(111124,"CIPAG","/committees/board/tac/ros/cipwg/cipag/index.html",4+1,0,0);
		nodeList[i++] = new Node(5103,"DWG","/committees/board/tac/ros/dwg/index.html",3+1,0,0);
		nodeList[i++] = new Node(5105,"NDSWG","/committees/board/tac/ros/ndswg/index.html",3+1,0,0);
		nodeList[i++] = new Node(289123,"NPGRTF","/committees/board/tac/ros/npgrtf/index.html",3+1,0,0);
		nodeList[i++] = new Node(289981,"NRWG","/committees/board/tac/ros/nrwg/index.html",3+1,0,0);
		nodeList[i++] = new Node(11494,"OTF","/committees/board/tac/ros/otf/index.html",3+1,0,0);
		nodeList[i++] = new Node(5106,"OWG","/committees/board/tac/ros/owg/index.html",3+1,1,0);
		nodeList[i++] = new Node(11490,"OGRTF","/committees/board/tac/ros/owg/ogrtf/index.html",4+1,0,0);
		nodeList[i++] = new Node(5108,"PDCWG","/committees/board/tac/ros/pdcwg/index.html",3+1,0,0);
		nodeList[i++] = new Node(263735,"PLWG","/committees/board/tac/ros/plwg/index.html",3+1,0,0);
		nodeList[i++] = new Node(5109,"SPWG","/committees/board/tac/ros/spwg/index.html",3+1,2,0);
		nodeList[i++] = new Node(11514,"DCCTF","/committees/board/tac/ros/spwg/dcctf/index.html",4+1,0,0);
		nodeList[i++] = new Node(11527,"PRTF","/committees/board/tac/ros/spwg/patton_review/index.html",4+1,0,0);
		nodeList[i++] = new Node(5110,"SSWG","/committees/board/tac/ros/sswg/index.html",3+1,0,0);
		nodeList[i++] = new Node(5115,"WMS","/committees/board/tac/wms/index.html",2+1,10,0);
		nodeList[i++] = new Node(5117,"CMWG","/committees/board/tac/wms/cmwg/index.html",3+1,0,0);
		nodeList[i++] = new Node(5118,"DSWG","/committees/board/tac/wms/dswg/index.html",3+1,0,0);
		nodeList[i++] = new Node(271349,"ETWG","/committees/board/tac/wms/etwg/index.html",3+1,0,0);
		nodeList[i++] = new Node(11519,"GATF","/committees/board/tac/wms/gatf/index.html",3+1,0,0);
		nodeList[i++] = new Node(227028,"MCWG","/committees/board/tac/wms/mcwg/index.html",3+1,0,0);
		nodeList[i++] = new Node(5119,"MWG","/committees/board/tac/wms/mwg/index.html",3+1,0,0);
		nodeList[i++] = new Node(5120,"QMWG","/committees/board/tac/wms/qmwg/index.html",3+1,0,0);
		nodeList[i++] = new Node(244610,"QSTF","/committees/board/tac/wms/qstf/index.html",3+1,0,0);
		nodeList[i++] = new Node(298723,"RDTF","/committees/board/tac/wms/rdtf/index.html",3+1,0,0);
		nodeList[i++] = new Node(189935,"VCWG","/committees/board/tac/wms/vcwg/index.html",3+1,0,0);
		nodeList[i++] = new Node(135915,"Nodal Groups","/committees/nodal/index.html",2+1,0,0);
		nodeList[i++] = new Node(59572,"Other Groups","/committees/other/index.html",2+1,4,0);
		nodeList[i++] = new Node(264264,"Long-Term Study Task Force","/committees/other/lts/index.html",3+1,1,0);
		nodeList[i++] = new Node(273584,"SDWG","/committees/other/lts/sdwg/index.html",4+1,0,0);
		nodeList[i++] = new Node(285982,"Market Information System User Group","/committees/other/mis/index.html",3+1,0,0);
		nodeList[i++] = new Node(124891,"Regional Planning Group","/committees/other/rpg/index.html",3+1,1,0);
		nodeList[i++] = new Node(155091,"Competitive Renewable Energy Zone Task Force","/committees/other/rpg/crez/index.html",4+1,0,0);
		nodeList[i++] = new Node(5113,"Training Seminar Working Group","/committees/board/tac/swg/index.html",3+1,0,0);
		nodeList[i++] = new Node(43935,"Market Participants","/mktparticipants/index.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 "";
}

