var safari = false;
var agent = navigator.userAgent.toLowerCase();
if (agent.indexOf("safari") != -1){
	safari = true;
}

var searchMenuId = -1;

function clearTimer(){
	clearInterval( searchMenuId );
	searchMenuId = -1;	
}

/// currently selected subnav button and subnav node
var cSub;
var cSubID;
/// colors for second level subnav [ normalColor, normalColorBG, hiliteColor, hiliteColorBG ]
var colorSrch = new Array('#A97446', '#F5F1B3', '#5B3E1C', '#FFCCCC');
var colorProd = new Array('#476C11', '#F5F1B3', '#488888', '#D6E4D6');
var cColors;
///// third level navigation
var colorSrchSub = new Array('#5B3E1C', '#FFCCCC', '#F5F1B3', '#9D6B3A' );
var colorProdSub = new Array('#488888', '#D6E4D6', '#075795', '#BDFF99' );
var cColorsSub;


///// functions called from top level subnav
var cID;
function rollOverNav(DIV, BGCOLOR, COLOR){
	//// make sure any open subsub nav items are closed before colors change!
	doOverSub(); // hides subsubnav when called with no arguments
	cSub = null; cSubID = null;
	
	cID = DIV.id;
	/// just reset all
	document.getElementById("searchButton").style.color ="#6C2311";
	document.getElementById("searchButton").style.backgroundColor ="#F5F1B3";
	document.getElementById("regionsButton").style.color ="#DA9AB9";
	document.getElementById("regionsButton").style.backgroundColor ="#0B034B";
	document.getElementById("producersButton").style.color ="#BDFF99";
	document.getElementById("producersButton").style.backgroundColor ="#075795";	
	
	DIV.style.backgroundColor = BGCOLOR;
	DIV.style.color = COLOR;
	
	clearTimer();
}

function rollOutNav( who ) {
	searchMenuId = setInterval(killMenus, 500);
}

function makeVisible(DIVID){
	makeInvisible();
	if (safari) {
		if( document.getElementById('map') != null ) {
			document.getElementById('map').style.visibility = "hidden";
		}
	}	
	document.getElementById( DIVID ).style.display ="block";
	//// show overlay with semi-transparent png
	document.getElementById('overlay').style.display ="block";
}

function makeInvisible( DIVID ){
	if (DIVID=="search") {
		document.getElementById("keyword").blur();
	}	
	if (DIVID==null) {
		makeInvisible("search");
		makeInvisible("regions");
		makeInvisible("producers");
		return;
	} else {
		document.getElementById(DIVID).style.display ="none";
	}
	if (safari) {
		if( document.getElementById('map') != null ) {
			document.getElementById('map').style.visibility = "hidden";
		}		
	}		
}

/// functions used by top level nav and by Regions sub node
function rollOver(DIV, BGCOLOR, COLOR){
	DIV.style.backgroundColor = BGCOLOR;
	DIV.style.color = COLOR;
	clearTimer();
}
function rollOut() {
	searchMenuId = setInterval(killMenus, 500);
}

///// second level navigation
function overSubProd( WHO, DIVID ){
	cColors = colorProd;
	cColorsSub = colorProdSub;
	doOverSub( WHO, DIVID );
}
function overSubSrch( WHO, DIVID ){
	cColors = colorSrch;
	cColorsSub = colorSrchSub;
	doOverSub( WHO, DIVID );
}

function doOverSub( WHO, DIVID ){ 
	clearTimer();
	if( cSub != null ){
		cSub.style.color = cColors[0];
		cSub.style.backgroundColor = cColors[1];		
	}
	if( WHO != null ){
		WHO.style.color = cColors[2];
		WHO.style.backgroundColor = cColors[3];
		cSub = WHO;	
	} else {
		cSub = null;
	}
	
	if( cSubID != null ){
		document.getElementById( cSubID ).style.display ="none";
	}
	if( DIVID != null ){
		document.getElementById( DIVID ).style.display ="block";
		cSubID = DIVID;		
	} else {
		cSubID = null;
	}
}

function outSub( WHO ){
	searchMenuId = setInterval(killMenus, 500);
}


///// third level navigation
function overSubSub( WHO ){
	clearTimer();
	if( WHO != null ){
		WHO.style.color = cColorsSub[2];
		WHO.style.backgroundColor = cColorsSub[3];
	}
	cSub.style.color = cColorsSub[0];//'#488888';
	cSub.style.backgroundColor = cColorsSub[1];	
}
function outSubSub( WHO ){
	WHO.style.color = cColorsSub[0];
	WHO.style.backgroundColor = cColorsSub[1];
	searchMenuId = setInterval(killMenus, 500);
}



function killMenus() {
	clearTimer();
	
	doOverSub();// hides subsubnav when called with no arguments
	cSub = null; cSubID = null;
	
	makeInvisible();
	
	if( cID == 'searchButton' ){
		document.getElementById("searchButton").style.color ="#6C2311";
		document.getElementById("searchButton").style.backgroundColor ="#F5F1B3";
	}
	if( cID == 'regionsButton' ){
		document.getElementById("regionsButton").style.color ="#DA9AB9";
		document.getElementById("regionsButton").style.backgroundColor ="#0B034B";
	}
	if( cID == 'producersButton' ){
		document.getElementById("producersButton").style.color ="#BDFF99";
		document.getElementById("producersButton").style.backgroundColor ="#075795";	
	}	

	//// hide overlay
	document.getElementById('overlay').style.display ="none";
	
	if (safari) {
		if( document.getElementById('map') != null ) {
			document.getElementById('map').style.visibility = "visible";
		}
	}	
}
