// JavaScript Documen
  if (GBrowserIsCompatible()) {
		
	var map = null;
	var cluster = null;
	//var om = null;

	window.onload=function(){window.onload;initialize()}; 
	window.onunload=function(){window.onunload;GUnload()}; 	

	function initialize(){
	
		map = new GMap2(document.getElementById("map")); //creates map	
		map.setCenter(new GLatLng(39.999035,-75.124157), 10, G_NORMAL_MAP); //sets center and map type
		map.addControl(new GSmallMapControl()); //adds pan and zoom control
		map.addControl(new GScaleControl()); //adds scale
		//zoom levels
		G_NORMAL_MAP.getMinimumResolution = function () { return 10 }; //sets min zoom level
		G_NORMAL_MAP.getMaximumResolution = function () { return 15 }; //sets max zoom level
		
		//city outline kml
		var city_poly = new GGeoXml("http://www.phillywatersheds.org/kml/City_Line.kml");
		map.addOverlay(city_poly); //adds city limits layer
		var dc = new GGeoXml("http://www.phillywatersheds.org/kml/Darby_Cobbs_Creek_Watershed.kml");
		var de = new GGeoXml("http://www.phillywatersheds.org/kml/Delaware_River_Direct_Watershed.kml");
		var pp = new GGeoXml("http://www.phillywatersheds.org/kml/Pennypack_Creek_Watershed.kml");
		var pq = new GGeoXml("http://www.phillywatersheds.org/kml/Poquessing_Creek_Watershed.kml");
		var sh = new GGeoXml("http://www.phillywatersheds.org/kml/Schuylkill_River_Watershed.kml");
		var tf = new GGeoXml("http://www.phillywatersheds.org/kml/Tookany_Tacony_Frankford_Creek_Watershed.kml");
		var wi = new GGeoXml("http://www.phillywatersheds.org/kml/Wissahickon_Creek_Watershed.kml");
		map.addOverlay(dc);
		map.addOverlay(de);
		map.addOverlay(pp);
		map.addOverlay(pq);
		map.addOverlay(sh);
		map.addOverlay(tf);
		map.addOverlay(wi);
		
		//loading message
		//var om = new OverlayMessage(document.getElementById('map'),"#FFFFFF","#a5c2ff");
		//om.Set('Loading...');
					
		//clusterer object
		clusterer = new Clusterer(map);	
		//override default cluster icon
		clusterer.SetIcon(icon_cluster_RB)
		clusterer.SetMinMarkersPerCluster(25)
		
		//calls php which generates xml file of barrel list, calls addBarrels which parses xml file
		GDownloadUrl("http://www.phillywatersheds.org/php/rainbarrel/rb_genMapList.php?list=master_rb_list", addBarrels); 
	}
	
	//function clearOverlay(){ om.Clear() }

	//custom rain barrel icon
	var iconRB = new GIcon(); 
	iconRB.image = 'http://www.phillywatersheds.org/img/rb/marker/rb_marker.png';
	iconRB.iconSize = new GSize(24, 24);
	iconRB.shadowSize = new GSize(0, 0);
	iconRB.iconAnchor = new GPoint(16, 24);
	iconRB.infoWindowAnchor = new GPoint(15, 1);	
	
	//custom rain barrel icon
	var icon_cluster_RB = new GIcon(); 
	icon_cluster_RB.image = 'http://www.phillywatersheds.org/img/rb/marker/rb_cluster_marker.png';
	icon_cluster_RB.iconSize = new GSize(48, 48);
	icon_cluster_RB.shadowSize = new GSize(0, 0);
	icon_cluster_RB.iconAnchor = new GPoint(24, 24);
	icon_cluster_RB.infoWindowAnchor = new GPoint(24, 1);	
	
	var geocoder = new GClientGeocoder(); //geocoder object
	var barrel_count = 0;
	
	//parses out xml file and grabs id (for use later) and address (for geocoding barrel)
	//then calls getAddress to geocode barrel
	function addBarrels(data){
  				
		var xml = GXml.parse(data);
  		var barrels = xml.documentElement.getElementsByTagName("barrel");
		
		barrel_count = barrels.length;
		
		for (var i = 0; i < barrels.length; i++) {
    	
			var id = barrels[i].getAttribute("ID");
			var lat = barrels[i].getAttribute("lat");
			var lng = barrels[i].getAttribute("lng");
			
			var search =  "";

			//if a barrel has never been geocoded, its lat/lng will be 0/0, so pass it into the geocoding function
			if(lat == 0.000000 || lng == 0.000000){
				getAddress(search,id);
			}else{ //if barrel has been geocoded, it will have coordinates, and just needs to have a marker created.
				createMarker(lat,lng);
			}
			
  		}
		
		var gallons_saved = (barrel_count * 54) * 64;
	
		document.getElementById("rb_count").innerHTML = addCommas(barrel_count);
		document.getElementById("gallons").innerHTML = addCommas(gallons_saved);
		//window.setTimeout(clearOverlay, 2000);
			
	}

	
	//geocodes barrel (called from addBarrels) and then calls createNewMarker to create marker for map
	//createNewMarker creates a marker based on the geocoding response, then saves the lat longs into a mySQL database so that the 
	//barrel doesn't have to be geocoded again.
	function getAddress(address,id){
		//alert(address);
		geocoder.getLocations(address, function createNewMarker(response,address){
		
		if(response.Status.code = 200){
			var barrel = response.Placemark[0];
			var point = new GLatLng(barrel.Point.coordinates[1],barrel.Point.coordinates[0]);
			var gmOpts = {icon:iconRB,clickable:false};
			var marker = new GMarker(point,gmOpts);	
			map.addOverlay(marker);
			
			//save lat long values in mySQL db
			GDownloadUrl('rb_saveLatLng.php?ID=' + id + '&lat='+barrel.Point.coordinates[1]+'&lng='+barrel.Point.coordinates[0],nonSense);
		}
		
		
	});
	}
	
	//adds barrel marker to map based on lat/lng fields retrieved from XML
	function createMarker(lat,lng) {
		point = new GLatLng(lat,lng);
		var gmOpts = {icon:iconRB,clickable:false};
		var marker = new GMarker(point,gmOpts);	
		clusterer.AddMarker(marker,"");
	}
	
	//adds commas to a number and returns a string
	function addCommas(nStr)
	{
		nStr += '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	}

	function nonSense(){}
	
	}else {
      alert("Sorry, Google Maps is not compatible with this browser");
    }
	