
  //<![CDATA[
var map1;	
	
	// Load the GMap in the page
function loadMap() {
	size = 0 + 1;
	var set = new Array(size);
	var coord = new Array();
	coord[0] = document.getElementById("northbound").value;
	coord[1] = document.getElementById("westbound").value;
	coord[2] = document.getElementById("southbound").value;
	coord[3] = document.getElementById("eastbound").value;
	wkt0 = formulateWKT(coord[2], coord[1], coord[0], coord[3]);
	set[0] = wkt0;
	if (GBrowserIsCompatible()) {
		map1 = new GMap2(document.getElementById("map1"));
		map1.addControl(new GSmallZoomControl());

			// need to set the the map before adding overlays
		map1.setCenter(new GLatLng(0, 0), 20);
		map1.setMapType(G_NORMAL_MAP);
		map1.addMapType(G_SATELLITE_MAP);
		map1.removeMapType(G_HYBRID_MAP);
		map1.removeControl(new GMapTypeControl());
		for (j = 0; j < set.length; j++) {
			highlightFeature(set[j]);
		}
		var bounds = jpGetGBoundsForAllWKT(set);
		mhFocusOnBox(map1, bounds);
		var bounds = map1.getBounds();
		var southWest = bounds.getSouthWest();
		var northEast = bounds.getNorthEast();
		var lngDelta = (northEast.lng() - southWest.lng()) / 4;
		var latDelta = (northEast.lat() - southWest.lat()) / 4;
	}
}

	// Zooms to global view of world
function zoomOutToWorld() {
	map1.setCenter(new GLatLng(0, 0), 0); // v2
}

	// Draws a coverage on the map
	// wkt = A single coverage in WKT format
function highlightFeature(wkt) {
	var bounds = jpGetGBoundsForWKT(wkt);
	var width = mhGetBoxWidth(bounds);
	var height = mhGetBoxHeight(bounds);
	if ((width == 0) && (height == 0)) {
		var markerLatLng = new GLatLng(bounds.getCenter().lat(), bounds.getCenter().lng());
		var icon = new GIcon();
		icon.image = "../images/marker16.png";
		icon.iconSize = new GSize(27, 27);
		icon.shadowSize = new GSize(5, 9);
		icon.iconAnchor = new GPoint(16, 27);
		var marker = new GMarker(markerLatLng, icon);
		map1.addOverlay(marker);
		map1.setCenter(markerLatLng);
	} else {
		var poly = mhCreateBoundsPoly(map1, bounds);
		map1.addOverlay(poly);
	}
}
    //]]>
//function getKey() {
//	return "ABQIAAAAlS3JK-ZqWSF9DvlsVuKDyBStLbd1zlO1JgMSKHwKtpUL2VUKEBSmWsDDBEvoiZmIBv4jFIgbYNMZ6g";
//}


// Include Google Maps API from maps.google.com
//document.write("<scr" + "ipt src=\"http://maps.google.com/maps?file=api&amp;v=2&amp;key=" + getKey() + "\" type=\"text/javascript\"></scr" + "ipt>");
document.write(tag);
/** Reusable functions from Mark Harwood */

	// Creates a polyline (box) for the given map and GLatLngBounds
	// box = GLatLngBounds
