var map, icon, mapZoom;

var navajoXML,hopiXML,tribesXML,detectsXML,class1XML, controlCls1;
var hmsXML0,hmsXML1,hmsXML2,hmsXML3,hmsXML4,hmsXML5;
var kmlHMS = ['','','','','','',''];
var tribeToggleState = 0;
var classToggleState = 0;
var detectToggleState = 0;
var hmsToggleState = 0;

var hmsPath = "http://www.wrapfets.org/hms/";

var markerSetWF=[], markerSetWFU=[], markerSetRX=[], markerSetNFR=[], markerSetAG=[], markerSetCounty=[], markerSetState=[];
var wfIconCluster ,wfuIconCluster, rxIconCluster ,nfrIconCluster, agIconCluster, coIconCluster, stIconCluster;
var clusterMarkerSetWF, clusterMarkerSetWFU, clusterMarkerSetRX, clusterMarkerSetNFR, clusterMarkerSetAG;

//var kmlDetects = "fireDetectsFETS" + mapDate + ".kml";

//var kmlHMS = "fire" + mapDate + ".kml";

function load() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
	
		map.addMapType(G_PHYSICAL_MAP);

		var mapControl = new GHierarchicalMapTypeControl();
		mapControl.clearRelationships();

		mapControl.addRelationship(G_SATELLITE_MAP, G_HYBRID_MAP, "Labels", false);
		
		if (mapType == "Map") {
			map.setMapType(G_NORMAL_MAP);
		} else if (mapType == "Satellite") {
			map.setMapType(G_SATELLITE_MAP);
		} else if (mapType == "Hybrid") {
			map.setMapType(G_HYBRID_MAP);
		} else {
			map.setMapType(G_PHYSICAL_MAP);
		}

		map.addControl(mapControl);
		map.enableScrollWheelZoom();
			
			//Parse lat/lng from CF variable.  Messy, but it's  the only way.
		map.setCenter(new GLatLng(initCenterLat,initCenterLon), parseInt(initCenterZoom));
		

	//	add an HtmlControl to enable toggling of the ClusterMarker cluster function
	//	see http://googlemapsapi.martinpearman.co.uk/htmlcontrol for more info on HtmlControl
		if (cluster == "on"){
					var html='<div id="cluster_control"><input type="checkbox" checked="True" onclick="toggleClustering()" title="group overlapping markers"/><strong> Enable clustering </strong><a href="javascript:popUp('+"'../../help.cfm#enableclustering'"+')" ><img src="images/help_icon.gif" alt="fd" width="12" height="11" title="Click to view help menu" /></a></div>';
		}else{
			var html='<div id="cluster_control"><input type="checkbox" onclick="toggleClustering()" title="group overlapping markers"/><strong> Enable clustering </strong><a href="javascript:popUp('+"'../../help.cfm#enableclustering'"+')" ><img src="images/help_icon.gif" alt="fd" width="12" height="11" title="Click to view help menu" /></a></div>';
		}
		
		var control=new HtmlControl(html);
		map.addControl(control, new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7,43)));
		
		var html='<div style="padding:3px 3px 3px 3px; background-color:#EEEEEE; border:1px solid gray; font-size: 12px;"><img src="images/CLS1_fws.png" alt="C1A color key" width="13" height="13" /> FWS<br/><img src="images/CLS1_nps.png" alt="C1A color key" width="13" height="13" /> NPS<br/><img src="images/CLS1_usfs.png" alt="C1A color key" width="13" height="13" /> USDA-FS<br/><img src="images/CLS1_native.png" alt="C1A color key" width="13" height="13" /> Native Am.</div>';
		controlCls1 = new HtmlControl(html);
		//map.addControl(controlCls1, new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7,40)));
		
		GEvent.addListener(map, 'zoomend', function() { 
															map.closeInfoWindow();
															returnZoom();
															});


		// Create "tiny" marker icon
		var baseIcon = new GIcon();
		//Eicon.image = "images/fire_icon_bg_small.png";
		baseIcon.iconSize = new GSize(10, 10);
		baseIcon.iconAnchor = new GPoint(5, 5);
		baseIcon.infoWindowAnchor = new GPoint(5,5);
		//create cluster icons
		wfIconCluster = new GIcon();
		wfIconCluster.image = "images/WF_clstr2.png";    //  Thanks Joseph Broach [http://bikeportland.org/closecall/home.php] 
		wfIconCluster.iconSize = new GSize(11, 11);    //  for the elegant cluster icon design.
		wfIconCluster.iconAnchor = new GPoint(5.5, 5.5);
		wfIconCluster.infoWindowAnchor = new GPoint(5.5, 5.5);
		
		rxIconCluster = new GIcon();
		rxIconCluster.image = "images/RX_clstr2.png";
		rxIconCluster.iconSize = new GSize(11, 11);
		rxIconCluster.iconAnchor = new GPoint(5.5, 5.5);
		rxIconCluster.infoWindowAnchor = new GPoint(5.5, 5.5);
		
		nfrIconCluster = new GIcon();
		nfrIconCluster.image = "images/NFR_clstr2.png";
		nfrIconCluster.iconSize = new GSize(11, 11);
		nfrIconCluster.iconAnchor = new GPoint(5.5, 5.5);
		nfrIconCluster.infoWindowAnchor = new GPoint(5.5, 5.5);
		
		agIconCluster = new GIcon();
		agIconCluster.image = "images/AG_clstr2.png";
		agIconCluster.iconSize = new GSize(11, 11);
		agIconCluster.iconAnchor = new GPoint(5.5, 5.5);
		agIconCluster.infoWindowAnchor = new GPoint(5.5, 5.5);
		
		wfuIconCluster = new GIcon();
		wfuIconCluster.image = "images/WFU_clstr2.png";
		wfuIconCluster.iconSize = new GSize(11, 11);
		wfuIconCluster.iconAnchor = new GPoint(5.5, 5.5);
		wfuIconCluster.infoWindowAnchor = new GPoint(5.5, 5.5);
		
		// Create large marker icon
		Sicon = new GIcon();
		Sicon.image = "images/fireIcon2.png";
		Sicon.iconSize = new GSize(20,20);
		Sicon.iconAnchor = new GPoint(10,10);
		Sicon.infoWindowAnchor = new GPoint(10,10);
		Bicon = new GIcon();
		Bicon.image = "images/fireIcon2.png";
		Bicon.iconSize = new GSize(26,26);
		Bicon.iconAnchor = new GPoint(13,13);
		Bicon.infoWindowAnchor = new GPoint(13,13);

		var Path = "http://www.wrapfets.org/mapfiles/";  //"http://www.airsci.net/client230/client230/MapTest/";   
		navajoXML = new GGeoXml(Path+"navajo2009.kml");
		hopiXML = new GGeoXml(Path+"hopi2009.kml");   
		tribesXML = new GGeoXml(Path+"tribes2009.kml");

		//detectsXML = new GGeoXml(Path+kmlDetects);
		
		//hmsXML = new GGeoXml(hmsPath+kmlHMS);
		
		class1XML = new GGeoXml(Path+"class1_16Nov2008.kml");
				
				
		// Our info window content
		var tabDesc = [
			"Burn Info",
			"County Summary",
			"State Summary"
			];
					
		function createInfoTabs(tabData) {
			if (tabData[0]=="event"){
				if (tabData[5]=="National Interagency Fire Center"){
					if (tabData[7] < 0){
						tabData[7] = "0.0"
						tabData[2] = "0.0"
					}
					var infoTabs = 				
						"<table id='mapinfowindow'><caption>"+tabData[8]+" "+tabData[6]+"</caption>"+
										"<tr><th colspan='2'>"+tabData[3]+" - "+tabData[4]+"<tr><th colspan='2'>"+
										"<tr><td>Reported burn days:<td>"+tabData[11]+
										"<tr><td>Total perimeter area:<td>"+tabData[13]+" acre(s)"+
										"<tr><td>Growth in this period:<td>"+tabData[7]+" acre(s)"+
										"<tr><td>PM<sub>2.5</sub> emitted in this period:<td>"+tabData[2]+" ton(s)"+
										"<tr><tr><td>Reporting State:<td>"+tabData[10]+
										"<tr><td>Data Source:<td><a href='http://fam.nwcg.gov/fam-web/' target='_blank'>ICS-209 report</a></table>";

				}else{
					if (tabData[15] > 0){
						if (tabData[7] > 0) {
							var quantLine = "<tr><td>Accomplished piles:<td>"+tabData[15]
						}else{
							var quantLine = "<tr><td>Planned piles:<td>"+tabData[15]
						}
					}else{
						if (tabData[7] > 0) {
							var quantLine = "<tr><td>Accomplished acres:<td>"+tabData[7]
						}else{
							var quantLine = "<tr><td>Planned acres:<td>"+tabData[1]
						}
					}
					if (tabData[10]=="ID" && tabData[5]=="Nez Perce ERWM Air Quality Program"){
					var infoTabs = 				
						"<table id='mapinfowindow'><caption>"+tabData[8]+" "+tabData[6]+"</caption>"+
										"<tr><th colspan='2'>"+tabData[3]+" - "+tabData[4]+"<tr><th colspan='2'>"+
										"<tr><td>Reported burns:<td>"+tabData[11]+
										quantLine+
										"<tr><td>Burns using ERTs:<td>"+tabData[12]+
										"<tr><td>Total PM<sub>2.5</sub> emissions:<td>"+tabData[2]+" tons"+
										"<tr><tr><td>Reporting Tribe:<td>"+"Nez Perce"+
										"<tr><td>Data Source:<td><a href='"+tabData[16]+"' target='_blank'>"+tabData[5]+"</a>"+
										"<tr><td>Last Updated:<td>"+tabData[14]+
										"<tr><th colspan='2'>*Location accurate to within 1 mile"+"</table>";
					
					}else{
					var infoTabs = 				
						"<table id='mapinfowindow'><caption>"+tabData[8]+" "+tabData[6]+"</caption>"+
										"<tr><th colspan='2'>"+tabData[3]+" - "+tabData[4]+"<tr><th colspan='2'>"+
										"<tr><td>Reported burns:<td>"+tabData[11]+
										quantLine+
										"<tr><td>Burns using ERTs:<td>"+tabData[12]+
										"<tr><td>Total PM<sub>2.5</sub> emissions:<td>"+tabData[2]+" ton(s)"+
										"<tr><tr><td>Reporting State:<td>"+tabData[10]+
										"<tr><td>Data Source:<td><a href='"+tabData[16]+"' target='_blank'>"+tabData[5]+"</a>"+
										"<tr><td>Last Updated:<td>"+tabData[14]+"</table>";
					}
				}
				return infoTabs;
			}else if (tabData[0]=="county"){
				var infoTabs = "<table id='mapinfowindow'><caption>"+tabData[3]+"</caption>"+
										"<tr><th colspan='2'>"+tabData[5]+" - "+tabData[6]+"<tr><th colspan='2'>"+
										"<tr><td>Number of burns:<td>"+tabData[4]+
										"<tr><td>Total acres:<td>"+tabData[1]+
										"<tr><td>PM<sub>2.5</sub> emissions:<td>"+tabData[2]+" ton(s)</table>";
				return infoTabs;
			}else{
				var infoTabs = "<table id='mapinfowindow'><caption>"+tabData[3]+"</caption>"+
										"<tr><th colspan='2'>"+tabData[5]+" - "+tabData[6]+"<tr><th colspan='2'>"+
										"<tr><td>Number of events:<td>"+tabData[4]+
										"<tr><td>Total acres:<td>"+tabData[1]+
										"<tr><td>PM<sub>2.5</sub> emissions:<td>"+tabData[2]+" ton(s)</table>";
				return infoTabs;
			}

		}
					
			// Creates a marker at the given point with the given number label
		function createMarker(point, tabData) {
			if(tabData[0]=="event"){
				//icon=baseIcon;
				icon = new GIcon();
				//Eicon.image = "images/fire_icon_bg_small.png";
				icon.iconSize = new GSize(10, 10);
				icon.iconAnchor = new GPoint(5.5, 5.5);
				icon.infoWindowAnchor = new GPoint(5.5, 5.5);
				if(tabData[9]=="WF"){
					icon.image="images/WF_bkgd2.png";
					//set = markerSetWF;
					//tabData[5]="ICS-209";
				}else if(tabData[9]=="RX"){
					icon.image="images/RX_bkgd2.png";
					//set = markerSetRX;
				}else if(tabData[9]=="WFU"){
					icon.image="images/WFU_bkgd2.png";
					//set = markerSetWFU;
					//tabData[5]="ICS-209";
				}else if(tabData[9]=="NFR"){
					icon.image="images/NFR_bkgd2.png";
					//set = markerSetNFR;
				}else if(tabData[9]=="AG"){
					icon.image="images/AG_bkgd2.png";
					//set = markerSetAG;
				}
				title=tabData[8];
			}else if (tabData[0]=="county"){
				icon=Sicon;
				title=tabData[3];
				set = markerSetCounty;
			}else{
				icon=Bicon;
				title=tabData[3];
				set = markerSetState;
			}
			var marker = new GMarker(point,{icon:icon,title:title});
			GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(createInfoTabs(tabData),maxWidth=50);
			});
			if(tabData[0]=="event"){
				if(tabData[9]=="WF"){
					markerSetWF.push(marker);
				}else if(tabData[9]=="RX"){
					markerSetRX.push(marker);
				}else if(tabData[9]=="WFU"){
					markerSetWFU.push(marker);
				}else if(tabData[9]=="NFR"){
					markerSetNFR.push(marker);
				}else{
					markerSetAG.push(marker);
				}
			}
			//set.push(marker);
			if (tabData[0]=="county" || tabData[0]=="state"){
				return marker;
			}
		}
			// Download the data in data.xml and load it on the map. The format we
			// expect is:
			// <markers>
			//   <marker lat="37.441" lng="-122.141"/>
			//   <marker lat="37.322" lng="-121.213"/>
			// </markers>
		//  The following functions are adapted from code by Martin Pearman [http://googlemapsapi.martinpearman.co.uk/] 	

		function ClusterClickWF(args) {
				if(map.getCurrentMapType().getName()=='Map' && map.getZoom()<19){ //max zoom values are hard coded due to unreliability of Google's function
					clusterMarkerSetWF.defaultClickAction=function(){
					map.setCenter(args.clusterMarker.getLatLng(), map.getBoundsZoomLevel(args.clusterMarker.clusterGroupBounds));
					delete clusterMarkerSetWF.defaultClickAction;};
				}
				else if(map.getCurrentMapType().getName()!='Map' && map.getZoom()<20){ //max zoom values are hard coded due to unreliability of Google's function
					clusterMarkerSetWF.defaultClickAction=function(){
					map.setCenter(args.clusterMarker.getLatLng(), map.getBoundsZoomLevel(args.clusterMarker.clusterGroupBounds));
					delete clusterMarkerSetWF.defaultClickAction;};
				}
				else {clusterMarkerSetWF.defaultClickAction=function(){alert('Maximum zoom level reached\nfor this map type!');};}
				var id;
				var html='<div style="height:8em; overflow:auto; width:18em;">'+args.clusteredMarkers.length+' fires near here: <br />';
				for (i=0; i<args.clusteredMarkers.length; i++) {
					html+='<a href="javascript:clusterMarkerSetWF.triggerClick('+args.clusteredMarkers[i].index+')">'+args.clusteredMarkers[i].getTitle()+'</a><br />';
				}
				html+='<br /><a href="" onclick="clusterMarkerSetWF.defaultClickAction(); return false;">Zoom</a> in to uncluster</div>';
					
				//	args.clusterMarker.openInfoWindowHtml(html); //this way results in info window sticking open on zoom
				map.openInfoWindowHtml(args.clusterMarker.getLatLng(), html);
		}
		
		function ClusterClickWFU(args) {
				if(map.getCurrentMapType().getName()=='Map' && map.getZoom()<19){ //max zoom values are hard coded due to unreliability of Google's function
					clusterMarkerSetWFU.defaultClickAction=function(){
					map.setCenter(args.clusterMarker.getLatLng(), map.getBoundsZoomLevel(args.clusterMarker.clusterGroupBounds));
					delete clusterMarkerSetWFU.defaultClickAction;};
				}
				else if(map.getCurrentMapType().getName()!='Map' && map.getZoom()<20){ //max zoom values are hard coded due to unreliability of Google's function
					clusterMarkerSetWFU.defaultClickAction=function(){
					map.setCenter(args.clusterMarker.getLatLng(), map.getBoundsZoomLevel(args.clusterMarker.clusterGroupBounds));
					delete clusterMarkerSetWFU.defaultClickAction;};
				}
				else {clusterMarkerSetWFU.defaultClickAction=function(){alert('Maximum zoom level reached\nfor this map type!');};}
				var html='<div style="height:8em; overflow:auto; width:18em;">'+args.clusteredMarkers.length+' fires near here <br />';
				for (i=0; i<args.clusteredMarkers.length; i++) {
					html+='<a href="javascript:clusterMarkerSetWFU.triggerClick('+args.clusteredMarkers[i].index+')">'+args.clusteredMarkers[i].getTitle()+'</a><br />';
				}
				html+='<br /><a href="" onclick="clusterMarkerSetWFU.defaultClickAction(); return false;">Zoom</a> in to uncluster</div>';
					
				//	args.clusterMarker.openInfoWindowHtml(html); //this way results in info window sticking open on zoom
				map.openInfoWindowHtml(args.clusterMarker.getLatLng(), html);
		}
		
		function ClusterClickRX(args) {
				if(map.getCurrentMapType().getName()=='Map' && map.getZoom()<19){ //max zoom values are hard coded due to unreliability of Google's function
					clusterMarkerSetRX.defaultClickAction=function(){
					map.setCenter(args.clusterMarker.getLatLng(), map.getBoundsZoomLevel(args.clusterMarker.clusterGroupBounds));
					delete clusterMarkerSetRX.defaultClickAction;};
				}
				else if(map.getCurrentMapType().getName()!='Map' && map.getZoom()<20){ //max zoom values are hard coded due to unreliability of Google's function
					clusterMarkerSetRX.defaultClickAction=function(){
					map.setCenter(args.clusterMarker.getLatLng(), map.getBoundsZoomLevel(args.clusterMarker.clusterGroupBounds));
					delete clusterMarkerSetRX.defaultClickAction;};
				}
				else {clusterMarkerSetRX.defaultClickAction=function(){alert('Maximum zoom level reached\nfor this map type!');};}
				var html='<div style="height:8em; overflow:auto; width:18em;">'+args.clusteredMarkers.length+' fires near here <br />';
				for (i=0; i<args.clusteredMarkers.length; i++) {
					html+='<a href="javascript:clusterMarkerSetRX.triggerClick('+args.clusteredMarkers[i].index+')">'+args.clusteredMarkers[i].getTitle()+'</a><br />';
				}
				html+='<br /><a href="" onclick="clusterMarkerSetRX.defaultClickAction(); return false;">Zoom</a> in to uncluster</div>';
					
				//	args.clusterMarker.openInfoWindowHtml(html); //this way results in info window sticking open on zoom
				map.openInfoWindowHtml(args.clusterMarker.getLatLng(), html);
		}
		
		function ClusterClickNFR(args) {
				if(map.getCurrentMapType().getName()=='Map' && map.getZoom()<19){ //max zoom values are hard coded due to unreliability of Google's function
					clusterMarkerSetNFR.defaultClickAction=function(){
					map.setCenter(args.clusterMarker.getLatLng(), map.getBoundsZoomLevel(args.clusterMarker.clusterGroupBounds));
					delete clusterMarkerSetNFR.defaultClickAction;};
				}
				else if(map.getCurrentMapType().getName()!='Map' && map.getZoom()<20){ //max zoom values are hard coded due to unreliability of Google's function
					clusterMarkerSetNFR.defaultClickAction=function(){
					map.setCenter(args.clusterMarker.getLatLng(), map.getBoundsZoomLevel(args.clusterMarker.clusterGroupBounds));
					delete clusterMarkerSetNFR.defaultClickAction;};
				}
				else {clusterMarkerSetNFR.defaultClickAction=function(){alert('Maximum zoom level reached\nfor this map type!');};}
				var html='<div style="height:8em; overflow:auto; width:18em;">'+args.clusteredMarkers.length+' fires near here <br />';
				for (i=0; i<args.clusteredMarkers.length; i++) {
					html+='<a href="javascript:clusterMarkerSetNFR.triggerClick('+args.clusteredMarkers[i].index+')">'+args.clusteredMarkers[i].getTitle()+'</a><br />';
				}
				html+='<br /><a href="" onclick="clusterMarkerSetNFR.defaultClickAction(); return false;">Zoom</a> in to uncluster</div>';
					
				//	args.clusterMarker.openInfoWindowHtml(html); //this way results in info window sticking open on zoom
				map.openInfoWindowHtml(args.clusterMarker.getLatLng(), html);
		}
		
		function ClusterClickAG(args) {
				if(map.getCurrentMapType().getName()=='Map' && map.getZoom()<19){ //max zoom values are hard coded due to unreliability of Google's function
					clusterMarkerSetAG.defaultClickAction=function(){
					map.setCenter(args.clusterMarker.getLatLng(), map.getBoundsZoomLevel(args.clusterMarker.clusterGroupBounds));
					delete clusterMarkerSetAG.defaultClickAction;};
				}
				else if(map.getCurrentMapType().getName()!='Map' && map.getZoom()<20){ //max zoom values are hard coded due to unreliability of Google's function
					clusterMarkerSetAG.defaultClickAction=function(){
					map.setCenter(args.clusterMarker.getLatLng(), map.getBoundsZoomLevel(args.clusterMarker.clusterGroupBounds));
					delete clusterMarkerSetAG.defaultClickAction;};
				}
				else {clusterMarkerSetAG.defaultClickAction=function(){alert('Maximum zoom level reached\nfor this map type!');};}
				var html='<div style="height:8em; overflow:auto; width:18em;">'+args.clusteredMarkers.length+' fires near here <br />';
				for (i=0; i<args.clusteredMarkers.length; i++) {
					html+='<a href="javascript:clusterMarkerSetAG.triggerClick('+args.clusteredMarkers[i].index+')">'+args.clusteredMarkers[i].getTitle()+'</a><br />';
				}
				html+='<br /><a href="" onclick="clusterMarkerSetAG.defaultClickAction(); return false;">Zoom</a> in to uncluster</div>';
					
				//	args.clusterMarker.openInfoWindowHtml(html); //this way results in info window sticking open on zoom
				map.openInfoWindowHtml(args.clusterMarker.getLatLng(), html);
		}
	// End adapted functions
			
		AjaxRequest.get(
			{
			'url': fileInstance
			,'onSuccess': function(req) {
					var xml = GXml.parse(req.responseText);
				//	alert(xml.documentElement.childNodes.length);
					<!---document.forms['simple'].pageSource.value = req.responseText;--->
					var markers = xml.documentElement.getElementsByTagName("marker");
				//	alert(markers.length);

					for (var i = 0; i < markers.length; i++) {		// markers.length
						var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),parseFloat(markers[i].getAttribute("lng")));
						if (filename=="V2MapXMLEvents.cfm"){
							var tabData = [
								"event",
								parseFloat(markers[i].getAttribute("plannedacres")).toFixed(1),
								parseFloat(markers[i].getAttribute("pm25emit")).toFixed(2),
								markers[i].getAttribute("startdate"),
								markers[i].getAttribute("enddate"),
								markers[i].getAttribute("agency"),
								markers[i].getAttribute("typedesc"),
								parseFloat(markers[i].getAttribute("actualacres")).toFixed(1),
								markers[i].getAttribute("site"),
								markers[i].getAttribute("type"),
								markers[i].getAttribute("state"),
								markers[i].getAttribute("events"),
								markers[i].getAttribute("numERTs"),
								parseFloat(markers[i].getAttribute("projacres")).toFixed(1),
								markers[i].getAttribute("modDate"),
								parseFloat(markers[i].getAttribute("piles")).toFixed(0),
								markers[i].getAttribute("url")
							];
							createMarker(point, tabData);
							isEvents = 1;
						}else if(filename=="V2MapXMLSumByCounty.cfm" || filename=="V2MapXMLSumByTribe.cfm"){
							var tabData = [
								"county",
								parseFloat(markers[i].getAttribute("acres")).toFixed(1),
								parseFloat(markers[i].getAttribute("pm25emit")).toFixed(2),
								markers[i].getAttribute("county"),
								markers[i].getAttribute("events"),
								markers[i].getAttribute("startdate"),
								markers[i].getAttribute("enddate")
							];
							map.addOverlay(createMarker(point, tabData));
						}else{
							var tabData = [
								"state",
								parseFloat(markers[i].getAttribute("acres")).toFixed(1),
								parseFloat(markers[i].getAttribute("pm25emit")).toFixed(2),
								markers[i].getAttribute("state"),
								markers[i].getAttribute("events"),
								markers[i].getAttribute("startdate"),
								markers[i].getAttribute("enddate")
							];
							map.addOverlay(createMarker(point, tabData));
						}
					}
					clusterMarkerSetWF = new ClusterMarker(map, { clusterMarkerClick:ClusterClickWF, clusterMarkerTitle:'Click to see info about %count wildfires', clusterMarkerIcon:wfIconCluster} );
					
					clusterMarkerSetWFU = new ClusterMarker(map, { clusterMarkerClick:ClusterClickWFU, clusterMarkerTitle:'Click to see info about %count WFU fires', clusterMarkerIcon:wfuIconCluster} );
					
					clusterMarkerSetRX = new ClusterMarker(map, { clusterMarkerClick:ClusterClickRX, clusterMarkerTitle:'Click to see info about %count prescribed fires', clusterMarkerIcon:rxIconCluster} );
						
					clusterMarkerSetNFR = new ClusterMarker(map, { clusterMarkerClick:ClusterClickNFR, clusterMarkerTitle:'Click to see info about %count rangeland fires', clusterMarkerIcon:nfrIconCluster} );
					
					clusterMarkerSetAG = new ClusterMarker(map, { clusterMarkerClick:ClusterClickAG, clusterMarkerTitle:'Click to see info about %count agricultural fires', clusterMarkerIcon:agIconCluster} );
					setClusters();
				}
			,'timeout':10000
			,'onTimeout':function(req){alert('Your query timed out. Submit again, or try choosing a smaller date range. If the problem persists, contact the site administrator immediately.'); 
				}
			}
		);

		GEvent.addListener(map, "moveend", function() {
					center = map.getCenter();
					//setClusters();
					document.getElementById("message").innerHTML = center.toString(); //+kmlDetects;
			});
		}
		
		if (detect == "on") {
			//map.addOverlay(detectsXML);
			//detectToggleState = 1;
			//map.addOverlay(hmsXML);
			hmsToggleState = 1;
			
			//document.mappo.detect.checked = True;
		}
		manageDetects(true);
		
		if (tribe == "on") {
			map.addOverlay(navajoXML);
			map.addOverlay(hopiXML);
			map.addOverlay(tribesXML);
			tribeToggleState = 1;
			//document.mappo.tribe.checked = True;
		}
		if (class1 == "on") {
			map.addOverlay(class1XML);
			map.addControl(controlCls1, new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7,75)));
			classToggleState = 1;
			//document.mappo.class1.checked = True;
		}
		if (cluster == "on"){
			toggleClustering()
		}


		




		//	map.addOverlay(navajoXML);
		//	map.addOverlay(hopiXML);
		//	map.addOverlay(tribesXML);
		
		//	map.addOverlay(class1XML);
} // end load()

