// Left Navigation Script for NYC.gov Agency Templates

// Returns path with folder and file, else -1.
// ex. /folder/file.html
function getPath(thePath) {
	if (thePath == null) { return -1; }
	var startSlash = thePath.lastIndexOf("/");
	startSlash = thePath.lastIndexOf("/",startSlash -1);

	if (startSlash == -1) {
		return -1;
	} else {
		return thePath.substr(startSlash, thePath.length - startSlash);
	}
}

// Returns folder, else -1.
// ex. /folder/
function getFolder(thePath) {
	if (thePath == null) { return -1; }
	var startSlash = thePath.lastIndexOf("/");
	startSlash2 = thePath.lastIndexOf("/",startSlash -1);

	if(startSlash == startSlash2) { return -1; }
	if (startSlash == -1 || startSlash2 == -1) {
		return -1;
	} else {
		return thePath.substr(startSlash2, startSlash - startSlash2 + 1);
	}
}

function convertURL(theUrl){
	var underscore=theUrl.lastIndexOf("_");
	var per=theUrl.lastIndexOf(".");
	return(theUrl.substr(0,underscore)+theUrl.substr(per));
}
if ((window.location.href).indexOf("translate_c?hl=en") == -1) { //do not show graphic nav if within google translate

var currentURL = getPath(location.href);
var convertedURL = convertURL(currentURL);
var expandCurrentNode = false;
var haveNodes = false;
var drawnSub=false;

document.write("<table width='100%' border='0' cellspacing='0' cellpadding='0' class='nav_table'>");

for(x=0;x<NAV_NODES.length;x++) {
	expandCurrentNode = false;
	haveNodes = false;
	drawnSub=false;

	//check for sub nodes
	if (NAV_NODES[x] == null) break;
	if (NAV_NODES[x].length > 2) {
		if (currentURL.indexOf(getFolder(NAV_NODES[x][1])) != -1) {
			expandCurrentNode = true;
		}
		for(y=2;y<NAV_NODES[x].length;y++) {
			if (NAV_NODES[x][y] == null) break;
			if (NAV_NODES[x][y][1] != null) haveNodes = true;
			if (currentURL.indexOf(getPath(NAV_NODES[x][y][1])) != -1) {
				expandCurrentNode = true;
			}
		}
	}
	var theFolder=getFolder(NAV_NODES[x][1]);
	theFolder=theFolder.substring(1,theFolder.length-1);
	if(theFolder.indexOf("careers")!=-1&&getPath(currentURL).indexOf("directory")!=-1){
		
		expandCurrentNode=true;
	}
	
	//draw main nodes
	if ((getPath(NAV_NODES[x][1]).indexOf("/home/")==-1)&&(currentURL.indexOf(getPath(NAV_NODES[x][1])) != -1 || expandCurrentNode == true)){
		document.write("<tr valign='top'><td colspan='2' class='nav_section_active' onMouseOver=this.id='over'; onMouseOut=this.id=''; onClick=document.location.href='" + NAV_NODES[x][1] + "';><a href='" + NAV_NODES[x][1] + "'>"+NAV_NODES[x][0]+"</a></td></tr>");
		expandCurrentNode = true;
	} else {
		if(getPath(NAV_NODES[x][1]).indexOf("/home/")!=-1){
			document.write("<tr valign='top'><td colspan='2' class='nav_section_home' onMouseOver=this.id='over'; onMouseOut=this.id=''; onClick=document.location.href='" + NAV_NODES[x][1] + "';><a href='" + NAV_NODES[x][1] + "'>"+NAV_NODES[x][0]+"</a></td></tr>");
		}else{
			document.write("<tr valign='top'><td colspan='2' class='nav_section' onMouseOver=this.id='over'; onMouseOut=this.id=''; onClick=document.location.href='" + NAV_NODES[x][1] + "';><a href='" + NAV_NODES[x][1] + "'>"+NAV_NODES[x][0]+"</a></td></tr>");
		}
	}

	//draw sub nodes
	if (NAV_NODES[x].length > 2 && expandCurrentNode == true) {
		for(y=2;y<NAV_NODES[x].length;y++) {
			if (NAV_NODES[x][y] == null) break;
			if (currentURL.indexOf(getPath(NAV_NODES[x][y][1])) != -1) {
				document.write("<tr valign='top'><td class='nav_subsection_arrow' id='active'>-</td><td class='nav_subsection' id='active'>" + NAV_NODES[x][y][0] + "</td></tr>");
				drawnSub=true;
			} else if(convertedURL.indexOf(getPath(NAV_NODES[x][y][1])) != -1) {
				document.write("<tr valign='top'><td class='nav_subsection_arrow'>-</td><td class='nav_subsection' id='active'>" + NAV_NODES[x][y][0] + "</td></tr>");
				drawnSub=true;
			} else {
				document.write("<tr valign='top'><td class='nav_subsection_arrow'>-</td><td class='nav_subsection'><a href='" + NAV_NODES[x][y][1] + "' class='nav_subsection'>" + NAV_NODES[x][y][0] + "</a></td></tr>");
				drawnSub=true;
			}
		}
		if(!drawnSub){
			document.write("<tr><td colspan='2' class='nav_spacer'>&nbsp;</td></tr>");
		}
	}
}

document.write("</table>");
}
