// table surround elements - common
tsc1 = "<table cellpadding='0' cellspacing='0' border='0'>";
tsc2 = "<tr>";
tsc3 = "<td valign='top'>";
tsc3a = "<td width='";
tsc3b = "'>";
tsc4 = "</td>";
tsc5 = "</tr>";
tsc6 = "</table>";


// first level div surround - with children
pdiv1 = "<div id='";
pdiv2 = "' onMouseOver=tst(";
pdiv3 = ",";
pdiv4 = ") onMouseOut=hideDiv(";
pdiv5 = ") class='";
pdiv6 = "' style='width:" + nwd + ";'>";


// menu div surround
mdiv1 = "<div id='";
mdiv2 = "' style='position:absolute; top:0px; left:0px; visibility:hidden; width:" + mwd + ";'>";


// menu item no child
mnc1 = "<div id='";
mnc2 = "' style='width:" + mwd + "px;'><table onMouseOver='chCol(this)' onMouseOut='rsCol(this)' cellpadding='0' cellspacing='0' width='" + mwd + "' border='0' class='";
mnc3 = "'><tr><td class='mnc' onClick=goLoc2('";
mnc4 = "')>";
mnc5 = "</td><td align='right' class='mnc'><img src='assets/t.gif' alt=' ' width='" + mmw + "' height='" + mmh + "' border='0' vspace='" + mmv + "'></td></tr></table></div>";


// menu item with child
mwc1 = "<div id='";
mwc2 = "' onMouseOver=tst2('";
mwc3 = "','";
mwc4 = "') onMouseOut=hideDiv('";
mwc5 = "') style='width:" + mwd + "px;'><table onMouseOver='chCol(this)' onMouseOut='rsCol(this)' cellpadding='0' cellspacing='0' width='";
mwc6 = "' border='0' class='";
mwc7 = "'><tr><td class='mwc'>";
mwc8 = "</td><td align='right' class='mwc'><img src='";
mwc9 = "' alt='more' width=";
mwc10 = " height=";
mwc11 = " border='0' vspace='";
mwc12 = "'></td></tr></table>";


// end div
ediv = "</div>";




// MAIN BUILD FUNCTION
function buildIt(nb,rid)
{
// initialise text selected item variable
var selButt = "0";

// write classes
doClass();

// set selected image
if(rid!="0")
	{
	rida = eval("mnavArray" + rid);
	rida[1] = rida[2];
	}

// write outer table start
document.write(tsc1);

// is it horizontal? yes - write row
if(hor)
	{
	document.write(tsc2);
	}


// create each menu item
for(i=0;i<nb;i++)
	{
	gg = i+1;

// set array id = idd
	idd = eval("mnavArray" + gg);

	if(!hor)
		{
		document.write(tsc2);		// if not horizontal write start row
		}


// write start cell



		document.write(tsc3a);
		document.write(nwd);
		document.write(tsc3b);

// write outer div
// does it have children?
		if(idd[7]!="0")
			{
			document.write(pdiv1);
			document.write("dv" + gg);
			document.write(pdiv2);
			document.write("'dv" + gg + "'");
			document.write(pdiv3);
			document.write("'tab" + gg + "'");
			document.write(pdiv4);
			document.write("'tab" + gg + "'");
			document.write(pdiv5);
			document.write("mnu1");
			document.write(pdiv6);
			}

		else
			{
			document.write(pdiv1);
			document.write("'dv" + gg + "' onClick='goLoc(" + gg + ")'");
			document.write(" class='");
			document.write("mnu2");
			document.write(pdiv6);
			}

// is it text?
		if(idd[1]=="")
			{
// it's text
			zzxy = "mi"+gg;

// find which text nav is selected and write / don't-write colour change calls
			if(gg==rid)
				{
				var selButt = gg;
				document.write("<div id='" + zzxy + "' style='width:" + nwd + "px; height:" + nwh + "px;'>");
				}

			else
				{
				document.write("<div id='" + zzxy + "' onMouseOver=chCol2(this) onMouseOut=rsCol2(this) style='width:" + nwd + "px; height:" + nwh + "px;'>");
				}

			document.write(idd[0]);
			document.write("</div>");
			}

		else
			{
// it's a graphic
			document.write("<img src='");
			document.write(idd[1]);
			document.write("' width='");
			document.write(idd[3]);
			document.write("' height='");
			document.write(idd[4]);
			document.write("' border='0' alt='");
			document.write(idd[5]);
			document.write("' onMouseOver=chImg(this," + gg + ") onMouseOut=rsImg(this," + gg + ")>");
			}

// do children
		if(idd[7]!="0")
			{
			mp = (i+1);
			doChild(mp)
			}

// ===================================================


// write end outer div
		document.write(ediv);

// write end cell
		document.write(tsc4);

	if(!hor)
		{
		document.write(tsc5);		// if not horizontal write end row
		}


// pdm specific bit - writes nav divider
	document.write("<tr><td valign='top'><img src='assets/pdm-mb-divider-green.gif' alt='PDM Data Limited' width=131 height=3 border=0 vspace='2'></td></tr>");
	}

// write outer table end

// is it horizontal? yes - write close row
if(hor)
	{
	document.write(tsc5);
	}



document.write(tsc6);

// sets selected text nav button colours
if(selButt>"0")
	{
	sti = ("mi"+selButt);
	document.getElementById(sti).style.backgroundColor = mmoc;
	document.getElementById(sti).style.olor = mmfc;
	}
}





