// set up the navigation array, and varibales used in populating it

arItems = new Array();
var items1 = 1;
var items2 = 3;
var items3 = 3;
var items4 = 3;

//fucntions used to populate the navigation array

function add1stlevelitem(name,url,imageno) {
arItems[items1] = new Array(name,url,imageno);
items2 = 3;
items3 = 3;
items4 = 3;
items1++;
}

function add2ndlevelitem(name,url,imageno) {
arItems[items1-1][items2] = new Array(name,url,imageno);
items3 = 3;
items4 = 3;
items2++;
}


function add3rdlevelitem(name,url,imageno) {
arItems[items1-1][items2-1][items3] = new Array(name,url,imageno);
items4 = 3;
items3++;
}
function add4thlevelitem(name,url,imageno) {
arItems[items1-1][items2-1][items3-1][items4] = new Array(name,url,imageno);
items4++;
}

// Variables used for highlighting, toggling folders
var currhighlighted = "0";
var changenavcode = "";

//Functions to draw the navigation

function starttable(tablewidth){

	document.write("<table class='navtable' width='" + tablewidth + "' border='0' cellpadding='0' cellspacing='0'>");
	document.write("<tr>")
	document.write("<td><img  src='invisible.gif' width='18' height='1'></td>");
	document.write("<td><img  src='invisible.gif' width='18' height='1'></td>");
	document.write("<td><img  src='invisible.gif' width='" + (tablewidth - 36) + "' height='1'></td></tr>")
}


function buildnav(strchangenav){

	
	//document.write("<textarea rows='8' cols='30'>")
	starttable(intwidth);
	
	//The first 1stlevel item gets to be the homepage
	document.write("<tr>");
	document.write("<td valign='top'><a href='");
	document.write(arItems[1][1] + "'");
	if (strTarget != "") {
		document.write(" target='" + strTarget + "'");
	}
	document.write(" onClick='highlight(\"0\");'>");
	document.write("<img  src='" + imagepath + arImages[arItems[1][2]] + "' width='18' height='18' alt='' border='0' id='img0'></a></td>");
	document.write("<td valign='top' colspan='2'>");
	document.write("<a href='");
	document.write(arItems[1][1]);
	document.write("'");
	if (strTarget != "") {
		document.write(" target='" + strTarget + "'");
	}
	document.write(" onClick='highlight(\"0\");' class='navselected' id='link0'>");
	document.write(arItems[1][0]);
	document.write("</a></td>");
	document.write("</tr>");
	
	for(i=2; i<arItems.length; i++) {
			if (i == arItems.length-1) {
				var levelend = 1;	
			} else {
				var levelend = 0;
			}		
		builditem(arItems[i],(intwidth - 18),levelend,i, strchangenav);
	}
	document.write("</table>");
	//document.write("</textarea>")
	
	if (changenavcode != "") {
	 changenavcode = changenavcode + ""
		openfolder(changenavcode)
	}	
	
}

