//BROWSER DETECTION BLOCK
var ua = navigator.userAgent.toLowerCase()
var mac = (navigator.appVersion.indexOf('Mac') != -1)
var ie = (document.all);
var safari = (ua.indexOf('safari') != - 1);
var ns = (document.getElementById)

//INITIALISE GENERAL VARIABLES
ref = (document.getElementById) ? "document.getElementById" :"document.all";
lastmain = 0;
lastdropmain = 0;


	
//prepare clock and associated variabls
clockbase = 40
thetime = 0
stoptime = false;
//clock provides timebase to manage drop down functionality
function clock(){
	//reset the time to stop big number problems
	//first on a bigmenu clear
	if (stoptime){
		thetime = 0;
		stoptime = false;
		}
	//then after two minutes
	if (thetime > (1000 * 60 * 2)){
		thetime = 0;
		}
	
	thetime += clockbase;
	setTimeout("clock()",clockbase);
	timBasedOps()
	//deb(thetime);
	}
	
//sets a variable to reset the clock
function stopclock(){	
	stoptime = true;
	}
	
//prepare timebased operations
stdwait = 300;	
maintimer = 0
subtimer = 0
//Check to see if its time to clear navigation or subnavigation
function timBasedOps(){
	if (thetime > maintimer){
		clearall();
		}
	if (thetime > subtimer){
		clearsub()
		}
	}

// unwanted Mouseclick catcher
function nothing(){}

//return the screen width minus padding divided by 4
function getwidth(){
	document.body.clientWidth
	screenWidth=document.body.clientWidth;
	screenHeight=screen.availHeight;
	if (screenWidth < 755) {
		screenWidth = 755
		}
	w = (screenWidth - 50) / 4
	
	return w
	}

//Control the highlighting of the four main navigation items
function switchmain(isON){
	//alert("from switchmain" + lastmain);
	if (isON){
		if (lastmain !=0){
			eval(ref + '("nav'+ lastmain +'")' + '.style.background = "#B9B9B9"');
			}
		}
	else{
		if (lastmain !=0){
			eval(ref + '("nav'+ lastmain +'")' + '.style.background = "#ffffff"');
			lastmain=0;
			}
		}
	
	}
	
//the rollover from the four main navigation buttons
function roll(ison, which){
	
	if (ison){
		
		//tidy up
		fliplop = true
		clearall();
		flipflop=false;
		switchmain(0)
		lastmain=which;
		
		if (thetime == 0){ //initialise the clock
			clock();
			}	
		//highlight the main nav element
		switchmain(1)
		
		dl = 25 + ((which-1) * getwidth())
		
		//positions browser specific
		if (mac){
			if (ie){ // 
				dw = getwidth() +1; //main width
				dt = 192; //main top
				sdl = dl + dw -1;//sub left
				}
			else if (safari){//NS 
				dw = getwidth() -0;
				dt = 191
				sdl = dw -1;
				dl = dl + 1;
				}
			else {
				dw = getwidth() -1;
				dt = 189;
				sdl = dw -1;
				}
			}
		else {
			if (ie){ // 
				dw = getwidth() +1; //main width
				dt = 193; //main top
				sdl = dl + dw -2;//sub left
				}
			else{//NS MORE LATER
				dw = getwidth() -2;
				dt = 191
				sdl = dw -1;
				}
			}	
		
		//position the container
		eval(ref + '("mainnavdrop").style' + '.top = dt');
		eval(ref + '("mainnavdrop").style' + '.left = dl');
		eval(ref + '("mainnavdrop").style' + '.width = dw');
		
		//horizontallyposition the sub nav container
		eval(ref + '("subnavdrop").style' + '.top = dt');
		//alert(dt);
		if (which != 4){
			sdl = dl + getwidth() -0;
			eval(ref + '("subnavdrop").style' + '.left = sdl');
			}
		else {
			if (document.all){
				sdl = dl - getwidth() -0;
				}
			else{
				sdl = dl - getwidth() + 1;
				}
			eval(ref + '("subnavdrop").style' + '.left = sdl');
			}	
		eval(ref + '("subnavdrop").style' + '.width = dw');
		
		//call the navigation builder
		buildnav((which-1))
		
		//manage timer
		maintimer = thetime + (stdwait*5);
		
		}
	else {
		//manage timer
		maintimer = thetime + (stdwait*1);
		}
	}
	
//actions clear all dropdown menus
function clearall(){
	emptymain();
	emptysub();
	switchmain(0);
	stopclock();
	}
	
