// declaration
currentMouseHOffset = "" ;
currentMouseVOffset = "" ;
var preload_1  = new Image;


// initialization
initTrackMouse();
browserType = getBrowserType() ;
platformType = getPlatform() ;
preload_1.src =  "images/wait.gif" ;


// broswer type & plartform functions
function getBrowserType() {
	if (navigator.userAgent.indexOf("Opera")!=-1 && document.getElementById) type="Opera";	
	else if (navigator.userAgent.indexOf("Safari")!=-1) type="Safari";	
	else if (navigator.userAgent.indexOf("iCab")!=-1) type="iCab";	
	else if (document.all) type="Internet Explorer";
	else if (document.layers) type="Netscape";	//Netscape Communicator 4
	else if (!document.all && document.getElementById) type="Mozilla"; //Mozila e.g. Netscape 6 upwards
	else type = "??";		//I assume it will not get here
	return type ;
}

function getPlatform() {
	var browserInfo = navigator.userAgent.toLowerCase() ;
	if (browserInfo.indexOf("win")!=-1) {
		var platform = "Windows" ;
	} else if (browserInfo.indexOf("mac")!=-1) {
		var platform = "Macintosh" ;
	} else {
		var platform = "??" ;
	}
	return platform ;
}
// broswer type & plartform functions END

// news functions
function showDetails(thisID) {
	if (!document.getElementById(thisID)) {
		return false ;
	}
	var thisContent = document.getElementById(thisID).innerHTML ;
	if (document.getElementById('detailsbox').innerHTML != thisContent) {
		document.getElementById('detailsbox').innerHTML = thisContent ;
	}
	if (document.getElementById('detailsbox').style.visibility == "hidden") {
		if (currentMouseVOffset != "") {
			document.getElementById('detailsbox').style.top = currentMouseVOffset-360;
			document.getElementById('detailsbox').style.left = currentMouseHOffset-1020;
			document.getElementById('detailsbox').style.visibility = "visible";
		}
	}
}

function hideDetails(thisID) {
	document.getElementById('detailsbox').style.visibility = "hidden" ;
}
// news functions END

// mouse functions
function initTrackMouse() {
	var isNS4=document.layers?true:false;
	var isIE=document.all?true:false;
	var isIE6=isIE&&document.getElementById?true:false;
	var isIE4=isIE&&!isIE6?true:false;
	var isNS6=!isIE6&&document.getElementById?true:false;
	var isNS=isNS4||isNS6;
	
	if(isNS4) {
		document.captureEvents(Event.MOUSEMOVE); 
		document.onmousemove=showMouseLocation; 
	} else if(isNS6) {
		document.addEventListener("mousemove", showMouseLocation, true);
	} else if(isIE) {
	   document.onmousemove=showMouseLocation;
	}
}

function showMouseLocation(e) {
	//var isNS4=document.layers?true:false;
	var isIE=document.all?true:false;
	var isIE6=isIE&&document.getElementById?true:false;
	var isIE4=isIE&&!isIE6?true:false;
	//var isNS6=!isIE6&&document.getElementById?true:false;
	//var isNS=isNS4||isNS6;
	if (isIE4) {
		return true ;
	}
	
	if (document.all) {
		var mouseHOffset = event.clientX ;
		var mouseVOffset = event.clientY ;
	} else {
		var mouseHOffset = e.pageX ;
		var mouseVOffset = e.pageY ;
	}
	
	var theHScroll = 0;
	var theVScroll = 0;
	if (window.pageYOffset) {
		theHScroll = window.pageXOffset;
		theVScroll = window.pageYOffset;
	} else if (window.document.documentElement && window.document.documentElement.scrollTop) {
		theHScroll = window.document.documentElement.scrollLeft;
		theVScroll = window.document.documentElement.scrollTop;
	} else if (window.document.body) {
		theHScroll = window.document.body.scrollLeft;
		theVScroll = window.document.body.scrollTop;
	}
	if (browserType == "Internet Explorer") {
		mouseHOffset += theHScroll ;
		mouseVOffset += theVScroll ;
	}
	if (browserType == "Mozilla") {
		mouseVOffset -= 1 ;
		mouseHOffset -= 1 ;
	} else if (browserType == "Internet Explorer" && platformType == "Windows") {
		mouseHOffset  -= 2 ;
		mouseVOffset  -= 2 ;
	}
	currentMouseHOffset = mouseHOffset ;
	currentMouseVOffset = mouseVOffset ;
	
	// typeof showMapLocation == 'function' is to check the function has already been loaded (it's in another script, lower on the page)
	if (document.getElementById('latitude') && typeof showMapLocation == 'function') {
		showMapLocation(mouseHOffset,mouseVOffset) ;
	}
}

// mouse functions END

function setWaitIconVisibility(state) {
	if (!document.getElementById("waiticon")) return;
	if (state=='flip') {
		if (document.getElementById("waiticon").style.visibility == "hidden") {
			document.getElementById("waiticon").style.visibility = "visible" ;
		} else {
			document.getElementById("waiticon").style.visibility = "hidden" ;
		}
	} else {
		document.getElementById("waiticon").style.visibility = state ;
	}
}

function searchResultsOnLoad() {
	setWaitIconVisibility('hidden');
	if (document.getElementById("searchboxlayer")) {
		document.getElementById("searchboxlayer").style.visibility = "visible" ;
		document.searchform.search.select();
	}
}

function doSearchForm(sortcat,sortcol,sortdir,detailcat,page) {
	if (sortcat!='') {
		document.searchFormHidden.srtcat.value=sortcat;
	}
	if (sortcol!='') {
		document.searchFormHidden.srtcol.value=sortcol;
	}
	if (sortdir!='') {
		document.searchFormHidden.srtdir.value=sortdir;
	}
	if (page!='') {
		document.searchFormHidden.page.value=page;
	}
	if (detailcat=='' || detailcat==undefined) {
		document.searchFormHidden.action=document.searchFormHidden.action+'#'+sortcat;
	} else {
		document.searchFormHidden.category.value=detailcat;
	}
	document.searchFormHidden.submit();
}

function showObservationsMap(name,src,observationsOrSpecimens) {
	document.showobservationsmap.name.value = name;
	document.showobservationsmap.source.value = src;
	document.showobservationsmap.data_type.value = observationsOrSpecimens;
	document.showobservationsmap.submit();
}


function flipVisibility(id) {
	var object=document.getElementById(id);
	if(object.style.display=='') {
		object.style.display='none';
	} else {
		object.style.display='';
	}
}

function flipArrow(id) {
	var object=document.getElementById(id);
	if (object.src.indexOf('advanced_search_arrow_right.gif')>0) {
		object.src = 'images/advanced_search_arrow_down.gif';
	} else {
		object.src = 'images/advanced_search_arrow_right.gif';
	}
}

function tableVisibilityFlip(idData,idTxt,labelShow,labelHide) {
	var div1=document.getElementById(idData);
	var div2=document.getElementById(idTxt);
	if(div1.style.display=='block') {
		div1.style.display='none';
		div2.innerHTML=labelShow;
	} else {
		div1.style.display='block';
		div2.innerHTML=labelHide;
	}
}