// MODX Wayfinder 2.0 menu items javascript highlighter  
// for CSS styles see (tere.css) with javascript
/*
	in any MODX template which will have a nav_level2 menu,...
		0. Make sure the file highLightHereInNav2.js is in the folder
			 ./assets/js/
		1. Make sure the Wayfinder parent DIV has the attribute id="nav_level2"
			e.g. <div class="nav_level2" id="nav_level2">

		2. put this header
			<script type="text/javascript" src="./assets/js/highLightHereInNav2.js"></script>
	
		3. put this as a  BODY tag attribute
			onload="highLightHereInNav2('nav_level2')"

		4. Make sure the wayfinder tag has the &hereTpl='TRUE' 
			e.g. [[Wayfinder? &startId=`2`&hereTpl='TRUE']]
*/
// 
globalString = "";
recursionCount = -1;
recursionDepth = -1;

// 
function highLightHereInNav2 (myObjName) { 
	// re inititialize globals
	globalString = "";
	recursionCount = -1;
	recursionDepth = -1;

	targetChildren = document.getElementById(myObjName).childNodes;
	tmpString = "";
	
	// CALL RECURSIVE FUNCTION
	recurseLi(targetChildren);
	
	//alert("recursionLi ("+recursionCount+","+recursionDepth+")finished : \n"+globalString);
}
	
//
function recurseLi(myNodeChildren){
	recursionCount++;
	recursionDepth++;


	// RECUSION PROTECTION 
	if(recursionCount > 100 || recursionDepth/2 > 4){
		return;
	}
	// RECURSION FILTERING LOGIC
	else{
		for(var i = 0; i < myNodeChildren.length; i++) // N.B. i MUST BE LOCALLY DEFINED
		{
			// Only if LIs
			if (myNodeChildren[i].nodeName != "LI" && myNodeChildren[i].nodeName != "UL") {
				continue; 
			}   
			else{
				// do something
				globalString += recursionCount+"("+recursionDepth+"): i="+i+" "+myNodeChildren[i].nodeName+" , "+myNodeChildren[i].className+" , "+myNodeChildren[i].childNodes[0].innerHTML+"\n";

				if(myNodeChildren[i].className == "active" || myNodeChildren[i].className == "last active"){
					myNodeChildren[i].childNodes[0].style.borderLeftColor = "#30669A";
					myNodeChildren[i].childNodes[0].style.backgroundColor = "#FE9900";
					myNodeChildren[i].childNodes[0].style.color = "#000000";//"#002673;"//"#FFFFFF";
					
					//myNodeChildren[i].childNodes[1].setAttribute("class", "some_class_name");
					//myNodeChildren[i].childNodes[1].setAttribute("className", "some_class_name");
				}

				//alert(recursionCount+"->"+myNodeChildren[i].nodeName+" , "+myNodeChildren[i].className);
				//recurse ONLY IF
				if(myNodeChildren[i].hasChildNodes && myNodeChildren[i].nodeName != ""){
					//globalString += " -> "+myNodeChildren[i].nodeName+"\n";
					
					
					// RECURSION
					recurseLi(myNodeChildren[i].childNodes);
				}
			}

		}
		recursionDepth--;
		return;
	}
}