function mhCreateBoundsPoly(map, box) {

        // if width > 180, google bug will draw the box with
		// east and west reversed because GLatLngBounds get normalized
		// So, must add two extra points at (nLat, centerLng) and
		// (sLat, centerLng) to force it to draw the correct box
	var c = box.getCenter().lng(); // center lng
	var c2 = box.getCenter().lat(); // center lat
	var height = box.toSpan().lat();
	var hDiff = 0;
	if (height > 0) {
		hDiff = height / 100;
	}
	var width = mhGetBoxWidth(box);
	var wDiff = 0;
	if (width > 0) {
		wDiff = width / 100;
	}
	var points = [];
	var n = box.getNorthEast().lat();
	var s = box.getSouthWest().lat();
	var e = box.getNorthEast().lng();
	var w = box.getSouthWest().lng();
	if (s != -90) {
		var sNew = s + hDiff;
		s = sNew;
	} else {
		s = -85;
	}
	if (n != 90) {
		var nNew = n - hDiff;
		n = nNew;
	} else {
		n = 85;
	}
	var wNew = w + wDiff;
	w = wNew;
	var eNew = e - wDiff;
	e = eNew;
	points.push(new GLatLng(n, w));
	points.push(new GLatLng(c2, w)); // extra center lat point
	points.push(new GLatLng(s, w));
	if (width >= 180) {
		points.push(new GLatLng(s, c));
	}
	points.push(new GLatLng(s, e));
	points.push(new GLatLng(c2, e));  // extra center lat point
	points.push(new GLatLng(n, e));
	if (width >= 180) {
		points.push(new GLatLng(n, c));
	}
	points.push(new GLatLng(n, w));

		//var color = "#666699";
	var color = "#cc0000";
	var weight = 3; // int equal to width in pixels
	var opacity = 0.75; // float from 0 to 1

//		alert("points = "+points);
	return new GPolyline(points, color, weight, opacity);
}


	//parses out coordinates from polygons (TODO no POINT/Line etc support here yet!)	
function jpGetGBoundsForWKT(wkt) {
	var cStart = wkt.lastIndexOf("(") + 1;
	var cEnd = wkt.indexOf(")");
	var coords = wkt.substring(cStart, cEnd);
				
		//alert("splitting["+coords+ "]from["+wkt);
	var points = coords.split(",");
	var sw = points[3];
	var ne = points[1];
	var s = 9999;
	var n = -9999;
	var e = -9999;
	var w = 9999;
	var latLon = sw.split(" ");
	s = parseFloat(latLon[0]);
	w = parseFloat(latLon[1]);
	latLon = ne.split(" ");
	n = parseFloat(latLon[0]);
	e = parseFloat(latLon[1]);
	return new GLatLngBounds(new GLatLng(s, w), new GLatLng(n, e));
}


	// Parses out coordinates from polygons (TODO no POINT/Line etc support here yet!)
	// to determine the bounds for the _set_ of polygons
	// set = Array of wkt polygons
function jpGetGBoundsForAllWKT(set) {

	   // the max extent for the entire SET of coverages
	var setN = -9999;
	var setS = 9999;
	var setE = -9999;
	var setW = 9999;
	for (j = 0; j < set.length; j++) {
		var wkt = set[j];
		// wkt = (slat wlon, slat elon, nlat elon, nlat wlon, slat wlon)
		// wkt = (LL, LR, UR, UL, LL)
		var cStart = wkt.lastIndexOf("(") + 1;
		var cEnd = wkt.indexOf(")");
		var coords = wkt.substring(cStart, cEnd);
				
		//alert("splitting["+coords+ "]from["+wkt);
		var points = coords.split(",");
		for (i = 0; i < points.length; i++) { // should be 5 points
		}
		var sw = points[3];
		var ne = points[1];
		var latLon = sw.split(" ");
		s = parseFloat(latLon[0]);
		w = parseFloat(latLon[1]);
		latLon = ne.split(" ");
		n = parseFloat(latLon[0]);
		e = parseFloat(latLon[1]);
		setW = Math.min(w, setW);
		setE = Math.max(e, setE);
		setS = Math.min(s, setS);
		setN = Math.max(n, setN);
	}
	return new GLatLngBounds(new GLatLng(setS, setW), new GLatLng(setN, setE));
}


	
	// sets position and zoom level appropriately so that box is visible on the map
	// map = GMap2
	// box = GLatLngBounds