function returnZoom(){
	var mapCenter = map.getCenter().toString();
	var cl = mapCenter.split(",");
	var initLat = parseFloat(cl[0].substring(1,9));
	var initLng = parseFloat(cl[1]);
	mapZoom = map.getZoom();
	document.mappo.vMapCenterLat.value=initLat;
	document.mappo.vMapCenterLon.value=initLng;
	document.mappo.vMapZoom.value=mapZoom;

	var mapType = map.getCurrentMapType();
	document.mappo.vMapType.value = mapType.getName();
}

function advanceDay(direction){
	var NewStartDate=new Date(SDate);
	var NewEndDate=new Date(EDate);
	NewStartDate.setDate(NewStartDate.getDate()+direction);
	NewEndDate.setDate(NewEndDate.getDate()+direction);
//The following ugly statement fixes IE6,7 incompatibility with CF
	startString=String("0" +(NewStartDate.getMonth() + 1)).slice(-2)+"/"+String("0" +NewStartDate.getDate()).slice(-2)+"/"+NewStartDate.getFullYear();
	endString=String("0" +(NewEndDate.getMonth() + 1)).slice(-2)+"/"+String("0" +NewEndDate.getDate()).slice(-2)+"/"+NewEndDate.getFullYear();
	document.mappo.StartDate.value=startString;
	document.mappo.EndDate.value=endString;
}