// builds each individual menu
function doChild(mp)
{
prevlvl = "0";
thislvl = "0";
nextlvl = "0";
id1 = "0";
id2 = "0";

idd = eval("mnavArray" + mp);
arLen = idd.length;

// write menu surround
document.write(mdiv1);
document.write("tab" + mp);
document.write(mdiv2);

for(x=8;x<arLen;x++)
	{
	++gg;				// sub id counter

// find prevlvl
	if(x-1 == "7")
		{
		prevlvl = "0";
		}

	else
		{
		var tmpArray1 = idd[x-1].split(",");
		prevlvl = tmpArray1[2];
		}

// find thislvl
	var tmpArray = idd[x].split(",");
	var thislvl = tmpArray[2];

// find nextlvl
	if(x+1 != arLen)
		{
		var tmpArray3 = idd[x+1].split(",");
		nextlvl = tmpArray3[2];
		}

	else
		{
		nextlvl = "0";
		}
z=x+1;

// this item id
tId = "dv" + mp + x;
// next lvl id
nId = "tab" + mp + z;

// are there any child surrounds open that we need to close??
	if(thislvl=="1")
		{
		if(id2>"0")
			{
			document.write(ediv);
			document.write(ediv);
			--id2;
			}

		if(id1>"0")
			{
			document.write(ediv);
			document.write(ediv);
			--id1;
			}
		}

	if(thislvl=="2")
		{
		if(id2>"0")
			{
			document.write(ediv);
			document.write(ediv);
			--id2;
			}
		}


// does this child have children ??
	if(nextlvl>thislvl)
		{
//has children

		wrtChild2(idd,x,nextlvl,thislvl,tId,nId);
		wrtSur(idd,x,nextlvl,thislvl,tId,nId);
// increment counter
		if(thislvl=="1"){++id1;}
		if(thislvl=="2"){++id2;}
		}

	else
		{
// no children
		wrtChild(idd,x,nextlvl,thislvl);
		}
	}
// close menu surround
document.write(ediv);
}


// write child surround
function wrtSur(idd,x,nextlvl,thislvl,tId,nId)
{
document.write(mdiv1);
document.write(nId);
document.write(mdiv2);
}



// write child with children
function wrtChild2(idd,x,nextlvl,thislvl,tId,nId)
{
var stmpArray = idd[x].split(",");
document.write(mwc1);
document.write(tId);
document.write(mwc2);
document.write(tId);
document.write(mwc3);
document.write(nId);
document.write(mwc4);
document.write(nId);
document.write(mwc5);
document.write(mwd);
document.write(mwc6);

if(nextlvl<thislvl || nextlvl==thislvl)
	{
	prc = "tbl4";
	}

else
	{
	cont = true;
	tarLen = idd.length;
	yy = x;

	while(cont)
		{
		if(yy+1 != tarLen)
			{
			var tmpArray3 = idd[yy+1].split(",");
			next1lvl = tmpArray3[2];
			}

		else
			{
			next1lvl = "0";
			}

		if(next1lvl==thislvl)
			{
			prc = "tbl3";
			cont =false;
			}

		if(next1lvl<thislvl)
			{
			prc = "tbl4";
			cont =false;
			}
		++yy;
		}
	}


document.write(prc);
document.write(mwc7);
document.write(stmpArray[0]);
document.write(mwc8);
document.write(mmi);
document.write(mwc9);
document.write(mmw);
document.write(mwc10);
document.write(mmh);
document.write(mwc11);
document.write(mmv);
document.write(mwc12);
}


// write child with no children
function wrtChild(idd,x,nextlvl,thislvl)
{
var stmpArray = idd[x].split(",");

document.write(mnc1);
document.write("dv" + mp + x);
document.write(mnc2);
if(nextlvl<thislvl)
	{
	prc = "tbl4";
	}

else
	{
	prc = "tbl3";
	}

document.write(prc);
document.write(mnc3);
document.write(stmpArray[1]);
document.write(mnc4);
document.write(stmpArray[0]);
document.write(mnc5);
}




