/**  * Functions providing overlay functionality for map server maps */var minMapLat = -90;var maxMapLat = 90;var minMapLong = -180;var maxMapLong = 180;var boxWidth = 40;var boxHeight = 20;var boxWidthInDegrees=20;var boxHeightInDegrees=10;var snapHalf=true;        var capture=false;var minX;var minY;var nudge=5;var zoom=1;var isIE = false;var northCoordinate=0;var westCoordinate=0;var southCoordinate=0;var eastCoordinate=0;var overviewMapName="overviewMap";var mapSelectorName="mapSelector";function mouseMove(ev){	if (capture) {		ev = ev || window.event;		var mousePos = mouseCoords(ev);		var selector = document.getElementById(mapSelectorName);		moveSelector(selector, mousePos.x, mousePos.y);					setCoordinates(overviewMapName);	}}/** * Supports 5 zooming levels. *  * level 1 - global (180x360) * level 2- quadrant (90x180) * level 3 -  is an area of 40x80 * level 4 -  is an area of 20x40 * level 5 -  is an area of 10x20 *  * levels 1-4 when selected all zoom into a level 5 view, ie. 10x20 */function selectBox() {		//alert('minX:'+minX+', minY:'+minY);	//map size is 360px by 720px		// zoom of 1 is the global view - 180x360	if (zoom==1) {		// 2 pixels represents 1 degree				var lon = - 180 + Math.floor(minX/2) ;		var lat = 90  - Math.floor(minY/2) - 10;		zoom2level5(lon, lat);	}	// zoom of 2 is a quadrant of the global view - 90x180	if (zoom==2) {		// 4 pixels represents 1 degree		var lon = minMapLong + Math.floor(minX/4);		var lat = maxMapLat - Math.floor(minY/4) - 10;		zoom2level5(lon, lat);	}		// zoom of 3 is an area of 40x80	if (zoom==3) {		// 9 pixels represents 1 degree		var lon = minMapLong + Math.floor(minX/9);		var lat = maxMapLat - Math.floor(minY/9) - 10;		zoom2level5(lon, lat);	}			// zoom of 4 is an area of  20x40	if (zoom==4) {		// 18 pixels represents 1 degree		var lon = minMapLong + Math.floor(minX/18);		var lat = maxMapLat - Math.floor(minY/18) - 10;		zoom2level5(lon, lat);	}		// zoom of 5 is an area of 10x20	if (zoom==5) {		// 36 pixels represents 1 degree		var lat = maxMapLat - (minY/36) - 1;		var lon = minMapLong + (minX/36);		refocus(zoom+1, lon, lat, lon+2, lat+1);	}}/** * Zoom in to level 5 - 10x20 using the specified min lat/long */function zoom2level5(minLongitude, minLatitude){	refocus(5, minLongitude, minLatitude, minLongitude+20, minLatitude+10);}/** * Redirects to the specified zoom level. */function refocus(zoom, minLongitude, minLatitude,  maxLongitude, maxLatitude){//		alert("refocus: "+zoom+', '+minLongitude+', '+minLatitude+', '+maxLongitude+', '+maxLatitude);		// %2B is a plus (+)		var extent = minLongitude + "%2B" + minLatitude + "%2B" + maxLongitude + "%2B" +maxLatitude;		document.location=document.location.pathname 			+ "?extent=" + extent 			+ "&zoom=" + zoom						+ "&minMapLong=" + minLongitude						+ "&minMapLat=" + minLatitude			+ "&maxMapLong=" + maxLongitude			+ "&maxMapLat=" + maxLatitude			+ "&"+extraParams;	}/** * Redirects to occurrence search. */function occurrenceSearch() {	// 36 pixels represents 0.1 degrees	var latMin = maxMapLat - (minY/360) - 0.1;	latMin = (Math.round(latMin*10) )/10;		var latMax = latMin + 0.1;	latMax = (Math.round(latMax*10) )/10;		var longMax = minMapLong + (minX/360) + 0.1;	longMax = (Math.round(longMax*10) )/10;	var longMin = longMax - 0.1;	longMin = (Math.round(longMin*10) )/10;	//alert("Searching for:\n 1) Name = " + name + "\n 2) " + latMin + " <= Latitude <= " + latMax + "\n 3) " + longMin + " <= Longitude <= " + longMax);	redirectToCell(longMin, latMin, longMax, latMax);}/** * Zooms out to the level above. */function zoomOut() {		if(zoom ==2){		adjustViewArea(1, -180, -90, 360, 180);	}	if(zoom==3){		//go to zoom 2		adjustViewArea(2, -50, -25, 180, 90);	}	if(zoom==4){		//go to zoom 3		adjustViewArea(3, -20, -10, 80, 40);	}	if(zoom==5){		//go to zoom 4		adjustViewArea(4, -10, -5, 40, 20);	}	if(zoom==6){		//go to zoom 5		adjustViewArea(5, -9, -4, 20, 10);	}}/** * Adjusts the view area by the specified amounts. */function adjustViewArea(zoomLevel, longAdjustment, latAdjustment, longRange, latRange){	var minLongitude = minMapLong +	longAdjustment;	var minLatitude = minMapLat +latAdjustment;	//sanity checks	if(minLongitude<-180)		minLongitude=-180	if(minLatitude<-90)		minLatitude=-90;				if(minLongitude+longRange>180){		minLongitude=180-longRange;	}		if(minLatitude+latRange>90){		minLatitude=90-latRange;	}	refocus(zoomLevel, minLongitude, minLatitude, minLongitude+longRange, minLatitude+latRange);}function nudgeUp() {	if (maxMapLat<90) {		minMapLat = minMapLat+nudge;		maxMapLat = maxMapLat+nudge;		reload();	}}function nudgeDown() {	if (minMapLat>-90) {		minMapLat = minMapLat-nudge;		maxMapLat = maxMapLat-nudge;		reload();	}}function nudgeLeft() {	if (minMapLong>-180) {		minMapLong = minMapLong-nudge;		maxMapLong = maxMapLong-nudge;		reload();	}}function nudgeRight() {	if (maxMapLong<180) {		minMapLong = minMapLong+nudge;		maxMapLong = maxMapLong+nudge;		reload();	}}function reload() {	document.location=document.location.pathname + "?extent=" + buildExtent() 		+ "&minMapLat=" + minMapLat		+ "&maxMapLat=" + maxMapLat		+ "&minMapLong=" + minMapLong		+ "&maxMapLong=" + maxMapLong		+ "&zoom=" + zoom		+ "&"+extraParams;}function buildExtent() {	return minMapLong + "%2B" + minMapLat + "%2B" + maxMapLong + "%2B" + maxMapLat;}function moveSelector(selector, x, y) {		var mapImg = document.getElementById(overviewMapName);	var mapPos = findPos(mapImg);	var mapLeft = mapPos.x;	var mapTop = mapPos.y;	if(isIE)		y = y + document.documentElement.scrollTop;	if (snapHalf) {		x=Math.floor(x);		y=Math.floor(y);			} else {		x=((boxHeight)*Math.floor((x-mapLeft)/(boxHeight))) + mapLeft;		y=((boxHeight)*Math.floor((y-mapTop)/(boxHeight))) + mapTop;	}		// make sure it is within limits	if (x>(mapLeft+(mapImg.offsetWidth - boxWidth))) {		x=(mapLeft+(mapImg.offsetWidth-boxWidth))	} else if (x<(mapLeft)) {		x=mapLeft;	}		if (y>(mapTop+(mapImg.offsetHeight-boxHeight))) {		y=(mapTop+(mapImg.offsetHeight-boxHeight));	} else if (y<mapTop) {		y=(mapTop);	} 	selector.style.left = x+"px";	selector.style.top = y+"px";	minX = x-mapLeft;	minY = y-mapTop;	//alert("moved " + document.getElementById("mapSelector") + " to " + x + ":" + y + " \nset minX: " + minX + ", minY: " + minY);}/** * Sets coordinate values in the UI for positioning. */function setCoordinates(mapImgName){	var latRange = maxMapLat - minMapLat;	var longRange = maxMapLong - minMapLong; 	var mapHeight = document.getElementById(mapImgName).height;	var mapWidth= document.getElementById(mapImgName).width;			if(zoom==6){		northCoordinate = maxMapLat - Math.round (( (minY *10)  /  (mapHeight/latRange) )) / 10;		westCoordinate   = minMapLong + Math.round (( (minX*10)  /  (mapWidth/longRange) )) / 10;		//round these values		northCoordinate = (Math.round(northCoordinate*10))/10;		westCoordinate = (Math.round(westCoordinate*10))/10;		southCoordinate = (Math.round((northCoordinate-boxHeightInDegrees)*10))/10; 		eastCoordinate = (Math.round((westCoordinate+boxWidthInDegrees)*10))/10;	} else {		northCoordinate = Math.floor(maxMapLat-(minY/(mapHeight/latRange)));		westCoordinate = Math.floor(minMapLong+(minX/(mapWidth/longRange)));		southCoordinate = northCoordinate-boxHeightInDegrees;		eastCoordinate = westCoordinate+boxWidthInDegrees;			}		//sanity checks	northCoordinate = northCoordinate>90 ? 90 :northCoordinate;	southCoordinate = southCoordinate<-90 ? -90 :southCoordinate;	westCoordinate = westCoordinate<-180 ? -180 :westCoordinate;	eastCoordinate = eastCoordinate>180 ? 180 :eastCoordinate;		document.getElementById("northCoordinate").innerHTML=northCoordinate		document.getElementById("southCoordinate").innerHTML=southCoordinate;	document.getElementById("westCoordinate").innerHTML=westCoordinate;	document.getElementById("eastCoordinate").innerHTML=eastCoordinate;		}function setBoundingBoxMarkerOnMap(mapImgName, imgContainerDiv, bbMarkerName, minLong, minLat, maxLong, maxLat){	var bbMarker = document.getElementById(bbMarkerName);	var mapImg = document.getElementById(mapImgName);//	alert(mapImg.offsetWidth+":"+mapImg.offsetHeight);	var bbMarkerWidth = Math.round((maxLong - minLong)*(mapImg.offsetWidth / 360));	var bbMarkerHeight = Math.round((maxLat - minLat)*(mapImg.offsetHeight / 180));		//set div dimensions	var smallMapLeft = findPos(mapImg).x;	var smallMapTop = findPos(mapImg).y;		var posLeft = smallMapLeft + Math.round((minLong + 180)*(mapImg.offsetWidth / 360));	var posTop = smallMapTop+Math.round((90 - maxLat)*(mapImg.offsetHeight / 180));	var posRight = posLeft+bbMarkerWidth;	var posBottom = posTop+bbMarkerHeight;		var smallMapRight = smallMapLeft + mapImg.offsetWidth;		var smallMapBottom = smallMapTop + mapImg.offsetHeight;			var bbMarkerLeft = 0;	var bbMarkerTop = 0;			if(posRight > smallMapRight){		bbMarkerLeft = smallMapRight - bbMarkerWidth;	} else {		bbMarkerLeft = posLeft;	}		//sanity check	if(posLeft < smallMapLeft){		bbMarkerLeft = smallMapLeft;	}			if(posBottom > smallMapBottom){		bbMarkerTop = smallMapBottom - bbMarkerHeight;			} else {		bbMarkerTop = posTop;	}		//sanity check	if(posTop < smallMapTop){		bbMarkerTop = smallMapTop;	}		//set the position relative to the container div	var containerDiv = document.getElementById(imgContainerDiv);	//alert(containerDiv.id);	//alert(bbMarkerWidth+":"+bbMarkerHeight);		bbMarker.style.display = "block";			bbMarker.style.position = "relative";//		bbMarker.style.width = "800px";//	bbMarker.style.height = "200px";				bbMarker.style.width = bbMarkerWidth +"px";	bbMarker.style.height = bbMarkerHeight +"px";		//alert(containerDiv.offsetLeft+":"+containerDiv.offsetTop);	//alert(bbMarkerLeft+":"+bbMarkerTop);//	bbMarker.style.position = "relative";	bbMarker.style.left = (bbMarkerLeft - containerDiv.offsetLeft)+"px";	bbMarker.style.top = (bbMarkerTop - containerDiv.offsetTop - mapImg.offsetHeight)+"px";	bbMarker.style.visibility = "visible";}function mouseCoords(ev){	if(ev.pageX || ev.pageY){		return {x:ev.pageX, y:ev.pageY};	}	return {		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,		y:ev.clientY + document.body.scrollTop  - document.body.clientTop	};}function findPos(obj) {	var curleft = curtop = 0;	if (obj.offsetParent) {		curleft = obj.offsetLeft		curtop = obj.offsetTop		while (obj = obj.offsetParent) {			curleft += obj.offsetLeft			curtop += obj.offsetTop		}	}	return {		x:curleft,		y:curtop	};}
