/////////////////////////////////////////////////////////////////////////////
// Function : Section Navigation Menu
// Comments : 
/////////////////////////////////////////////////////////////////////////////

function SectionNavigation(nodeId){
	SectionNavigation.prototype.display = SectionNavigation_display;
	SectionNavigation.prototype.findNode = SectionNavigation_findNode;
	SectionNavigation.prototype.sectionLevelNode = SectionNavigation_sectionLevelNode;
	SectionNavigation.prototype.displayNode = SectionNavigation_displayNode;
	
	this.m_thisNode = this.findNode(g_navNode_Root, nodeId);
}



function SectionNavigation_display() {
	this.m_sectionTree = this.displayNode(this.m_thisNode, '', true);	

	document.writeln(this.m_sectionTree);		
}

function SectionNavigation_findNode(theNode, nodeId) {
	var foundNode = false;

	try {
		if (theNode.m_id == nodeId) {
			foundNode = theNode;
		}
		else {
			if (theNode.m_subNodes.length > 0) {
				for (var i = 0; i < theNode.m_subNodes.length; i++) {
					childNode = theNode.m_subNodes[i];
					foundNode = this.findNode(childNode, nodeId);
					if (foundNode != false){
						break;
					}
				}
			}
		}
	}
	catch (e) {
		g_errorHandler('SectionNavigation_findNode',e);
	}
	return foundNode;		
}

function SectionNavigation_sectionLevelNode(rootLevelNode,currentLevelNode){
	var foundNode = false;
	var tempNode;
	
	try{
		for (var x=0;x<rootLevelNode.m_subNodes.length;x++){
			tempNode = currentLevelNode;
			while (tempNode.m_id != rootLevelNode.m_id){
				if (tempNode.m_id == rootLevelNode.m_subNodes[x].m_id){
					foundNode = tempNode;
					break;	
				}					
				tempNode = currentLevelNode.m_parent;				
			}		
			if (foundNode != false)break;	
		}
	}catch(e){
		g_errorHandler('SectionNavigation_sectionLevelNode',e);	
	}	
	return foundNode;
}

function SectionNavigation_displayNode(theNode, menuSoFar, includeChildren){
	try {
		var result = '';
		
		if (menuSoFar == null)
			menuSoFar = "";
		
		if (includeChildren == null)
			includeChildren = false;
		

		// build lowest level list of nodes to start the iteration that builds the entire node list
		if (includeChildren && theNode.m_subNodes != null && theNode.m_subNodes.length>0){
			if (g_debug) alert("Building child menu for " + theNode.m_label);
			menuSoFar = "";
			for (var i=0;i<theNode.m_subNodes.length;i++){
				menuSoFar += '<div class="nodeLevel_' + parseInt(theNode.m_subNodes[i].m_level) + '"><a href="' + theNode.m_subNodes[i].m_href + '">' + theNode.m_subNodes[i].m_label + '</a></div>\n';
			}
		}		
			
		if (theNode.m_level == 1 && includeChildren){
			if (g_debug) alert("At top level so only show child menu");
			result = menuSoFar;
		} else if (theNode.m_subNodes != null) {
			var myParent = theNode.m_parent;
			for (var x=0;x<myParent.m_subNodes.length;x++){
				var childNode = myParent.m_subNodes[x];
				if (g_debug) alert("Menu when looping " + myParent.m_label + " for child " + childNode.m_label + "\n" + result);
				
				if (childNode.m_id == this.m_thisNode.m_id) {
				
					result += '<div id="selected_nodeLevel" class="nodeLevel_' + parseInt(childNode.m_level)  + '"><a href="' + childNode.m_href + '">\n' + childNode.m_label + '</a></div>';					
				}
				else {
					result += '<div class="nodeLevel_' + parseInt(childNode.m_level) + '"><a href="' + childNode.m_href + '">\n' + childNode.m_label + '</a></div>';
				}
				
				if (childNode.m_id == theNode.m_id) {
					if (g_debug) alert("Found node " + childNode.m_label + " so append menuSoFar");
					result += menuSoFar;
				}			
			}
		
			if (myParent.m_level > 1){
				if (g_debug) alert("Result before looping parent " + myParent.m_label + ":\n" + result);
				result = this.displayNode(myParent, result);
		  	}
		}
		
  		if (theNode.m_parent != null && theNode.m_parent.m_level == 1){
	  		result = '<div class="nodeLevel_0">' + theNode.m_parent.m_label + '</div>' + result;
  		}else if(includeChildren && theNode.m_parent != null && theNode.m_parent.m_level == 0)
  			result = '<div class="nodeLevel_0">' + theNode.m_label + '</div>' + result;
	}catch(e){
		g_errorHandler("SectionNavigation_displayNode", e);
	}finally{
		if (g_debug) alert("Final menu\n" + result);	
		return result;
	}
}