// WRITE CLASSES FUNCTION
function doClass()
{
document.write("<style type='text/css'>")
document.write("<!--")

// table with 3 borders class
document.write(".tbl3");
document.write("{");
document.write("font-family:" + ftf + ";")
document.write("font-size:" + fts + ";")
document.write("font-weight:" + ftw + ";")
document.write("color:" + nfc + ";")
document.write("text-align:" + cen + ";");
//document.write("padding:" + mpd + "px;");

if(nbc!="")
	{
	document.write("background-color:" + nbc + ";")
	}

if(mbw>0)
	{
	document.write("border-style:solid;");
	document.write("border-width:");
	document.write(mbw + "px " + mbw + "px " + "0px " + mbw + "px");
	document.write(";");
	document.write("border-color:" + mbc + ";");
	}

document.write("}");

// table with 4 borders class
document.write(".tbl4");
document.write("{");
document.write("font-family:" + ftf + ";")
document.write("font-size:" + fts + ";")
document.write("font-weight:" + ftw + ";")
document.write("color:" + nfc + ";")
document.write("text-align:" + cen + ";");


if(nbc!="")
	{
	document.write("background-color:" + nbc + ";")
	}

if(mbw>0)
	{
	document.write("border-style:solid;");
	document.write("border-width:");
	document.write(mbw + "px");
	document.write(";");
	document.write("border-color:" + mbc + ";");
	}

document.write("}");



// menu item with child class
document.write(".mwc");
document.write("{")
document.write("font-weight:" + ftw + ";");
document.write("padding:" + mpd + "px;");
document.write("}")

// menu item without child class
document.write(".mnc");
document.write("{")
document.write("font-weight:" + ftw + ";");
document.write("padding:" + mpd + "px;");
document.write("cursor:pointer;")
document.write("cursor:hand;")
document.write("}")


// nav item class with child
document.write(".mnu1");
document.write("{")
document.write("font-family:" + mftf + ";")
document.write("font-size:" + mfts + ";")
document.write("font-weight:" + mftw + ";")
document.write("color:" + mnfc + ";")
document.write("background-color:" + mnbc + ";")

if(mcen)
	{
	document.write("text-align:center;");
	}

document.write("}")


// nav item class without child
document.write(".mnu2");
document.write("{")
document.write("font-family:" + mftf + ";")
document.write("font-size:" + mfts + ";")
document.write("font-weight:" + mftw + ";")
document.write("color:" + mnfc + ";")
document.write("cursor:pointer;")
document.write("cursor:hand;")
document.write("background-color:" + mnbc + ";")

if(mcen)
	{
	document.write("text-align:center;");
	}

document.write("}")

document.write("//-->")
document.write("</style>")
}




// FUNCTION LINKS TO NEW PAGE
function goLoc(lc)
{
idd = eval("mnavArray" + lc);
window.location.href = idd[6];
}


// FUNCTION LINKS TO NEW PAGE
function goLoc2(lc)
{
window.location.href = lc;
}



function tst(id,idd)
{
w = "";
x = nhr;
y = nwh;

if(!hor)
	{
	x += nwd;
	y = eval(y+nvo);
	}


var w = document.getElementById(id);
while(w)
	{
	x += parseInt(w.offsetLeft);
	y += parseInt(w.offsetTop);
	w = w.offsetParent;
	}

//alert("x = " + x + " y = " + y);
//document.getElementById('inM').value = ("x = " + x + " y = " + y);

document.getElementById(idd).style.top = (y + "px");
document.getElementById(idd).style.left = (x + "px");
document.getElementById(idd).style.visibility = "visible";
}


function tst2(id,idd)
{
x = parseInt(mwd) + parseInt(mho);
y = mvo;

x += parseInt(document.getElementById(id).offsetLeft);
y += parseInt(document.getElementById(id).offsetTop);

document.getElementById(idd).style.left = (x + "px");
document.getElementById(idd).style.top = (y + "px");
document.getElementById(idd).style.visibility = "visible";
}

function hideDiv(id)
{
document.getElementById(id).style.visibility = "hidden";
}



// menu item mouseover colours
function chCol(idd)
{
idd.style.backgroundColor = moc;
idd.style.color = mfc;
}

// menu item reset colours
function rsCol(idd)
{
idd.style.backgroundColor = nbc;
idd.style.color = nfc;
}

// nav item mouseover colours
function chCol2(idd)
{
idd.style.backgroundColor = mmoc;
idd.style.color = mmfc;
}

// nav item reset colours
function rsCol2(idd)
{
idd.style.backgroundColor = mnbc;
idd.style.color = mnfc;
}

// image rollover
function chImg(id,idd)
{
ims = eval("mnavArray" + idd)
ims = ims[2]
id.src = ims;
}

// image reset
function rsImg(id,idd)
{
ims = eval("mnavArray" + idd)
ims = ims[1]
id.src = ims;
}
