var selectedRow="td0";
var cellHeight = 24; /* 22 = height, 1 = padding */
var standardRowCount = 7;

/* Adds the navigation table to the page */
function AddNavTable(displayJobSearch)
{
	var addCellHeight;
	
	var bSafari = false;
	
	topDiv = document.getElementById("topImage");
	buildNavTable(_NavTableData,topDiv, displayJobSearch);
	addCellHeight = 0;
		
	/*if ((BrowserDetect.browser)=="Safari")
	{
		if (parseInt(BrowserDetect.version) > 500)
		{
			bSafari = true;
		}
	}
	
	var safariMenuOptionTextLength;*/
	
	var maxChars=0;
	
	/* Adjust height for menu options that span more than 1 line (16 char). */
	for (i=0;i < numrows*5; i += 5)
	{	
		maxChars = 17;
		if (_NavTableData[i].length > maxChars) 
		{
			addCellHeight = addCellHeight + (Math.floor(_NavTableData[i].length/maxChars));
		}	
		/*
		{
			if (bSafari)
			{
				safariMenuOptionTextLength = 0;
				for (t=0;t < _NavTableData[i].length;t++)
				{
					if (_NavTableData[i].charAt(t) != " ")
						safariMenuOptionTextLength++;
				}
				addCellHeight = addCellHeight + (Math.floor(safariMenuOptionTextLength/17) - 1);
			
			}
			else
			{
				addCellHeight = addCellHeight + (Math.floor(_NavTableData[i].length/17));
			}
		}*/
	}
	
	
	
	/*Dynamically set the height of the menu depending on how many menu items there are*/
	thestyle = document.getElementById("bottomImage").style;	
	thestyle.height = DetermineNavBHeight(parseInt(numrows), addCellHeight);

	/* Set the current button to "on" state based on GUID of page*/	
	var tNumRows = numrows;
	if (displayJobSearch) { tNumRows++; }
	
	for (i = 2 ; i < tNumRows*5 ; i += 5)
	{		
		if (_NavTableData[i].toUpperCase() == _NavThisItemGuid.toUpperCase()) 
		{
			selectedRow = "td" + (Math.floor(i/5) + 1);	
					
			if (displayJobSearch && (selectedRow == "td" + tNumRows)) 
			{
				selectedRow = "jobSearch";
			}
			
			break;
		}
	}
	
	if (selectedRow == "td1")
		selectedRow = "td0";
		
	if (selectedRow != "td0")
		bcolor(_NavTableData[i + 1],_NavTableData[i + 2],selectedRow,"on",displayJobSearch);

}

/* This function allows us to reference style properties - It is needed because you can't reference style properties if the css is in an include file (like we have) and not inline. */
function getStyle(oElm, strCssRule)
{
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}

/* Nav Table Construction */
function buildNavTable(_data, _addNode, displayJobSearch)
{
		
	table = document.createElement("TABLE");
	table.width = "100%";
	table.className = "navTable";
	table.style.tableLayout = "fixed";
	table.id="navTableControl";

	tablebody = document.createElement("TBODY");
	row = document.createElement("TR");
	row.className = "nav_menuRow";
	cell = document.createElement("TD");
	cell.className = "nav_home";
	cell.id="td1";
	cell.onclick = new Function("evt_click_1", "selectedRow='td1';bcolor('" + _data[3] + "','" + _data[4] + "','td1','on'," + displayJobSearch + ");window.open('" + _data[1] + "','_self'); return false");
	cell.onmouseover= new Function("evt_onmouseover_1","bcolor('"+_data[3]+"','"+_data[4]+"','td1','on'," + displayJobSearch + ");return true;");
	cell.onmouseout= new Function("evt_onmouseout_1","if (selectedRow != 'td1'){bcolor('#004f7b','white','td1','out'," + displayJobSearch + ")};return true;");
	cell.appendChild(document.createTextNode(_data[0]));
	row.appendChild(cell);
	tablebody.appendChild(row);

	
	for(j=2 ; j <= numrows; j++) 
	{ 
		row = document.createElement("TR");
		row.className = "nav_menuRow";
		cell = document.createElement("TD");
		cell.className = "nav_menuOption";
		cell.id="td" + j;
		k = j - 1;
		cell.onclick = new Function("evt_click_" + j, "selectedRow='td"+j+"';bcolor('"+_data[k*5 + 3]+"','"+_data[k*5 + 4]+"','td"+j+"','on'," + displayJobSearch + ");window.open('"+_data[k*5 + 1]+"','_self');return false");
		cell.onmouseover= new Function("evt_onmouseover_" + j,"bcolor('"+_data[k*5 + 3]+"','"+_data[k*5 + 4]+"','td"+j+"','on'," + displayJobSearch + ");return true;");
		cell.onmouseout= new Function("evt_onmouseout_" + j,"if (selectedRow != 'td"+j+"'){bcolor('#F5ECE3','#005B92','td"+j+"','out'," + displayJobSearch + ")};return true;");

		if (_data[(5 * (j - 1))].length > 16)
		{
			firstbreak = _data[(5 * (j - 1))].lastIndexOf(' ',16);
			var newOptionName;
			var addspaces;
			addspaces = " ";
			for (i=firstbreak;i < 20;i++)
			{
				addspaces = addspaces + " ";
			}
			
			newOptionName = _data[(5 * (j - 1))].substr(0,firstbreak) + addspaces + _data[(5 * (j - 1))].substr(firstbreak+1,_data[(5 * (j - 1))].length)
		}
		
		cell.appendChild(document.createTextNode(_data[(5 * (j - 1))]));
		row.appendChild(cell);
		tablebody.appendChild(row);
	}
	
	table.appendChild(tablebody);
	_addNode.appendChild(table);

	if (displayJobSearch) 
	{
		k = numrows;
		jobSearchDiv = document.getElementById("jobSearch");
		jobSearchDiv.innerHTML = _data[(5 * k)];
		jobSearchDiv.onclick = new Function("evt_click_" + numrows, "selectedRow='jobSearch';bcolor('"+_data[k*5 + 3]+"','"+_data[k*5 + 4]+"','jobSearch','on'," + displayJobSearch + ");window.open('"+_data[k*5 + 1]+"','_self');return false");
		jobSearchDiv.onmouseover= new Function("evt_onmouseover_" + numrows,"bcolor('"+_data[k*5 + 3]+"','"+_data[k*5 + 4]+"','jobSearch','on'," + displayJobSearch + ");return true;");
		jobSearchDiv.onmouseout= new Function("evt_onmouseout_" + numrows,"if (selectedRow != 'jobSearch'){bcolor('#F5ECE3','white','jobSearch','out'," + displayJobSearch + ")};return true;");
	}
}

