if (GBrowserIsCompatible()) {
 
	var map = null;
	var om = null;

	window.onload=function(){window.onload;initialize()}; 
	window.onunload=function(){window.onunload;GUnload()}; 
	
	function initialize() {
		//loading message	
		om = new OverlayMessage(document.getElementById('map'),"#FFFFFF","#0096d9");
		om.Set('Loading...');
		
		map = new GMap2(document.getElementById("map")); 
		map.addControl(new GSmallMapControl()); 
		map.addControl(new GScaleControl()); 
		map.setCenter(new GLatLng(40.001935,-75.125157), 11, G_PHYSICAL_MAP);
		
		G_PHYSICAL_MAP.getMinimumResolution = function () { return 11 }; 
		G_PHYSICAL_MAP.getMaximumResolution = function () { return 15 };
		
		var city_line = new GGeoXml("http://www.phillywatersheds.org/kml/City_Line.kml");
		map.addOverlay(city_line); 
		var csoShed = new GGeoXml("http://www.phillywatersheds.org/kml/Combined_Sewer_Area.kml");
		map.addOverlay(csoShed);
		
		GDownloadUrl("/castdata/hybrid_out.txt", process_reg);
		
		
		// Add a move listener to restrict the bounds range
		GEvent.addListener(map, "move", function() {
			checkBounds();
			showOutfallWm();
		});

	}
	
			
	function clearOverlay(){ om.Clear() }

	//set icon characteristics for reg icon
	var baseIcon = new GIcon(G_DEFAULT_ICON);
	baseIcon.iconSize=new GSize(24,24);
	baseIcon.shadowSize=new GSize(0,0);
	baseIcon.iconAnchor=new GPoint(12,16);
	
	//outfall location marker, smaller than the base icon
	var outfallIcon = new GIcon(G_DEFAULT_ICON);
	outfallIcon.iconSize=new GSize(10,10);
	outfallIcon.shadowSize=new GSize(0,0);
	outfallIcon.iconAnchor=new GPoint(5,5);
 
	//set icon characteristics for rain icon
	var baseRainIcon = new GIcon(G_DEFAULT_ICON);
	baseRainIcon.iconSize=new GSize(20,20);
	baseRainIcon.shadowSize=new GSize(0,0);
	baseRainIcon.iconAnchor=new GPoint(10,10);

	//set icon characteristics for search result icon
	var geocodeIcon = new GIcon(G_DEFAULT_ICON);
	geocodeIcon.iconSize=new GSize(32,32);
	geocodeIcon.shadow = "/img/csocast/marker/shadow-geocodemarker.png";
	geocodeIcon.shadowSize=new GSize(48,32);
	geocodeIcon.iconAnchor=new GPoint(19,1);
	geocodeIcon.infoWindowAnchor=new GPoint(18,30)
	
	//no outfall watermark
	var outfallWm = new GIcon(G_DEFAULT_ICON);
	outfallWm.image = "/img/csocast/marker/noOutfallWatermark.png";
	outfallWm.iconSize = new GSize(250,250);
	outfallWm.shadowSize = new GSize(0,0);

	var gmOpts = {icon:outfallWm,clickable:false};
	
	//set up outfall watermarks
	var wissWm = new GMarker(new GLatLng(40.0521,-75.2181),gmOpts);
	var poqWm = new GMarker(new GLatLng(40.10038,-74.9726),gmOpts);
		
	//icon for past overflows
	var curOverIcon = new GIcon(baseIcon,"/img/csocast/marker/cur_over.png");
	//icon for no overflows
	var pastOverIcon = new GIcon(baseIcon,"/img/csocast/marker/over.png");
	//icon for currently overflows
	var noOverIcon = new GIcon(baseIcon,"/img/csocast/marker/noover.png");
	//icon for no data
	var outLoc = new GIcon(outfallIcon,"/img/csocast/marker/of_marker.png");
	//rain gauge icon when raining
	var RainIcon = new GIcon(baseRainIcon,"/img/csocast/marker/rain_marker.png");
	//rain gauge icon when not raining
	var NoRainIcon = new GIcon(baseRainIcon,"/img/csocast/marker/no_rain_marker.png");
	//rain gauge icon when rain gauge is broken
	var NoRainDataIcon = new GIcon(baseRainIcon,"/img/csocast/marker/no_rain_data_marker.png");
	//address marker
	var Arrow = new GIcon(geocodeIcon,"/img/marker/geocodemarker.png");

	
	function createMarker(point,html,icon) {
        var marker = new GMarker(point,icon);
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
        return marker;
	}
  
	//stores reg name and marker object, used for zooming to regs
	markerArray = new Array(166);
	for (i=0; i<markerArray.length; i++){
		markerArray[i]=new Array(2);
	}
	//array to return index of markers
	markerArrayIndex = new Array(166);

	//regulator array, for storage of regs and their coordinates, and distance from the geocode marker
	var regArr = new Array(166);
	for (i=0; i<regArr.length; i++){
		regArr[i]=new Array(7);
	}
	//array to return index of outfalls
	var regArrIndex = new Array(166); 

	//processes regulator monitor file
	process_reg = function(doc) {
    lines = doc.split("\n");
    
		for (var i=0; i<lines.length; i++) {
			if (lines[i].length > 1) {
				//now split each line by the comma character
				parts = lines[i].split(",");
				var outfall_name = parts[0];
				var status_msg = parts[1];
				var monitor_update = parts[2];
				var lat = parseFloat(parts[3]);
				var lng = parseFloat(parts[4]);
				var status = parts[5];
				var int_sys = parts[6];
				var waterbody = parts[7];
				var street = parts[9];
				var point = new GLatLng(lat,lng);
				
				var rec_water = {
					"D":"Delaware River",
					"C":"Cobbs Creek",
					"S":"Schuylkill River",
					"W":"Wissahickon Creek",
					"T":"Tacony Creek",
					"F":"Frankford Creek",
					"P":"Pennypack Creek"};
	            
				var data_html = "<div id=\"infowindow\" style='width: 350px'>" + 
							"<span id=\"iw_title\">Outfall Information</span>" + "<br/>" + 
							"<hr>" +
							"<b>Outfall ID: </b>" + outfall_name + "<br/>" + 
							"<b>Status: </b>" + status_msg + "<br/>" + 
							"<b>Data Available Through: </b>" + monitor_update + "<br/>" +
							"<b>Location: </b>" + street + "<br/>" + 
							"<b>Receiving Water: </b>" + rec_water[waterbody] + 
							"</div>";
			
				var no_data_html = "<div id=\"infowindow\" style='width: 350px'>" + 
							"<span id=\"iw_title\">Outfall Information</span>" + "<br/>" + 
							"<hr>" +
							"<b>Outfall ID: </b>" + outfall_name + "<br/>" + 
							"<b>Location: </b>" + street + "<br/>" + 
							"<b>Receiving Water: </b>" + rec_water[waterbody] +
							"</div>"; 
			
				
				//add all the regulators to an array, this array is used in distance calculations
				if(outfall_name != "Data last updated" || outfall_name != "System Msg"){
							regArr[i][0] = outfall_name;
							regArr[i][1] = lat;
							regArr[i][2] = lng;
							regArr[i][4] = status;
							regArr[i][5] = int_sys;
							regArr[i][6] = waterbody;		
						}
		
				//create the marker for the regulator
				if (status == 4){
					var marker = createMarker(point,data_html,curOverIcon);
					map.addOverlay(marker);
					markerArray[i][0] = outfall_name;
					markerArray[i][1] = marker;
				}else if (status == 3){		
					var marker = createMarker(point,data_html,pastOverIcon);
					map.addOverlay(marker);
					markerArray[i][0] = outfall_name;
					markerArray[i][1] = marker;
				}else if (status == 2 || status == 0){		
					var marker = createMarker(point,no_data_html,outLoc);
					map.addOverlay(marker);
					markerArray[i][0] = outfall_name;
					markerArray[i][1] = marker;
				}else if (status == 1){		
					var marker = createMarker(point,data_html,noOverIcon);
					map.addOverlay(marker);
					markerArray[i][0] = outfall_name;
					markerArray[i][1] = marker;
				}else if (outfall_name == "Data last updated"){
					document.getElementById("last_updated").innerHTML = "<b>" + outfall_name + ":" + status_msg + "</b>";
				}else if (outfall_name == "System Msg"){
					document.getElementById("systemMsg").innerHTML = "<marquee><b>" + status_msg + "</b></marquee>";
					document.getElementById("systemMsg").style.visibility = "visible";
				}
				}
			
			}
		  
		//copies markerArray	
		for(var z=0; z < regArr.length; z++){
			markerArrayIndex[z] = markerArray[z][0];
		}
	
		GDownloadUrl("/castdata/rain_out.txt", process_rain);
	
	}
  
	function process_rain(doc){
		lines = doc.split("\n");
			for (var x=0; x<lines.length; x++) {
				if (lines[x].length > 1) {
          			//now split each line by the comma character
          			parts = lines[x].split(",");
          			var rg_name = parts[0];
					var status_msg = parts[1];
					var rg_update = parts[2]
					var lat = parseFloat(parts[3]);
          			var lng = parseFloat(parts[4]);
          			var rainfall = parts[5];
					var intensity = parts[6];
					
					var html = "<div id=\"infowindow\" style='width: 350px'>" + 
							"<span id=\"iw_title\">Rain Gauge Information</span>" + "<br/>" + 
							"<hr>" +
							"<b>Rain Gauge ID: </b>" + rg_name + "<br/>" + 
							//"<b>Status: </b>" + status_msg + "<br/>" + 
							"<b>Rainfall in past 48 hours: </b>" + rainfall + "\"<br/>" + 
							//"<b>Peak Intensity: </b>" + intensity + "in/2.5 min<br/>" + 
							"<b>Data Available Through: </b>" + rg_update + 
							"</div>";
					
					var point = new GLatLng(lat,lng);
					
				// create the marker, 
				if (rainfall > 0){
					var marker = createMarker(point,html,RainIcon);
					map.addOverlay(marker);
				}else if (rainfall == 0){		
					var marker = createMarker(point,html,NoRainIcon);
					map.addOverlay(marker);
				}
	}
	}
		//remove loading msg
		window.setTimeout(clearOverlay, 5000);
	
	}
	
	//bounding box for searches and panning
	var allowedBounds = new GLatLngBounds(new GLatLng(39.8480851,-75.395736), new GLatLng(40.15211,-74.863586));		

	//geocoder object
	var geocoder = new GClientGeocoder();
	
	//stores the search result marker
	var resultArray = new Array();
	
	//geocoding function
	function AddressSearch(address) {
	
		address = address + ", Philadelphia, PA";
	
		//if another marker already exists on the map, remove it
		if(resultArray[0]){
			map.removeOverlay(resultArray[0]);
		}
		geocoder.getLatLng(
    		address,
    		function(point) {
      		if (!point) {
        		alert(address + " not found");			
			} else {
        			var marker = createMarker(point,address,Arrow);
				if(allowedBounds.contains(marker.getPoint())){
					var marker = createMarker(point,address + getClosestRegs(marker),Arrow);
					map.setCenter(point, 14);
					map.addOverlay(marker);
					marker.openInfoWindowHtml(address + getClosestRegs(marker));
					resultArray[0] = marker;
				}else{
					alert("Please restrict your search to the Philadelphia area.");
				}
      		}
    		}
  	);
	}
	
} else {
  alert("Sorry, Google Maps is not compatible with this browser");
}
// Portions of this Javascript is from code provided by the
// Blackpool Community Church Javascript Team  
// http://econym.googlepages.com/index.htm
//]]>