function builditem  (arSubItem,tablewidth,endlevel,levelid, strchangenav) 
{
	
	// start row
	document.write("<tr>");
	
	//control cell
	document.write("<td valign='top' ");
	
	// Decide which background image to use for control icon
	if(endlevel == 0)	{
		document.write(" background='"  + imagepath +"i-line.gif' >");
	} else {
		document.write(">");
	}				
		
	//Decide which control icon to use
	if (arSubItem.length > 3) {
		document.write("<a href='javascript:toggle(\"" + levelid + "\");'>");
		document.write("<img  src='"   + imagepath + "plus-line.gif' width='18' height='18' alt='' border='0' "); 	
		document.write("name='control" + levelid + "'>");
		document.write("</a>");
	} else { 
	document.write("<img src='"  + imagepath + "l-line.gif' width='18' height='18' alt='' border='0'>");
	}				
	
	document.write("</td>");			

	//folder cell
	document.write("<td valign='top' id='cell" + levelid + "'>");	
		
	//Decide which image to use
	document.write("<a");
	if (strTarget != "") {
		document.write(" target='" + strTarget + "'");
	}
	document.write(" onClick='highlight(\"");
	document.write(levelid);
	document.write("\");' href='");
	document.write(arSubItem[1]);
	document.write("');\")'>");
	document.write("<img  src='"  + imagepath);
	document.write(arImages[arSubItem[2]]);
	document.write("' width='18' height='18' alt='' border='0' ");	
	document.write(" name='img" + levelid + "'>");
	document.write("</a>");	
	document.write("</td>");			
	
	
	//link/text cell
	document.write("<td valign='top'>");		
	document.write("<div class='navdiv'>");				
	document.write("<a");
	if (strTarget != "") {
		document.write(" target='" + strTarget + "'");
	}
	document.write(" onClick='highlight(\"");
	document.write(levelid);
	document.write("\");'  href='");	
	document.write(arSubItem[1]);
	document.write("' class='");
	document.write("navunselected");
	document.write("' id='link");
	document.write(levelid);
	document.write("'>");		
	document.write(arSubItem[0]);
	//document.write(arSubItem[2]);
	document.write("</a></div></td>");	

	// end row
	document.write("</tr>");
	
	//Is this our selected navigation item?
	if (arSubItem[1] == strchangenav) {
		changenavcode =  levelid
	}
	
	//check for subitems and displauy them
	
	if (arSubItem.length > 3){
		
		//start row
		document.write("<tr id='div" + levelid + "' style='display : none'>")
		
		// cell containing line
		document.write("<td")
		if(endlevel == 0)	{
			document.write(" background='"  + imagepath + "i-line.gif' >");
		} else {
			document.write(">");
		}			
		
		//cell containing table
		document.write("<td colspan='2'>")
		starttable(tablewidth);

		var newtablewidth = tablewidth - 18
		
		//call recursive function
		for(var itemcount=3; itemcount<arSubItem.length; itemcount++) {	
			var newlevelid = levelid + "-" + itemcount	
			if (itemcount == arSubItem.length-1) {
				var newlevelend = 1;	
			} else {
				var newlevelend = 0;
			}	
			
			builditem(arSubItem[itemcount],newtablewidth,newlevelend,newlevelid, strchangenav);				
		}
		
		// close table, cell, row
		
		document.write("</table></td>")
		document.write("</tr>")
	}
	
	return;
	
}
function toggle(navitem) {

	strdiv = "div" + navitem
	strcell = "cell" + navitem	
	strcontrol = "control" + navitem	
	
	if (document.getElementById(strdiv).style.display == "none") {
		document.getElementById(strdiv).style.display = "";
		document.getElementById(strcell).style.backgroundImage = "url('"  + imagepath + "i-line-long.gif')";
		document.images[strcontrol].src =  imagepath +"minus-line.gif";	
	} else {
		document.getElementById(strdiv).style.display = "none";
		document.getElementById(strcell).style.backgroundImage = "";
		document.images[strcontrol].src =   imagepath + "plus-line.gif";	
	}
}


function highlight(navitem) {
	strnewimg = "img" + navitem;
	stroldimg = "img" + currhighlighted;
	strnewlink = "link" + navitem;
	stroldlink = "link" + currhighlighted;
	strdiv = "div" + navitem
	strcell = "cell" + navitem	
	strcontrol = "control" + navitem			
	 
	if (strnewimg != stroldimg) {
		document.getElementById(strnewlink).className = "navselected";
		document.getElementById(stroldlink).className = "navunselected";
		
		if (document.images[strnewimg].src == imagepath + imgclosefoldersrc.src) {
			document.images[strnewimg].src = imagepath + imgopenfoldersrc.src;	
		}
		if (document.images[stroldimg].src == imagepath + imgopenfoldersrc.src) {
			document.images[stroldimg].src = imagepath + imgclosefoldersrc.src;
		}	
	}

	if (document.getElementById(strdiv)) {
		document.getElementById(strdiv).style.display = "";
		document.getElementById(strcell).style.backgroundImage = "url('" + imagepath + "i-line-long.gif')";
		document.images[strcontrol].src = imagepath + "minus-line.gif";			
	}	
	currhighlighted = navitem;
}

//function to expand all the parents of an item and highlight it 

function openfolder(folderid) {

	var splitArray = folderid.split('-');
	var strTemp
	
	for (var i=1; i<splitArray.length; i++) {
		strTemp = splitArray[0]
		for (var x=1; x<i; x++) {
	    	strTemp = strTemp + "-" + splitArray[x];
		}
		strdiv = "div" + strTemp
		strimg = "img" + strTemp
		strcell = "cell" + strTemp	
		strcontrol = "control" + strTemp			
		if (document.getElementById(strdiv)) {
			document.getElementById(strdiv).style.display = "";
			document.getElementById(strcell).style.backgroundImage = "url(" +imagepath + "i-line-long.gif)";
			document.images[strcontrol].src = imagepath + "minus-line.gif";			
		}
	}
	highlight(folderid);
}


function donothing() {

	// just like it says on the tin.

}
