// 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 getAnchor(thePath) {
	var urlArray=thePath.split("#");
	return(urlArray[1]);
}

function clearActive(theLink){
	var linksArray=document.getElementsByTagName("a");
	for(var i=0;i<linksArray.length;i++){
		if(linksArray[i].className=="nav_subsection_active"){
			linksArray[i].className="nav_subsection";
		}
	}
	if(theLink){
		theLink.className="nav_subsection_active";
	}
}

var currentURL = getPath(location.href);
var expandCurrentNode = false;
var haveNodes = false;

document.write("<table width='100%' border='0' cellspacing='0' cellpadding='0'>");
document.write("<tr valign='top'><td><img src='../../includes/site_images/spacers/spacer.gif' height='25' width='150' alt='' border='0'></td></tr>");

for(x=0;x<NAV_NODES.length;x++) {
	expandCurrentNode = false;
	haveNodes = 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);

	//draw main nodes
	if (currentURL.indexOf(getPath(NAV_NODES[x][1])) != -1 || expandCurrentNode == true) {
		if(currentURL.indexOf("home")!=-1){
			MM_preloadImages("../../includes/site_images/nav/"+theFolder+"_1.gif");
			document.write("<tr valign='top'><td align='right'><a href='" + NAV_NODES[x][1] + "' onmouseout='MM_swapImgRestore()' onmouseover='MM_swapImage(\""+theFolder+"\",\"\",\"../../includes/site_images/nav/"+theFolder+"_1.gif\",1)'><img src='../../includes/site_images/nav/"+theFolder+"_0.gif' width='150' alt='"+NAV_NODES[x][0]+"' name='"+theFolder+"' border='0'></a></td></tr>");
		}else{
			MM_preloadImages("../../includes/site_images/nav/"+theFolder+"_2.gif");
			document.write("<tr valign='top'><td align='right'><a href='" + NAV_NODES[x][1] + "' onmouseout='MM_swapImgRestore()' onmouseover='MM_swapImage(\""+theFolder+"\",\"\",\"../../includes/site_images/nav/"+theFolder+"_2.gif\",1)'><img src='../../includes/site_images/nav/"+theFolder+"_1.gif' width='150' alt='"+NAV_NODES[x][0]+"' name='"+theFolder+"' border='0'></a></td></tr>");
		}
		expandCurrentNode = true;
	} else {
		if(currentURL.indexOf("home")!=-1){
			MM_preloadImages("../../includes/site_images/nav/"+theFolder+"_1.gif");
			document.write("<tr valign='top'><td align='right'><a href='" + NAV_NODES[x][1] + "' onmouseout='MM_swapImgRestore()' onmouseover='MM_swapImage(\""+theFolder+"\",\"\",\"../../includes/site_images/nav/"+theFolder+"_1.gif\",1)'><img src='../../includes/site_images/nav/"+theFolder+"_0.gif' width='150' alt='"+NAV_NODES[x][0]+"' name='"+theFolder+"' border='0'></a></td></tr>");
		}else{
			MM_preloadImages("../../includes/site_images/nav/"+theFolder+"_2.gif");
			document.write("<tr valign='top'><td align='right'><a href='" + NAV_NODES[x][1] + "' onmouseout='MM_swapImgRestore()' onmouseover='MM_swapImage(\""+theFolder+"\",\"\",\"../../includes/site_images/nav/"+theFolder+"_2.gif\",1)'><img src='../../includes/site_images/nav/"+theFolder+"_0.gif' width='150' alt='"+NAV_NODES[x][0]+"' name='"+theFolder+"' border='0'></a></td></tr>");
		}
	}

	//draw sub nodes
	if (NAV_NODES[x].length > 2 && expandCurrentNode == true) {
		if (NAV_NODES[x][2] != null){
			document.write("<tr valign='top'><td><img src='../../includes/site_images/spacers/spacer.gif' height='3' width='150' alt='' border='0'></td></tr>");
		}
		for(y=2;y<NAV_NODES[x].length;y++) {
			if (NAV_NODES[x][y] == null) break;
			if(theFolder=="conditions"||theFolder=="requirements"||theFolder=="structure"){
				if(getAnchor(currentURL)==getAnchor(NAV_NODES[x][y][1])){
					document.write("<td width='100%' align='right'><a href='" + NAV_NODES[x][y][1] + "' class='nav_subsection_active'>" + NAV_NODES[x][y][0] + "</a></td></tr>");
				}else{
					document.write("<td width='100%' align='right'><a href='" + NAV_NODES[x][y][1] + "' class='nav_subsection' onclick=clearActive(this)>" + NAV_NODES[x][y][0] + "</a></td></tr>");
				}
			}else{
				if (currentURL.indexOf(getPath(NAV_NODES[x][y][1])) != -1) {
					document.write("<td width='100%' align='right'><a href='" + NAV_NODES[x][y][1] + "' class='nav_subsection_active'>" + NAV_NODES[x][y][0] + "</a></td></tr>");
				} else {
					document.write("<td width='100%' align='right'><a href='" + NAV_NODES[x][y][1] + "' class='nav_subsection'>" + NAV_NODES[x][y][0] + "</a></td></tr>");
				}
			}
			document.write("<tr valign='top'><td><img src='../../includes/site_images/spacers/spacer.gif' height='10' width='150' alt='' border='0'></td></tr>");
		}
		document.write("<tr valign='top'><td><img src='../../includes/site_images/spacers/spacer.gif' height='1' width='150' alt='' border='0'></td></tr>");
	}
	document.write("<tr><td width='100%'><tr valign='top'><td><img src='../../includes/site_images/nav/nav_divider.gif' height='1' width='150' alt='' border='0'></td></tr></td></tr>");
}

document.write("</table>");