//actions to clear sub dropdown menu
function clearsub(){
	emptysub();
	}
	
	
//OBSOLTE?
function clk(which){
	
	}
	
//ROLLOVERS ON DROP DOWN 
function droproll(isON,which,sub){
		
	//FIRST THE MAIN DROPDOWN ITEMS
	if (isON == 0){ //rolloff
		txt = ref + '("maindrop'+ which +'")' + '.style.background = "#ffe5e5"';
		lastdropmain = 0;
		maintimer = thetime + (stdwait*1);
		}
	else{ //rollon
		txt = ref + '("maindrop'+ which +'")' + '.style.background = "#ffcccc"';
		
		//tidyup last rollover
		if (lastdropmain != 0){
			setTimeout("droproll(0," + lastdropmain + ",-1)",10)
			}
		lastdropmain = which;
		maintimer = thetime + (stdwait*5);
		}
	eval(txt);
	
	//NOW ISSUES RELATED TO DROPDOWN SUBMENUS
	if (sub >-1){//active submenu rollover
		if (isON == 1){
			subtimer = thetime + (stdwait*5);
			maintimer = thetime + (stdwait*5);
			}
		else {
			subtimer = thetime + (stdwait*1);
			maintimer = thetime + (stdwait*1);
			}
		}
	else if (sub == -2) { //main menu item with sub array (show the sub array)
		if (isON == 1){
			buildsubnav(1, which);
			
			//THIS MAY HAVE TO BE CALCULATED DIFFERENTLY
			//browser nudging
			if (mac){
				if (ie){
					dt = 194 + ((which+ 1) * 18 -24);
					}
				else if (safari){
					dt = 196 + ((which+ 1) * 18 -12);
					}
				else{
					dt = 205 + ((which+ 1) * 18 -23);
					}
				}
			else {
				if (ie){
					dt = 192 + ((which+ 1) * 18 -14);
					}
				else{
					dt = 194 + ((which+ 1) * 18 -12);
					}
				}
				
				
			eval(ref + '("subnavdrop").style' + '.top = dt');
			
			subtimer = thetime + (stdwait*5);
			maintimer = thetime + (stdwait*5);
			}
		else {// main menu item without sub item array
			maintimer = thetime + (stdwait*1);
			subtimer = 0;//thetime + (stdwait*1);
			}
		}
	
	}



//WRITE THE MAIN DROP DOWN ON THE FLY BASED ON DATA IN navdata.js
function buildnav(which){
	stringbuffer = "";
	for(i=0;i<dataArray[which].length;i++){
		txt = dataArray[which][i][0];
		lnk = dataArray[which][i][1];
		isArray = true;
		if (lnk == "*") {
			isArray = false;
			}
		stringbuffer += "<a href=\"";
		if (isArray){
			stringbuffer += lnk;
			}
		else{
			stringbuffer += "Javascript:nothing()"
			}
		if (isArray){
			stringbuffer += "\" onMouseOver=\"droproll(1," + i + ",-1)\" onMouseOut=\"droproll(0," + i + ",-1)\" class=\"navmenulink\" >";
			}
		else {
			stringbuffer += "\" onMouseOver=\"droproll(1," + i + ",-2)\" onMouseOut=\"droproll(0," + i + ",-2)\" class=\"navmenulink\" >";
			}
		stringbuffer += "<div class=\"navnode\" id=\""; //"<div class=\"navtree\">"
		stringbuffer += "maindrop" + i;
		stringbuffer += "\"";
		if ((mac) && (ie)){
			stringbuffer += " style=\"margin:-2px 0px 0px 0px;\"";
			}
		stringbuffer += ">";
		
		if (isArray)
			{
			stringbuffer += "<img src=\"img/nodemarker.gif\" border=\"0\" hspace=\"4\">"
			}
		else{
			stringbuffer += "<img src=\"img/jointmarker.gif\" border=\"0\" hspace=\"4\">"
			}
		stringbuffer += txt;
		stringbuffer += "</div>";
		stringbuffer += "</a>\n";
		
		}
	eval(ref + '("mainnavdrop")' + '.innerHTML = stringbuffer');
}

//clear the sub Drop down navigation
function emptysub(){
	eval(ref + '("subnavdrop")' + '.innerHTML = ""');
	}
	
//clear the main navigation
function emptymain(){
	eval(ref + '("mainnavdrop")' + '.innerHTML = ""');
	}