function manageOverlays(overlay) {
	if (overlay == "tribe") {
		if (tribeToggleState == 1) {
			map.removeOverlay(navajoXML);
			map.removeOverlay(hopiXML);
			map.removeOverlay(tribesXML);
			tribeToggleState = 0;
	
		} 
		else {
			map.addOverlay(navajoXML);
			map.addOverlay(hopiXML);
			map.addOverlay(tribesXML);
			tribeToggleState = 1;
		}
	} else if (overlay == "class1") {
		if (classToggleState == 1) {
			map.removeOverlay(class1XML);
			map.removeControl(controlCls1);
			classToggleState = 0;
		} 
		else {
			map.addOverlay(class1XML);
			map.addControl(controlCls1, new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7,75)));
			classToggleState = 1;
		}
	} else {
		if (hmsToggleState == 1) {
			//map.removeOverlay(detectsXML);
			//detectToggleState = 0;
			//map.removeOverlay(hmsXML);
			hmsToggleState = 0;
			manageDetects(false);
		} 
		else {
			//map.addOverlay(detectsXML);
			//detectToggleState = 1;
			//map.addOverlay(hmsXML);
			hmsToggleState = 1;
			manageDetects(false);
		}
	}
}

function manageDetects(init){
	//eval('map.addOverlay(hmsXML'+0+');');

	var NewStartDate=new Date(SDate);
	var NewEndDate=new Date(EDate);
	var diff = (NewEndDate.getTime() - NewStartDate.getTime()) / 1000 / 60 / 60 / 24;
	
	if (diff > 6) {
		diff = 6;
	}
	if (init) {
		hmsXML0 = new GGeoXml(hmsPath + "fire" + mapStartDate + ".kml");
		for (i = 1; i <= diff; i++) {
			NewStartDate.setDate(NewStartDate.getDate()+1);
			var dateString = NewStartDate.getFullYear() + String("0" +(NewStartDate.getMonth() + 1)).slice(-2) + String("0" +NewStartDate.getDate()).slice(-2);
			eval('hmsXML'+i+' = new GGeoXml(hmsPath+"fire" + dateString + ".kml");');
		}
	}
	for (i=0; i <= diff; i++) {
		document.getElementById("message").innerHTML+='';
		if (hmsToggleState == 1) {
			eval('map.addOverlay(hmsXML'+i+');');
		}else if (hmsToggleState == 0) {
			if (!init) {
				eval('map.removeOverlay(hmsXML'+i+');');
			}
		}
	}
}

