
	var map = null;
	var stations = new Array();


	function initMap() {
		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("mmsmap"));
			map.addMapType(G_PHYSICAL_MAP);
			map.setMapType(G_PHYSICAL_MAP)
			map.addControl(new GScaleControl(),new GControlPosition(G_ANCHOR_BOTTOM_RIGHT,new GSize(8,20)));
			var useZoom = 8;
			if (typeof HWmap != 'undefined' && HWmap == 'large') {
				map.addControl(new GLargeMapControl ());
				useZoom = 7;
			}
			else map.addControl(new GSmallMapControl ());
			map.addControl(new GMapTypeControl())


			//=======Restricting the range of zoom levels

			if (typeof tlLat != 'undefined' && typeof tlLon != 'undefined' && typeof brLat != 'undefined' && typeof brLon != 'undefined') {
//				var bounds = new GLatLngBounds(new GLatLng(brLat+0, tlLon+0),new GLatLng(tlLat+0, brLon+0));

				var bounds = new GLatLngBounds;

				bounds.extend(new GLatLng(tlLat+0, tlLon+0));
				bounds.extend(new GLatLng(brLat+0, brLon+0));


				if (typeof hwvUseLat != 'undefined' && typeof hwvUseLon != 'undefined') {
					map.setCenter(new GLatLng(hwvUseLat+0, hwvUseLon+0));
					if (typeof hwvAddmarker != 'undefined') map.addOverlay(new GMarker(new GLatLng(hwvUseLat+0, hwvUseLon+0)));
				}
				else {
					map.setCenter(bounds.getCenter());
				}
				map.setZoom(map.getBoundsZoomLevel(bounds));
			}
			else {
				map.setCenter(new GLatLng(hwvUseLat+0, hwvUseLon+0), useZoom);
			}





			//Add a move listener to restrict the bounds range
			GEvent.addListener(map, "moveend", function() {
				updateStations();
			});

			updateStations();


		}
		 // display a warning if the browser was not compatible
	    else {
	      alert("Sorry, the Google Maps API is not compatible with this browser");
	    }
	}

	function updateStations() {

		var stageTypes = new Array();
		stageTypes[0] = 'Old Data';
		stageTypes[1] = 'Normal';
		stageTypes[2] = 'Near Flood Stage';
		stageTypes[3] = 'Minor Flooding';
		stageTypes[4] = 'Moderate Flooding';
		stageTypes[5] = 'Major Flooding';


		var icons = new Array();


		icons[0] = new GIcon();
		icons[0].image = "/images/rivers/grey_marker.png";
		icons[0].shadow = null;
		icons[0].iconSize = new GSize(11, 11);
		icons[0].shadowSize = null;
		icons[0].iconAnchor = new GPoint(5, 5);
		icons[0].infoWindowAnchor = new GPoint(5, 5);


		icons[1] = new GIcon();
		icons[1].image = "/images/rivers/green_circle.png";
		icons[1].shadow = null;
		icons[1].iconSize = new GSize(8, 8);
		icons[1].shadowSize = null;
		icons[1].iconAnchor = new GPoint(5, 5);
		icons[1].infoWindowAnchor = new GPoint(5, 5);

		icons[2] = new GIcon();
		icons[2].image = "/images/rivers/yellow_circle.png";
		icons[2].shadow = null;
		icons[2].iconSize = new GSize(9, 9);
		icons[2].shadowSize = null;
		icons[2].iconAnchor = new GPoint(5, 5);
		icons[2].infoWindowAnchor = new GPoint(5, 5);

		icons[3] = new GIcon();
		icons[3].image = "/images/rivers/orange_circle.png";
		icons[3].shadow = null;
		icons[3].iconSize = new GSize(10, 10);
		icons[3].shadowSize = null;
		icons[3].iconAnchor = new GPoint(5, 5);
		icons[3].infoWindowAnchor = new GPoint(5, 5);

		icons[4] = new GIcon();
		icons[4].image = "/images/rivers/red_circle.png";
		icons[4].shadow = null;
		icons[4].iconSize = new GSize(12, 12);
		icons[4].shadowSize = null;
		icons[4].iconAnchor = new GPoint(5, 5);
		icons[4].infoWindowAnchor = new GPoint(5, 5);

		icons[5] = new GIcon();
		icons[5].image = "/images/rivers/purple_circle.png";
		icons[5].shadow = null;
		icons[5].iconSize = new GSize(14, 14);
		icons[5].shadowSize = null;
		icons[5].iconAnchor = new GPoint(5, 5);
		icons[5].infoWindowAnchor = new GPoint(5, 5);




		var bounds = map.getBounds();


		var lonSW = bounds.getSouthWest().lng();
		var latSW = bounds.getSouthWest().lat();

		var lonNE = bounds.getNorthEast().lng();
		var latNE = bounds.getNorthEast().lat();

		var xmlURL = '/rivers/gaugesinarea?';
		if (typeof waterbody != 'undefined' && waterbody) xmlURL += "wbody=" + waterbody;
		else  xmlURL += "lat1=" + latNE + "&lon1=" + lonSW + "&lat2=" + latSW + "&lon2=" + lonNE;


		GDownloadUrl(xmlURL, function(data, responseCode) {
			if(data=="" || !(responseCode==200||responseCode==304) ) {
							return;
						}

			map.clearOverlays();


			if (typeof hwvUseLat != 'undefined' && typeof hwvUseLon != 'undefined') {
				if (typeof hwvAddmarker != 'undefined') {
					var useName = (typeof hwvAddName != 'undefined') ? hwvAddName : '';
					marker = new GMarker(new GLatLng(hwvUseLat+0, hwvUseLon+0),{title:useName});

					if (typeof hwvMarkerURL != 'undefined') GEvent.addListener(marker, "click", function() { top.location.href=hwvMarkerURL; });
					map.addOverlay(marker);
				}
			}


		function createMarker(stn,stnname,lat,lon, stageCode, stage, obtime) {
					var stnpos = new GLatLng(lat,lon);
					bounds.extend(stnpos);
					var marker = null;

					if (!stageCode) stageCode = 0;
					stageCode++;

					stageType = stageTypes[stageCode];

					var useName = (stnname) ? stnname + ' - ' : '';
					useName += 'Gauge ' + stn + "\r\n";
					useName += "Stage: " + stage + "ft - " + stageType;

					marker = new GMarker(stnpos,{title:useName,icon:icons[stageCode]});

					GEvent.addListener(marker, "click", function() { top.location.href="http://weather.hamweather.com/rivers/gauge/"+stn+".html"; });
					marker.stationid=stn;
					return marker;
		}


		var xml = GXml.parse(data);
		var stationsxml = new Array();
		if(xml.documentElement) {
	  		stationsxml = xml.documentElement.getElementsByTagName("loc");
		} else {//for Safari
			xml = document.createElement("div");
			xml.innerHTML = data;
			stationsxml = xml.getElementsByTagName("loc");
		}
		var len = stationsxml.length;

		for (var i=0;i<len;i++) {
			stations[i] = createMarker(stationsxml[i].getAttribute("id"), stationsxml[i].getAttribute("name"),  stationsxml[i].getAttribute("lat"), stationsxml[i].getAttribute("lon"),  stationsxml[i].getAttribute("stageCode"),  stationsxml[i].getAttribute("stage"),  stationsxml[i].getAttribute("obtime"));
			map.addOverlay(stations[i]);

		}






		map.savePosition();
		return;
		});




	}





	window.onload=initMap;
	window.onunload=GUnload;



