// JavaScript Document

////////////////////////////////////////////////////////
// FUNCTIONS THAT RUN THE MENUS FOR DDAL LEFT COLUMN  //
////////////////////////////////////////////////////////


// Returns the directory to a particular number of levels
function returnDir(url,levels) {
	// first we'll strip the domain name and stuff out
	var leadSlashes = url.indexOf("//");
	var domainStart = leadSlashes + 2;
	url = url.substring(domainStart,url.length);
	var nextSlash = url.indexOf("/");
	pathFromRoot = url.substring(nextSlash,url.length);
	
	pathArray = pathFromRoot.split("/");
	
	var rootPath = "/";
	// Now truncate the path by as many levels as we asked for
	for(var i=1; i < levels+1 ; i++) {
		rootPath = rootPath + pathArray[i] + "/";
	}
	return rootPath;
}
// returns the full path from the root on a passed URL
function returnPagePath(url) {
	// first we'll strip the domain name and stuff out
	var leadSlashes = url.indexOf("//");
	var domainStart = leadSlashes + 2;
	url = url.substring(domainStart,url.length);
	var nextSlash = url.indexOf("/");
	pathFromRoot = url.substring(nextSlash,url.length);
	return pathFromRoot;

}
// Gets the directory info from the page URL
function returnPageDir(levels) {
	var dir = location.href.substring(0,location.href.lastIndexOf('/')+1);
	if(levels == 'all') {
		myVar = returnPagePath(dir);
	} else {
		myVar = returnDir(dir,levels);
	}
	return myVar;
}

// Gets the number of levels in a passed dir
function returnLevels(path) {
	var pathArray = path.split("/");
	var pathLevels = pathArray.length -2;
	return pathLevels;
}

// Compares two URLs to each other
function matchURLs(url1,url2) {
	var matched = false;
	if(url1 == url2) {
		matched = true;	
	}
	return matched;
}

//
// Function runs through a menu node and prints it out
//
function printMenu(mainNode,subNode) {
	//document.write("<pre>Passed Variables = " + mainNode + ":" + subNode +"</pre>");

	// Print out top nodes
	var flagged = 0;
	var increment = 0; // number we'll use to reduce the levels in our page URL

	// get the number of levels in the active page's directory so we know how many times we can loop through it
	var pageLevels = returnLevels(returnPageDir('all'));
	// Make a correction to ensure we loop in NN4
	if(pageLevels == 0){
		pageLevels = 1;
	}
	
	//document.write("Page levels = " + pageLevels);
	// This is the top level while loop that lets us cycle through directory levels looking for either the current page
	// or parent links.	
	while((pageLevels - increment) > 0) {
	
		
		//document.write("<br />first while loop running");
		//document.write("level "+ increment + "<br />");
		
		
		// This is the WHILE LOOP we use just to evaluate the menu for matches so we'll know if at the end we need to either 
		// print the menu OR loop through again on the next highest level to look for parents.
		var i=0;	
		while(i < menuLabels[mainNode].length) {
			//document.write("<br />second while loop running");
		
			// Simplify these two variables
			var menuDir = menuURLs[mainNode][i][0];
			var menuLabel = menuLabels[mainNode][i][0];
		
			menuURLlevels = returnLevels(menuDir);
			pageDir = returnPageDir(pageLevels - increment);//returnPageDir(menuURLlevels-increment);  get our page directory

			if(matchURLs(pageDir,menuDir) == true) {
				flagged = 1; // Set the flag if there was a match between the page URL and the one in the menu
			}
			
			// Loop through the submenus
			if(subNode != 'all') {
				if(i == subNode && menuLabels[mainNode][i].length > 1) {
					// Print out subnode
					for(var j=1; j < menuLabels[mainNode][i].length; j++ ) {
						if(matchURLs(pageDir,menuURLs[mainNode][i][j]) == true) {
							flagged = 1; // Set the flag if there was a match between the page URL and the one in the menu
						}
					}
				} 
			}
			i++; // Increment our While Loop
		}
		// At the end of our while loop, check to see if a match was found. If not, we'll loop through again on the 
		// level above the current page URL to see if a parent link can be found
		// 
		// Otherwise we'll print out the whole page (we'll also print it if no matches were found BUT we're on a first level
		// page, since they don't have matches by definiton)
		if(flagged == 1 || (flagged == 0 && pageLevels == 1)) {
			//document.write("Found a match");
			// The WHILE loop that ACTUALLY PRINTS THE MENU
			var i=0;	
			while(i < menuLabels[mainNode].length) {
			
				// Simplify these two variables
				var menuDir = menuURLs[mainNode][i][0];
				var menuLabel = menuLabels[mainNode][i][0];
			
				menuURLlevels = returnLevels(menuDir);
				pageDir = returnPageDir(pageLevels - increment);//returnPageDir(menuURLlevels-increment);  get our page directory
				document.write("<h3>");
				document.write("<a href=\"" + menuDir + "\"");
				if(matchURLs(pageDir,menuDir) == true) {
					document.write("class=\"thisPage\"");
				}
				document.write(">" + menuLabel + "</a>");
				document.write("</h3>");
				// Loop through the correct submenu (if submenu exists AND they didn't request that no submenus be shown)
				if(i == subNode && menuLabels[mainNode][i].length > 1 && subNode != 'none') {
					//Print out subnode
					document.write("<ul>");
					for(var j=1; j < menuLabels[mainNode][i].length; j++ ) {
						document.write("<li>");
						document.write("<a href=\"" + menuURLs[mainNode][i][j] + "\"");
						if(matchURLs(pageDir,menuURLs[mainNode][i][j]) == true) {
							document.write("class=\"thisPage\"");
						}
						document.write(">" + menuLabels[mainNode][i][j] + "</a>");
						document.write("</li>");
					}
					document.write("</ul>");
				} 
				i++; // Increment our While Loop
			}
			break;
		} else {
			increment++;		
		}
	}

}
