// SideMenuScript.js
// last update: 091907
// by: Adveno
//
// this file contains the code to create the side menu


// web site url 

var URL_PREFIX = "http://www.state.nj.us/mvc";

// constant array indices meaningfull name
var TITLE 	= 0;
var LINK	= 1;
var ID		= 2;
var PARENT	= 3;

var D1 = level1Arr;
var D2 = level2Arr;
var D3 = level3Arr;

// id of 1st level item in the menu that should be extracted (or -1 if none)
var lev1 = -1;

// id of 2nd level item in the menu that should be extracted (or -1 if none)
var lev2 = -1;

// id of 3rd level item in the menu that should be extracted (or -1 if none)
var lev3 = -1;



 

// calculates the ver1,ver2 and ver3 according to pageId
function CalculateParents(id)
{


	// if this page is of a 1st depth - function will exit in the following loop
	for (i=0; i<D1[ID].length; i++)
	{
		if (D1[ID][i] == id)
		{
			lev2 = -1;
			lev3 = -1;
			lev1 = id;
			return;
		}
	}

	// if this page is of a 2nd depth - function will exit in the following loop
	for (i=0; i<D2[ID].length; i++)
	{
		if (D2[ID][i] == id)
		{
			lev3 = -1;
			lev2 = id;
			lev1 = D2[PARENT][i];			
			return;
		}
	}


	// if this page is of a 3rd depth - function will exit in the following loop
	for (i=0; i<D3[ID].length; i++)
	{
		if (D3[ID][i] == id)
		{
			lev3 = id;
			lev2 = D3[PARENT][i];
			for (j=0;j<D2[ID].length;j++)
			{
				if (D2[ID][j] == lev2)
				{
					lev1 = D2[PARENT][j];
				}
			}
		}
	}

}




// DrawSideMenu - creates the html for the menu by using the 3 arrays: D1,D2,D3
function DrawSideMenu()
{

	// menuHTML will contain the HTML for the side menu
	var menuHTML = "<ul>";
	
	
	// loop through all of the 1st depth items
	for (i=0; i < D1[TITLE].length; i++)
	{
		
			// assume this is a 1st depth not selected item
			divClass 	= "smDivDepth1";
			overHandler = "smD1Over(this)";
			outHandler 	= "smD1Out(this)";
			

			// or maybe this is the selected page and it is 1st depth
			if (pageId==D1[ID][i])
			{
					divClass 	= "smDivDepth1S";
					overHandler = "void(0)";
					outHandler 	= "void(0)";
			}
			
		
			// or maybe this is a 1st depth item and the currently selected item is one of its sons or grandsons
			if (lev1 == D1[ID][i] && pageId != D1[ID][i])
			{
					divClass 	= "smDivDepth1P";
			}
	
	
			// add the menu item to the html string
			menuHTML += "<li class='" +divClass+ "' onMouseOver='" +overHandler+
					"' onMouseOut='" +outHandler+ "' onClick=\"smClick('" +D1[LINK][i]+ "')\">"+
					D1[TITLE][i]+ "</li>";	

			// handle special case of first 1st depth item - dont draw top border
			if (i==0)
			{
				menuHTML = "<li class='" +divClass+ "' style='border-top:0px solid red' onMouseOver='" +overHandler+
					"' onMouseOut='" +outHandler+ "' onClick=\"smClick('" +D1[LINK][i]+ "')\">"+
					D1[TITLE][i]+ "</li>";	
			}

			// if this item is selected draw all its 2nd depth children
			if (lev1==D1[ID][i])
			for (j=0; j < D2[TITLE].length; j++)
			{		
					if (D2[PARENT][j] == D1[ID][i] )
					{
							// assume this is not the selected item 2nd depth
							divClass = "smDivDepth2";
							
							// if this is the selected item - 2nd depth
							if (pageId == D2[ID][j])
							{
									divClass 	= "smDivDepth2S";
							}
							
							// or maybe this is a 2nd depth item and the currently selected item is one of its sons
							if (lev2 == D2[ID][j] && pageId != D2[ID][j])
							{
								divClass	= "smDivDepth2P";
								}
							
							// add 2nd depth item to the HTML string
							menuHTML += "<li class='" +divClass+ 
									"' onMouseOver='smD2Over(this)' onMouseOut='smD2Out(this)' onClick=\"smClick('" +D2[LINK][j]+ 
									"')\">"+D2[TITLE][j]+ "</li>";				
							
							// if there are 3rd depth children
							if (lev2==D2[ID][j])
							for (k=0; k<D3[TITLE].length; k++)
							{
									if (D3[PARENT][k] == D2[ID][j] )
									{
											// assume not selected item 3rd depth
											divClass = "smDivDepth3";
											overHandler = "smD3Over(this)";
											outHandler 	= "smD3Out(this)";
											
											// if this is a 3rd depth selected item
											if (pageId==D3[ID][k])
											{
													divClass 	= "smDivDepth3S";
													overHandler = "void(0)";
													outHandler 	= "void(0)";
										}
											
											// add 3rd depth item to the HTML string
											menuHTML += "<li class='"+divClass+
													"' onMouseOver='"+overHandler+"' onMouseOut='"+outHandler+"' onClick=\"smClick('" +
													D3[LINK][k]+ "')\">"  + " " +D3[TITLE][k]+ "</li>";										
									}			
							}
					}
			}
	}
	
	menuHTML += "<li class='last_menu_item'></li></ul>";
	// write side menu to the page
	document.write(menuHTML);
}




