// 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);
	}
}

var currentURL = getPath(location.href);
var expandCurrentNode = false;
var haveNodes = false;

document.write("<table width='211' border='0' cellspacing='0' cellpadding='0'>");
var imageNav='<tr><td id="image_nav"><table cellspacing="0" cellpadding="0" border="0" width="211">';
var textNav='<tr><td id="nav_content" style="display:none;"><table cellspacing="0" cellpadding="0" border="0" width="211">';

for(x=0;x<NAV_NODES.length;x++) {
	expandCurrentNode = false;
	haveNodes = false;
	var 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]);
	var activeNav=false;
	theFolder=theFolder.substr(1,theFolder.length-2);
	if (theFolder.indexOf("home")==-1){
		imageNav=imageNav+"<tr><td class='nav_divider'><img src='../../includes/site_images/spacers/spacer.gif' width='211' height='1' alt='' border=''></td></tr>";
		textNav=textNav+"<tr><td class='nav_divider'><img src='../../includes/site_images/spacers/spacer.gif' width='211' height='1' alt='' border=''></td></tr>";
	}
	if ((currentURL.indexOf("/home/")==-1)&&(currentURL.indexOf(getPath(NAV_NODES[x][1])) != -1 || expandCurrentNode == true)){
		imageNav=imageNav+"<tr valign='top'><td><a onMouseOver=swapImage('"+theFolder+"','"+theFolder+"_3.gif',0) onMouseOut=restoreImage('"+theFolder+"') href="+NAV_NODES[x][1]+"><img src='../../includes/site_images/nav/"+theFolder+"_2.gif' alt='"+ NAV_NODES[x][0] + "' border='0' name='"+theFolder+"' id='"+theFolder+"'></a></td></tr>";
		preLoadImages("../../includes/site_images/nav/"+theFolder+"_3.gif");
		textNav=textNav+"<tr valign='top'><td class='nav_section_active'><a href='"+NAV_NODES[x][1]+"'>"+ NAV_NODES[x][0] + "</a></td></tr>";
		expandCurrentNode = true;
		activeNav=true;
	} else {
		imageNav=imageNav+"<tr valign='top'><td><a onMouseOver=swapImage('"+theFolder+"','"+theFolder+"_1.gif',0) onMouseOut=restoreImage('"+theFolder+"') href="+NAV_NODES[x][1]+"><img src='../../includes/site_images/nav/"+theFolder+"_0.gif' alt='"+ NAV_NODES[x][0] + "' border='0' name='"+theFolder+"' id='"+theFolder+"'></a></td></tr>";
		preLoadImages("../../includes/site_images/nav/"+theFolder+"_1.gif");
		textNav=textNav+"<tr valign='top'><td class='nav_section'><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) {
				imageNav=imageNav+"<tr valign='top'><td class='nav_subsection_active'>" + NAV_NODES[x][y][0] + "</td></tr>";
				textNav=textNav+"<tr valign='top'><td class='nav_subsection_active'>" + NAV_NODES[x][y][0] + "</td></tr>";
				drawnSub=true;
			} else {
				imageNav=imageNav+"<tr valign='top'><td class='nav_subsection'><a href='" + NAV_NODES[x][y][1] + "'>" + NAV_NODES[x][y][0] + "</a></td></tr>";
				textNav=textNav+"<tr valign='top'><td class='nav_subsection'><a href='" + NAV_NODES[x][y][1] + "'>" + NAV_NODES[x][y][0] + "</a></td></tr>";
				drawnSub=true;
			}
		}
	}
	if (drawnSub){
		imageNav=imageNav+"<tr><td class='subnav_divider'><img src='../../includes/site_images/spacers/spacer.gif' width='211' height='5' alt='' border=''></td></tr>";
		textNav=textNav+"<tr><td class='subnav_divider'><img src='../../includes/site_images/spacers/spacer.gif' width='211' height='5' alt='' border=''></td></tr>";
	}
}
imageNav=imageNav+'</table></td></tr>';
textNav=textNav+'</table></td></tr>';
document.write(imageNav);
document.write(textNav);
document.write("</table>");