function mhFocusOnBox(map, box) {
	
		// Just for testing purposes
	var lat = box.getCenter().lat();
	var lng = box.getCenter().lng();
		//alert("setting center to "+lat+", "+lng);
	var boxCenter = box.getCenter();
	map.setCenter(boxCenter, 2); // need to zoom in a bit first
	var viewbox = map.getBounds();
	if (!viewbox.containsBounds(box)) {
			//alert("map does NOT contain cov bounds");

			//zoom out to show polygon
		var lastZoomLevel = 9999;
		var zoomLevel = map.getZoom();
		while ((!viewbox.containsBounds(box)) && (zoomLevel != lastZoomLevel)) {
				//alert("too close, zooming out ("+zoomLevel+")");
			lastZoomLevel = zoomLevel;
			map.setCenter(boxCenter, zoomLevel - 1);
			viewbox = map.getBounds();
			zoomLevel = map.getZoom();
		}

			// if map is centered more to the north or more to the south, because
			// of mercator projection, it will appear as though it is not centered.
			// Try to adjust to accomodate this.
		var lastCenter = new GLatLng(-999, -999, true);
		var center = boxCenter;
		while ((!viewbox.containsBounds(box)) && (center != lastCenter) && (center.lat() != 0)) {
			lastCenter = center;
			var lastCenterLat = lastCenter.lat();
			if (lastCenterLat > 0) {
				lastCenterLat += 2;
				if (lastCenterLat > 90) {
					break;
				}
			} else {
				lastCenterLat -= 2;
				if (lastCenterLat < -90) {
					break;
				}
			}
			center = new GLatLng(lastCenterLat, lastCenter.lng());
			map.setCenter(center, zoomLevel);
			viewbox = map.getBounds();
			zoomLevel = map.getZoom();
		}
	} else {
			//alert("map DOES contain cov bounds");

			//TODO Check sufficiently zoomed in for box to be visible -need min 30% of view width
		var bWidth = mhGetBoxWidth(box);
		var bHeight = mhGetBoxHeight(box);
		var vWidth = mhGetBoxWidth(viewbox);
		var vHeight = mhGetBoxHeight(viewbox);
		var lastZoomLevel = 9999;
		var zoomLevel = map.getZoom();
		while (((bWidth / vWidth < 0.3) && (bHeight / vHeight < 0.3)) && (zoomLevel != lastZoomLevel) && (zoomLevel > 1)) {
				//alert("too far away, zooming in ("+zoomLevel+")");

				// zoomed in farther usually gives no data, so set limit
			if (zoomLevel > 6) {
				zoomLevel = 10;
				map.setCenter(boxCenter, zoomLevel);
				return;
			}
			lastZoomLevel = zoomLevel;
			map.setCenter(boxCenter, zoomLevel + 1);
			viewbox = map.getBounds();
			vWidth = mhGetBoxWidth(viewbox);
			vHeight = mhGetBoxHeight(viewbox);
			zoomLevel = map.getZoom();
		}
	}
}
	

	// Returns the width of the box in degrees
	// bounds = GLatLngBounds
function mhGetBoxWidth(bounds) {
	var w = bounds.getSouthWest().lng();
	var e = bounds.getNorthEast().lng();
	if (w > e) {
			//alert("crosses int dateline w="+w+" and e="+e)
		var rtOfIntLine = e + 180;
		var ltOfIntLine = 180 - w;
			//alert("box width = "+( rtOfIntLine + ltOfIntLine));
		return rtOfIntLine + ltOfIntLine;
	}
	return e - w;
}


	// Returns the height of the box in degrees
	// bounds = GLatLngBounds
function mhGetBoxHeight(bounds) { 
	//alert(bounds);
	var s = bounds.getSouthWest().lat();
	var n = bounds.getNorthEast().lat();
	return n - s;
}


	// formulates the proper syntax for a WKT POLYGON
function formulateWKT(south, west, north, east) {
	    
	
	
		// syntax = POLYGON ((N W,N E,S E,S W,N W))
	var string = "POLYGON ((";
	string += north + " " + west + ",";
	string += north + " " + east + ",";
	string += south + " " + east + ",";
	string += south + " " + west + ",";
	string += north + " " + west + "))";
	return string;
}
function goToCov(south, west, north, east, map) {
	var wkt = formulateWKT(south, west, north, east);
	var bounds = jpGetGBoundsForWKT(wkt); // now a GLatLngBounds
	mhFocusOnBox(map, bounds);
}