// style changeing scripts

// handle mouse over top level menu item
function smD1Over(obj){	obj.style.textDecoration='underline';obj.style.color='#215994';}

// handle mouse out top level menu item
function smD1Out(obj){	obj.style.textDecoration='none';;obj.style.color='#5E5E5E';}


// handle mouse over 2nd level menu item
function smD2Over (obj){	obj.style.textDecoration='underline';obj.style.color='#215994';}

// handle mouse out 2nd level menu item
function smD2Out(obj){	obj.style.textDecoration='none';obj.style.color='#5E5E5E';}

// handle mouse over 3rd level menu item
function smD3Over (obj){	obj.style.textDecoration='underline';obj.style.color='#215994';}

// handle mouse out 3rd level menu item
function smD3Out(obj){	obj.style.textDecoration='none';obj.style.color='#5E5E5E';}


// handle mouse over BC
function BCOver (obj){	obj.style.textDecoration='underline';obj.style.color='#0068BF';}

// handle mouse out BC
function BCOut(obj){	obj.style.textDecoration='none';obj.style.color='#666666';}



// handle click on top level menu item
// if this is an absolute URL - don't add prefix
function smClick(url){
	if (url.indexOf("http://") == 0)
	{
		document.location=url;
	}
	else
	{
		document.location = URL_PREFIX + url;
	}
}




// writes breadcrumbs to the page
function writeBreadCrumbs(sectionCode, pageId)
{
	CalculateParents(pageId);

	// bcHTML is the breadcrumbs HTML string 
	bcHTML = "<ul><li class=\"first\" onClick=\"smClick('/index.htm')\" onMouseOver='BCOver(this)' onMouseOut='BCOut(this)'>Home</li>";
	
	// add the index breadcrumb according to the sectionCode parameter (passed from the 
	if (sectionCode == "VE") {bcHTML+="<li onClick=\"smClick('/Vehicle/index.htm')\"  onMouseOver='BCOver(this)' onMouseOut='BCOut(this)'>Vehicle</li>";}
	if (sectionCode == "CO") {bcHTML+="<li onClick=\"smClick('/Commercial/index.htm')\"  onMouseOver='BCOver(this)' onMouseOut='BCOut(this)'>Commercial</li>";}
	if (sectionCode == "IN") {bcHTML+="<li onClick=\"smClick('/Inspections/index.htm')\"  onMouseOver='BCOver(this)' onMouseOut='BCOut(this)'>Inspections</li>";}
	if (sectionCode == "LI") {bcHTML+="<li onClick=\"smClick('/Licenses/index.htm')\"  onMouseOver='BCOver(this)' onMouseOut='BCOut(this)'>Licenses</li>";}
	if (sectionCode == "AB") {bcHTML+="<li onClick=\"smClick('/About/index.htm')\"  onMouseOver='BCOver(this)' onMouseOut='BCOut(this)'>About</li>";}
	if (sectionCode == "VI") {bcHTML+="<li onClick=\"smClick('/Violations/index.htm')\"  onMouseOver='BCOver(this)' onMouseOut='BCOut(this)'>Violations</li>";}
	if (sectionCode == "LO") {bcHTML+="<li onClick=\"smClick('/Location/index.htm')\"  onMouseOver='BCOver(this)' onMouseOut='BCOut(this)'>Locations</li>";}
	// since this is at least 2nd depth item - display the relevant 1st depth item (parent or grandparent)
	if (lev2 != -1) 
	{
		for(i=0; i < D1[TITLE].length; i++)
		{
			if (D1[ID][i] == lev1)
			{
				bcHTML+="<li onClick=\"smClick('"+D1[LINK][i]+"')\"  onMouseOver='BCOver(this)' onMouseOut='BCOut(this)'>"+D1[TITLE][i]+"</li>"
			}
		}
	}
	// since this is a 3rd depth item display the relevant 2nd depth item (parent)
	if (lev3 != -1) 
	{
		for(i=0; i < D2[TITLE].length; i++)
		{
			if (D2[ID][i] == lev2)
			{
				bcHTML+="<li onClick=\"smClick('"+D2[LINK][i]+"')\"  onMouseOver='BCOver(this)' onMouseOut='BCOut(this)'>"+D2[TITLE][i]+"</li>"
			}
		}	}
	
	
	// add current page
	var title="";
	if (lev3 != -1)
	{
		for (i=0;i<D3[ID].length;i++) if (D3[ID][i] == lev3 ) title=D3[TITLE][i];
	}
	else if (lev2 != -1)
	{
		for (i=0;i<D2[ID].length;i++) if (D2[ID][i] == lev2 ) title=D2[TITLE][i];
	}
	else
	{
		for (i=0;i<D1[ID].length;i++) if (D1[ID][i] == lev1 ) title=D1[TITLE][i];
	}
	
	
	bcHTML+="<li >"+title+"</li></ul>";
	
	// draw breadcrumbs HTML to page
	document.write(bcHTML);

}