// 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 getFolderName(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 +1, startSlash - startSlash2 -1);
	}
}

var currentURL = getPath(location.href);
var expandCurrentNode = false;
var haveNodes = false;
var imgPath = "../../includes/site_images/nav/";
var subExist = false;

document.write("<table border='0' cellpadding='0' cellspacing='0' width='165'>");
document.write('<tr valign="top"><td><table cellspacing="0" cellpadding="0" border="0" width="165">');

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;
			}
		}
	}

	subExist = false;
	
	//draw main nodes
	if (currentURL.indexOf(getPath(NAV_NODES[x][1])) != -1 || expandCurrentNode == true) {
		document.write("<tr><td>");
		document.write("<a href='" + NAV_NODES[x][1] + "' onMouseOver=\"MM_swapImage('" + getFolderName(NAV_NODES[x][1]) + "','','" + imgPath + getFolderName(NAV_NODES[x][1]) + "_over.gif',0)\" onMouseOut=\"MM_swapImgRestore()\">");
		document.write("<img src='" + imgPath + getFolderName(NAV_NODES[x][1]) + "_over.gif' alt='" + NAV_NODES[x][0] + "' name='" + getFolderName(NAV_NODES[x][1]) + "' width='165' height='50' border='0'></a>");
		document.write("</td></tr>");
		expandCurrentNode = true;
	} else {
		document.write("<tr><td>");
		document.write("<a href='" + NAV_NODES[x][1] + "' onMouseOver=\"MM_swapImage('" + getFolderName(NAV_NODES[x][1]) + "','','" + imgPath + getFolderName(NAV_NODES[x][1]) + "_over.gif',0)\" onMouseOut=\"MM_swapImgRestore()\">");
		document.write("<img src='" + imgPath + getFolderName(NAV_NODES[x][1]) + ".gif' alt='" + NAV_NODES[x][0] + "' name='" + getFolderName(NAV_NODES[x][1]) + "' width='165' height='50' border='0'></a>");
		document.write("</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) {
				if (subExist == false) {
					document.write("<tr><td><img src='../../includes/site_images/spacers/spacer.gif' width='165' height='13' alt=''></td></tr>");	
				}
				document.write("<tr><td><table border='0' cellpadding='2' cellspacing='0' width='165'><tr valign='top'>");
				document.write("<td valign='top' align='right' width='22'><span class='nav_subsection_active'>&#8226;&nbsp;</span></td>");
				document.write("<td valign='top'><a href='" + NAV_NODES[x][y][1] + "' class='nav_subsection_active'>" + NAV_NODES[x][y][0] + "</a></td>");
				document.write("</tr></table></td></tr>");
				subExist = true;
			} else {
				if (subExist == false) {
					document.write("<tr><td><img src='../../includes/site_images/spacers/spacer.gif' width='165' height='13' alt=''></td></tr>");	
				}
				document.write("<tr><td><table border='0' cellpadding='2' cellspacing='0' width='165'><tr valign='top'>");
				document.write("<td valign='top' align='right' width='22'><span class='nav_subsection'>&#8226;&nbsp;</span></td>");
				document.write("<td valign='top'><a href='" + NAV_NODES[x][y][1] + "' class='nav_subsection'>" + NAV_NODES[x][y][0] + "</a></td>");
				document.write("</tr></table></td></tr>");
				subExist = true;
			}
		}
	}
	if (subExist == true) {
		document.write("<tr><td><img src='../../includes/site_images/spacers/spacer.gif' width='165' height='30' alt=''></td></tr>");	
	}
}
document.write("</table>");

function setPage(){
	if ((window.location.href).indexOf("tl=") != -1) { //show simple nav
		document.getElementById("image_nav").style.display="none";
		document.getElementById("alt_nav").style.display="";
		document.getElementById("translate_button").style.display="none";
		document.getElementById("english_button").style.display="";
	}
	if(this.location.href.indexOf("tl=")==-1&&top.location!=this.location){
		top.location.href=this.location.href;
	}
};

window.onload=setPage;