/* Back Colour */
function bcolor(bcol, tcol, d_name, direction, displayJobSearch)
{
	var thestyle= eval('document.getElementById("'+d_name+'").style');

	if (displayJobSearch && (d_name == "jobSearch"))
	{
		bcol = "#F5ECE3";
	}

	thestyle.backgroundColor=bcol;
	thestyle.color=tcol;
	
	if (d_name == "td1" && direction=="on")
	{
		topDiv = document.getElementById("topImage");
		topDiv.className = "nav_topover";
	}

	if (d_name == "td1" && direction=="out" && selectedRow != "td1")
	{
		topDiv = document.getElementById("topImage");
		topDiv.className = "nav_t";
	}

	if (displayJobSearch && (d_name == "jobSearch" && direction=="on"))
	{
		jobSearchDiv = document.getElementById("jobSearch");
		jobSearchDiv.className = "navJobSearchOver";
	}

	if (displayJobSearch && (d_name == "jobSearch" && direction=="out" && selectedRow != "jobSearch"))
	{
		jobSearchDiv = document.getElementById("jobSearch");
		jobSearchDiv.className = "navJobSearch";
	}
	
	for (i = 1;i <= numrows; i++)
	{	
		if (d_name != "td1" && selectedRow != "td1") 
		{
			thestyle= eval ('document.getElementById("td1").style');
			thestyle.backgroundColor='#004f7b';
			thestyle.color='white';
			topDiv = document.getElementById("topImage");
			topDiv.className="nav_t";
		}
		
		if (d_name != "td" + i && selectedRow != "td" + i) 
		{
			thestyle= eval ('document.getElementById("td' + i + '").style');
			thestyle.backgroundColor='#F5ECE3';
			thestyle.color='#005B92';
		}
	}

	if (displayJobSearch &&  (d_name != "jobSearch" && selectedRow != "jobSearch"))
	{
		jobSearchDiv = document.getElementById("jobSearch");
		jobSearchDiv.className="navJobSearch";		
	}

}

/* Determines the correct height of the nav_b element */
function DetermineNavBHeight(numRows, addCellHeight)
{	
	
	var addOffsetHeight;
	
	if((BrowserDetect.browser)=="Safari" )
	{
		addOffsetHeight = 8;

	} else {
		addOffsetHeight = 9;
	}

	var heightAdjustment=0;
	if ((numRows >= 9) && (numRows < 12))
	{
		if ((BrowserDetect.browser)=="Explorer" ) {
			heightAdjustment = -3;
		} else if ((BrowserDetect.browser)=="Firefox" ) {
			heightAdjustment = -4.5;
		} else if ((BrowserDetect.browser)=="Safari" ) {
			heightAdjustment = -4;
		}		
		heightAdjustment += numrows - 9;
	}
	
	if ((numRows >= 12))
	{
		if ((BrowserDetect.browser)=="Explorer" ) {
			heightAdjustment = -0.2;
		} else if ((BrowserDetect.browser)=="Firefox" ) {
			heightAdjustment = -2.0;
		} else if ((BrowserDetect.browser)=="Safari" ) {
			heightAdjustment = -1.5;
		}		
		heightAdjustment += (numrows - 12) * 0.25;
	}
	
	var suppValue = (cellHeight + heightAdjustment) * ( parseInt(numRows) - standardRowCount ) + parseInt( addCellHeight * addOffsetHeight );
	
	//if( (BrowserDetect.browser)=="Explorer" )
	//{	
		
		/*if (parseInt(BrowserDetect.version) > 6)
		{
			if (numrows > 7) {
				var counterOffset = numrows ;
				suppValue -= Math.floor(counterOffset - (numrows));
			}
		}*/
	//}	
	return parseInt(getStyle(document.getElementById('bottomImage'),'height')) + suppValue + "px";
}

/* BROWSER DETECTION */
var BrowserDetect = 
{
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
		