function setClusters() {
	//document.getElementById("message").innerHTML = markerSetRX.length + " " + markerSetWFU.length + " " + markerSetWF.length;
	clusterMarkerSetWF.addMarkers(markerSetWF);
	clusterMarkerSetWF.refresh(true);
	
	clusterMarkerSetWFU.addMarkers(markerSetWFU);
	clusterMarkerSetWFU.refresh(true);

	clusterMarkerSetRX.addMarkers(markerSetRX);
	clusterMarkerSetRX.refresh(true);
	
	clusterMarkerSetNFR.addMarkers(markerSetNFR)
	clusterMarkerSetNFR.refresh(true);
	
	clusterMarkerSetAG.addMarkers(markerSetAG)
	clusterMarkerSetAG.refresh(true);
	if (cluster != "on"){
	toggleClustering();
	}
}

function toggleClustering() {
	clusterMarkerSetWF.clusteringEnabled=!clusterMarkerSetWF.clusteringEnabled;
	clusterMarkerSetWF.refresh(true);	//	true required to force a full update of the markers - otherwise the update would occur next time that the map is zoomed or the active markers change
	clusterMarkerSetWFU.clusteringEnabled=!clusterMarkerSetWFU.clusteringEnabled;
	clusterMarkerSetWFU.refresh(true);
	clusterMarkerSetRX.clusteringEnabled=!clusterMarkerSetRX.clusteringEnabled;
	clusterMarkerSetRX.refresh(true);
	
	clusterMarkerSetAG.clusteringEnabled=!clusterMarkerSetAG.clusteringEnabled;
	clusterMarkerSetAG.refresh(true);
	clusterMarkerSetNFR.clusteringEnabled=!clusterMarkerSetNFR.clusteringEnabled;
	clusterMarkerSetNFR.refresh(true);
	if (clusterMarkerSetWF.clusteringEnabled) {
		document.mappo.clust.value = "on";
	}else{
		document.mappo.clust.value = "";
	}
}

function setCenter(view){
	if(view=="Alaska"){
		map.setCenter(new GLatLng(64.83025,-152.27051), 4);
	//	viewCurrent="Alaska";
	}else if(view=="Lower48"){
		map.setCenter(new GLatLng(40.813809, -110.874023), 5);
	//	viewCurrent="Lower48";
	}else{
		map.setCenter(new GLatLng(46.293815562333684,-116.444091796875), 9);
	//	viewCurrent="NezPerce";
	}
	//document.message.open()	
	//document.getElementById("message").innerHTML=view;
}

