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(2669,"Market Rules","/mktrules/index.html",1+1,6,0);
		nodeList[i++] = new Node(270258,"Protocols - Nodal","/mktrules/nprotocols/index.html",2+1,6,0);
		nodeList[i++] = new Node(270257,"Current Protocols - Nodal","/mktrules/nprotocols/current.html",3+1,0,0);
		nodeList[i++] = new Node(270657,"Protocol Library - Nodal","/mktrules/nprotocols/lib.html",3+1,0,0);
		nodeList[i++] = new Node(270259,"NPRRs","/mktrules/nprotocols/nprr.html",3+1,0,0);
		nodeList[i++] = new Node(270685,"NPRR Submission Process","/mktrules/nprotocols/nprr_process.html",3+1,0,0);
		nodeList[i++] = new Node(6143,"PIR Submission Process","/mktrules/nprotocols/pir_process.html",3+1,0,0);
		nodeList[i++] = new Node(1563,"Archives - Zonal","/mktrules/protocols/index.html",3+1,2,0);
		nodeList[i++] = new Node(3487,"Zonal Protocol Library","/mktrules/protocols/library/index.html",4+1,0,0);
		nodeList[i++] = new Node(2123,"PRRs","/mktrules/protocols/prr.html",4+1,0,0);
		nodeList[i++] = new Node(6147,"Market Guides","/mktrules/guides/index.html",2+1,10,0);
		nodeList[i++] = new Node(67740,"Commercial Operations Market Guide","/mktrules/guides/commercialops/index.html",3+1,3,0);
		nodeList[i++] = new Node(159632,"Current Commercial Operations Market Guide","/mktrules/guides/commercialops/current.html",4+1,0,0);
		nodeList[i++] = new Node(68813,"Commercial Operations Market Guide Library","/mktrules/guides/commercialops/copmglib.html",4+1,0,0);
		nodeList[i++] = new Node(67741,"Commercial Operations Market Guide Revision Requests","/mktrules/guides/commercialops/copmgrr.html",4+1,0,0);
		nodeList[i++] = new Node(16463,"Data Transport Guides","/mktrules/guides/data_transport/index.html",3+1,2,0);
		nodeList[i++] = new Node(59845,"FTP Replacement/Electronic Business Using XML","/mktrules/guides/data_transport/ebiz/index.html",4+1,0,0);
		nodeList[i++] = new Node(47217,"Security for Digital Certificates","/mktrules/guides/data_transport/security.html",4+1,0,0);
		nodeList[i++] = new Node(3623,"Load Profiling Guide","/mktrules/guides/loadprofiling/index.html",3+1,3,0);
		nodeList[i++] = new Node(104501,"Current Load Profiling Guide","/mktrules/guides/loadprofiling/current.html",4+1,0,0);
		nodeList[i++] = new Node(74765,"Load Profiling Guide Library","/mktrules/guides/loadprofiling/lpglib.html",4+1,0,0);
		nodeList[i++] = new Node(6222,"Load Profiling Guide Revision Requests","/mktrules/guides/loadprofiling/lpgrr.html",4+1,0,0);
		nodeList[i++] = new Node(264841,"Nodal Operating Guides","/mktrules/guides/noperating/index.html",3+1,3,0);
		nodeList[i++] = new Node(264840,"Current Nodal Operating Guides","/mktrules/guides/noperating/cur.html",4+1,0,0);
		nodeList[i++] = new Node(264842,"Nodal Operating Guide Library","/mktrules/guides/noperating/lib.html",4+1,0,0);
		nodeList[i++] = new Node(265087,"Nodal Operating Guide Revision Requests","/mktrules/guides/noperating/nogrr.html",4+1,0,0);
		nodeList[i++] = new Node(6145,"Operating Procedures","/mktrules/guides/procedures/index.html",3+1,0,0);
		nodeList[i++] = new Node(269093,"Planning Guide","/mktrules/guides/planning/index.html",3+1,3,0);
		nodeList[i++] = new Node(269095,"Current Planning Guide","/mktrules/guides/planning/current.html",4+1,0,0);
		nodeList[i++] = new Node(269094,"Planning Guide Library","/mktrules/guides/planning/complete_list.html",4+1,0,0);
		nodeList[i++] = new Node(269096,"Planning Guide Revision Requests","/mktrules/guides/planning/pgrr.html",4+1,0,0);
		nodeList[i++] = new Node(1565,"Retail Market Guide","/mktrules/guides/retail/index.html",3+1,3,0);
		nodeList[i++] = new Node(103405,"Current Retail Market Guide","/mktrules/guides/retail/current.html",4+1,0,0);
		nodeList[i++] = new Node(6163,"Retail Market Guide Library","/mktrules/guides/retail/complete_list.html",4+1,0,0);
		nodeList[i++] = new Node(6164,"Retail Market Guide Revision Requests","/mktrules/guides/retail/rmgrr.html",4+1,0,0);
		nodeList[i++] = new Node(6017,"Settlement Metering Operating Guide","/mktrules/guides/settlement/index.html",3+1,2,0);
		nodeList[i++] = new Node(42611,"Settlement Metering Operating Guide Library","/mktrules/guides/settlement/smog.html",4+1,0,0);
		nodeList[i++] = new Node(6242,"Settlement Metering Operating Guide Revision Requests","/mktrules/guides/settlement/smogrr.html",4+1,0,0);
		nodeList[i++] = new Node(3304,"Texas SET Guides","/mktrules/guides/txset/index.html",3+1,4,0);
		nodeList[i++] = new Node(106207,"Current Texas SET Implementation Guides","/mktrules/guides/txset/tsig.html",4+1,0,0);
		nodeList[i++] = new Node(106208,"Texas SET Swimlanes","/mktrules/guides/txset/sw/index.html",4+1,0,0);
		nodeList[i++] = new Node(62819,"Texas SET Issues","/mktrules/guides/txset/issues.html",4+1,0,0);
		nodeList[i++] = new Node(220896,"Texas SET Change Controls","/mktrules/guides/txset/changecontrols/index.html",4+1,0,0);
		nodeList[i++] = new Node(290569,"Inactive Market Guides","/mktrules/guides/archives.html",3+1,2,0);
		nodeList[i++] = new Node(6019,"Competitive Metering Guide","/mktrules/guides/competitive/index.html",4+1,2,0);
		nodeList[i++] = new Node(46950,"Competitive Metering Guide Library (Inactive)","/mktrules/guides/competitive/cmg_library.html",5+1,0,0);
		nodeList[i++] = new Node(6246,"Competitive Metering Guide Revision Requests","/mktrules/guides/competitive/cmgrr.html",5+1,0,0);
		nodeList[i++] = new Node(1539,"Operating Guides","/mktrules/guides/operating/index.html",4+1,2,0);
		nodeList[i++] = new Node(9502,"Operating Guide Library (Inactive)","/mktrules/guides/operating/complete_list.html",5+1,0,0);
		nodeList[i++] = new Node(12683,"Operating Guide Revision Requests","/mktrules/guides/operating/ogrr.html",5+1,0,0);
		nodeList[i++] = new Node(1562,"System Changes","/mktrules/syschangereq/index.html",2+1,1,0);
		nodeList[i++] = new Node(3962,"SCRs","/mktrules/syschangereq/scrs.html",3+1,0,0);
		nodeList[i++] = new Node(6144,"Compliance","/mktrules/compliance/index.html",2+1,0,0);
		nodeList[i++] = new Node(277651,"Business Practice Manual","/mktrules/bpm/index.html",2+1,0,0);
		nodeList[i++] = new Node(278010,"Other Binding Documents","/mktrules/obd/index.html",2+1,2,0);
		nodeList[i++] = new Node(281254,"Other Binding Documents List","/mktrules/obd/obdlist.html",3+1,0,0);
		nodeList[i++] = new Node(281247,"Other Binding Documents Revision Requests","/mktrules/obd/obdrr.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 "";
}

