// 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;
var wasActiveNode=true;

// Array containing references to the images for the HPD site nav
var NAV_IMAGES=[
['../../includes/site_images/nav/home_0.gif','../../includes/site_images/nav/home_1.gif','home','../../includes/site_images/nav/home_2.gif'],
['../../includes/site_images/nav/site_index_0.gif','../../includes/site_images/nav/site_index_1.gif','site_index','../../includes/site_images/nav/site_index_2.gif'],
['../../includes/site_images/nav/about_0.gif','../../includes/site_images/nav/about_1.gif','about','../../includes/site_images/nav/about_2.gif'],
['../../includes/site_images/nav/language_0.gif','../../includes/site_images/nav/language_1.gif','languageaccess','../../includes/site_images/nav/language_2.gif'],
['../../includes/site_images/nav/apartment_0.gif','../../includes/site_images/nav/apartment_1.gif','apartment','../../includes/site_images/nav/apartment_2.gif'],
['../../includes/site_images/nav/architects_0.gif','../../includes/site_images/nav/architects_1.gif','architects','../../includes/site_images/nav/architects_2.gif'],
['../../includes/site_images/nav/developers_0.gif','../../includes/site_images/nav/developers_1.gif','developers','../../includes/site_images/nav/developers_2.gif'],
['../../includes/site_images/nav/buyers_0.gif','../../includes/site_images/nav/buyers_1.gif','buyers','../../includes/site_images/nav/buyers_2.gif'],
['../../includes/site_images/nav/homeowners_0.gif','../../includes/site_images/nav/homeowners_1.gif','homeowners','../../includes/site_images/nav/homeowners_2.gif'],
['../../includes/site_images/nav/courses_0.gif','../../includes/site_images/nav/courses_1.gif','courses','../../includes/site_images/nav/courses_2.gif'],
['../../includes/site_images/nav/jobseekers_0.gif','../../includes/site_images/nav/jobseekers_1.gif','jobseekers','../../includes/site_images/nav/jobseekers_2.gif'],
['../../includes/site_images/nav/pr_0.gif','../../includes/site_images/nav/pr_1.gif','pr','../../includes/site_images/nav/pr_2.gif'],
['../../includes/site_images/nav/owners_0.gif','../../includes/site_images/nav/owners_1.gif','owners','../../includes/site_images/nav/owners_2.gif'],
['../../includes/site_images/nav/tenants_0.gif','../../includes/site_images/nav/tenants_1.gif','tenants','../../includes/site_images/nav/tenants_2.gif'],
['../../includes/site_images/nav/section8_0.gif','../../includes/site_images/nav/section8_1.gif','section8','../../includes/site_images/nav/section8_2.gif'],
['../../includes/site_images/nav/vendors_0.gif','../../includes/site_images/nav/vendors_1.gif','vendors','../../includes/site_images/nav/vendors_2.gif'],
['../../includes/site_images/nav/contact_0.gif','../../includes/site_images/nav/contact_1.gif','contact','../../includes/site_images/nav/contact_2.gif']];

document.write("<table width='100%' border='0' cellspacing='0' cellpadding='0' bgcolor='#58729E' height='100%'>");

for(x=0;x<NAV_NODES.length;x++) {
	expandCurrentNode = false;
	haveNodes = false;
	drawDots=true;

	//check for sub nodes
	if (NAV_NODES[x] == null||NAV_IMAGES[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;
			}
		}
	}

	//draw main nodes
	if(wasActiveNode){
		drawDots=false;
	}
	theFolder=getFolder(currentURL).substring(1,3);
	thePathFolder=getFolder(NAV_NODES[x][1]).substring(1,3);
	if (((currentURL.indexOf(getPath(NAV_NODES[x][1])) != -1 || expandCurrentNode == true)&&(x>0))||(theFolder=="pr"&&thePathFolder=="pr")) {
		document.write("<tr valign='top'><td colspan='2'><img src='../../includes/site_images/nav/nav_divider2.gif' height='5' width='177'></td></tr>");
		document.write("<tr valign='top'><td colspan='2'><a href='" + NAV_NODES[x][1] + "' onMouseOut='MM_swapImgRestore()' onMouseOver='MM_swapImage("+getSwapImageText(x,3)+")'><img src='"+NAV_IMAGES[x][1]+"' alt='"+NAV_NODES[x][0]+"' name='"+NAV_IMAGES[x][2]+"' border='0'></a></td></tr>");
		expandCurrentNode = true;
		wasActiveNode=true;
		if(NAV_NODES[x].length<=2){
			document.write("<tr valign='top'><td colspan='2'><img src='../../includes/site_images/nav/nav_divider3.gif' height='7' width='177'></td></tr>");
		}
	} else {
		if(drawDots){
			document.write("<tr valign='top'><td colspan='2'><img src='../../includes/site_images/nav/nav_divider.gif' width='177' height='5'></td></tr>");
		}
		document.write("<tr valign='top'><td colspan='2'><a href='" + NAV_NODES[x][1] + "' onMouseOut='MM_swapImgRestore()' onMouseOver='MM_swapImage("+getSwapImageText(x,1)+")'><img src='"+NAV_IMAGES[x][0]+"' alt='"+NAV_NODES[x][0]+"' name='"+NAV_IMAGES[x][2]+"' border='0'></a></td></tr>");
		wasActiveNode=false;
		if (NAV_NODES[x+1]==null){
			document.write("<tr valign='top'><td colspan='2'><img src='../../includes/site_images/nav/nav_divider.gif' width='177' height='5'></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){
				if(x>0){
					if(y<=2){
						document.write("<tr valign='top'><td colspan='2'><img src='../../includes/site_images/nav/nav_divider4.gif' height='2' width='177'></td></tr>");
					}else{
						document.write("<tr valign='top'><td colspan='2'><img src='../../includes/site_images/nav/nav_divider3.gif' height='7' width='177'></td></tr>");
					}
				}
			 break;
			 }
			if (currentURL.indexOf(getPath(NAV_NODES[x][y][1])) != -1) {
				document.write("<tr valign='top' bgcolor='#FFFFFF'><td width='20'><img src='../../includes/site_images/spacers/spacer.gif' height='10' width='10'><span class='nav_subsection'>-&nbsp;</span></td>");
				document.write("<td width='157'><a href='" + NAV_NODES[x][y][1] + "' class='nav_subsection_active'>" + NAV_NODES[x][y][0] + "</a></td></tr>");
			} else {
				document.write("<tr valign='top' bgcolor='#FFFFFF'><td width='20'><img src='../../includes/site_images/spacers/spacer.gif' height='10' width='10'><span class='nav_subsection'>-&nbsp;</span></td>");
				document.write("<td width='157'><a href='" + NAV_NODES[x][y][1] + "' class='nav_subsection'>" + NAV_NODES[x][y][0] + "</a></td></tr>");
			}
		}
	}
}

document.write("</table>");

function getSwapImageText(x,y){
	text='"'+NAV_IMAGES[x][2]+'","","'+NAV_IMAGES[x][y]+'",1';
	return(text);
}

// The following DW JS is used for imageover effects


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