//BUILD THE SUB-DROPDOWN ON THE FLY BASED ON DATA IN navdata.js
function buildsubnav(which, whichsub){
	stringbuffer = "";
	for(i=0;i<subsarray[which][whichsub].length;i++){
		txt = subsarray[which][whichsub][i][0];
		lnk = subsarray[which][whichsub][i][1];
		if (i==0){
			stringbuffer += "<div class=\"subnavnode\" style=\"border-width:1px\" id=\""; //"<div class=\"navtree\">"
			}
		else{
			stringbuffer += "<div class=\"subnavnode\" id=\""; //"<div class=\"navtree\">"
			}
			
		stringbuffer += "subdrop" + i;
		stringbuffer += "\"";
		if ((mac) && (ie)){
			stringbuffer += " style=\"margin:0px 0px 0px 0px;\"";
			}
		stringbuffer += ">";
		stringbuffer += "<a href=\"";
		stringbuffer += lnk;
		stringbuffer += "\" onMouseOver=\"droproll(1," + whichsub +"," + i + ")\" onMouseOut=\"droproll(0," + whichsub +"," + i + ")\" class=\"navmenulink\" >";
		stringbuffer += "<img src=\"img/nodemarker.gif\" border=\"0\" hspace=\"4\">"
		stringbuffer += txt;
		stringbuffer += "</a>";
		stringbuffer += "</div>\n";
		}

	eval(ref + '("subnavdrop")' + '.innerHTML = stringbuffer');
}

//PLACEHOLDER FUNCTION. In case we want to fine tune positioning of mainnav elements
function navnudge(){
	
	}

//SELECT BOX SCRIPTS

//deals with the rollover in the jumpbox once it has "dropped down"	
function selrol(isON, which){
	if (isON){
		eval(ref + '("selfield'+ which +'").style' + '.color = "#ffffff"');
		eval(ref + '("selfield'+ which +'").style' + '.background = "#666666"');
		}
	else {
		eval(ref + '("selfield'+ which +'").style' + '.color = "#666666"');
		eval(ref + '("selfield'+ which +'").style' + '.background = "#ffffff"');
		}
	}
	
//flipflop showing state of drop down
SelectOn = false;

//Drop down the select box
function showselect(){
	if (SelectOn) {
		hideselect();
		SelectOn = false;
		}
	else{
		stringbuffer  = "<div id=\"selectbuttond\" class=\"selectbutton\"><a href=\"Javascript:showselect()\" ><img src=\"img/selectbutton.gif\" width=\"18\" height=\"18\" border=\"0\"></a></div>";
		stringbuffer +=	"<a href=\"Javascript:showselect()\" class=\"selectfield0\" ><div style=\"width:125px\" class=\"selectfield1\">select an option</div></a>";
		
		for(i=0; i<jumpboxes[0]["values"].length;i++){
			arr = jumpboxes[0]["values"][i];
			stringbuffer += "<a href=\"Javascript:setselectval('" + arr[1] + "')\" class=\"selectfield\" onmouseOver=\"selrol(1," + i + ")\" onmouseout=\"selrol(0," + i + ")\" >";
			stringbuffer += "<div class=\"selectfieldx\" id=\"selfield" + i + "\">";
			stringbuffer += arr[2];
			stringbuffer += "</div></a>";
			}
		
		eval(ref + '("selectfields")' + '.innerHTML = stringbuffer');
		
		SelectOn = true
		}
	}
	
//Hide the Drop Down Box
function hideselect(){
	stringbuffer  = "<div id=\"selectbuttond\" class=\"selectbutton\"><a href=\"Javascript:showselect()\" ><img src=\"img/selectbutton.gif\" width=\"18\" height=\"18\" border=\"0\"></a></div>";
	stringbuffer +=	"<a href=\"Javascript:showselect()\" class=\"selectfield0\" ><div style=\"width:125px\" class=\"selectfield1\">select an option</div></a>";
	eval(ref + '("selectfields")' + '.innerHTML = stringbuffer');
	}
	
//Catch the value of the selection made. Currently stored in a hidden form field.
//LATER SHOULD CALL DOCUMENT.LOCATION
function setselectval(which) {
	hideselect();
	document.form1.topic.value = which;
	//document.location = which
	}
	
// A function to fine tune the positioning of the drop down box.
function selectnudge(){
	if (mac){
		if (ie){
			eval(ref + '("selectfields")' + '.style.top = 242');
			}
		else if (safari){
			//alert("safari called");
			eval(ref + '("selectfields")' + '.style.top = 255');
			}
		else {
			eval(ref + '("selectfields")' + '.style.top = 252');
			}
		
		}
	}
