var CauseNA = "NOT AVAILABLE";

    if (GBrowserIsCompatible()) 
	{
		var gmarkers = [];
		var gicons = [];

		active = new GIcon();
		active.image = "active.png";
		active.iconSize = new GSize(10, 15);
		active.iconAnchor = new GPoint(4, 4);
		active.infoWindowAnchor = new GPoint(4, 4);
		/*Icon.printImage = "active.gif";
        Icon.mozPrintImage = "active.gif";*/
		gicons["active"] = new GIcon(active);
		
		out = new GIcon();
		out.image = "out.png";
		out.iconSize = new GSize(10, 15);
		out.iconAnchor = new GPoint(4, 4);
		out.infoWindowAnchor = new GPoint(4, 4);
		/*Icon.printImage = "mymarkerie.gif";
        Icon.mozPrintImage = "mymarkerff.gif";*/
		gicons["out"] = new GIcon(out);
		
		today = new GIcon();
		today.image = "new.png";
		today.iconSize = new GSize(10, 15);
		today.iconAnchor = new GPoint(4, 4);
		today.infoWindowAnchor = new GPoint(4, 4);
		/*Icon.printImage = "mymarkerie.gif";
        Icon.mozPrintImage = "mymarkerff.gif";*/
		gicons["new"] = new GIcon(today);
		
     	// A function to create the marker and set up the event window
        function createMarker(point,label, date, cause, status,statusdate, size, category) 
		{
        	var marker = new GMarker(point,gicons[category]);


			switch(cause)
			{
				case "NOT AVAILBLE":
					cause = CauseNA;
					break;
					
				default:
					break;
			}





var msg= ""+label+" <br><strong>Confirmed Date: </strong>"+date+"<br><strong>Probable Cause: </strong>"+cause+" <br><strong>Status: </strong>"+status+"<br><strong>Status Updated: </strong>"+statusdate+"<br><strong>Size (ha): </strong>"+size+" "




       		// === Store the category and name info as a marker properties ===
        	marker.mycategory = category;
        	marker.mylabel = label;
			
        	GEvent.addListener(marker, "click", function() 
			{
          		marker.openInfoWindowHtml(msg);
        	});
			
        	gmarkers.push(marker);
       		return marker;
      	}
		
     	// == shows all markers of a particular category, and ensures the checkbox is checked ==
		function show(category) 
		{
			for (var i=0; i<gmarkers.length; i++) 
			{
				if (gmarkers[i].mycategory == category) 
		  		{
            		gmarkers[i].show();
			    }
        	}
        // == check the checkbox ==
        	document.getElementById(category+"box").checked = true;
      	}

      // == hides all markers of a particular category, and ensures the checkbox is cleared ==
      	function hide(category) 
	  	{
			for (var i=0; i<gmarkers.length; i++) 
			{
          		if (gmarkers[i].mycategory == category) 
		  		{
            		gmarkers[i].hide();
          		}
		    //gmarkers.sort();
        	}
        	// == clear the checkbox ==
        	document.getElementById(category+"box").checked = false;
        	// == close the info window, in case its open on a marker that we just hid
        	map.closeInfoWindow();
      	}

        // == a checkbox has been clicked ==
		function boxclick(box,category) 
	  	{
			if (box.checked) 
			{
          		show(category);
        	} 
			else 
			{
          		hide(category);
        	}
        // == rebuild the side bar
		//sortSidebar();
        makeSidebar();
		
      }

      function myclick(i) 
	  {
		GEvent.trigger(gmarkers[i],"click");
      }

//var dictionary = [];

// == rebuilds the sidebar to match the markers currently displayed ==
		function makeSidebar()
		{
			var html = "";

			for (var i=0; i<gmarkers.length; i++) 
			{
				if (!gmarkers[i].isHidden()) 
				{
					html += '<a href="javascript:myclick(' + i + ')">' + gmarkers[i].mylabel + '</a><br>';
				}
			}
			document.getElementById("side_bar").innerHTML = html;
		}
	  
	  	//TODO: sort all data in the sidebar
	  	function sortSidebar()
	  	{
	  		//gmarkers.sort();
	  	}

	  		//----------------------districts
			
	   // var kml = new GGeoXml("http://www.web2.mnr.gov.on.ca/MNR/affmb/fire/Interactivemap/districts.kml");
    var kml = new GGeoXml("http://firelab.forestry.utoronto.ca/fbkb/Test/districts.kml");
      //map.addOverlay(kml);

      // create the map
      var map = new GMap2(document.getElementById("map"));
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.setCenter(new GLatLng(50,-87),5);

	//out of fire region boundary

		// Read the data
		GDownloadUrl("fire.xml", function(doc) 
		{

			var xmlDoc = GXml.parse(doc);
			var markers = xmlDoc.documentElement.getElementsByTagName("marker");
			for (var i = 0; i < markers.length; i++) 
			{
				// obtain the attribues of each marker
				var lat = parseFloat(markers[i].getAttribute("lat"));
				var lng = parseFloat(markers[i].getAttribute("lng"));
				var point = new GLatLng(lat,lng);
				var label = markers[i].getAttribute("label");
				var category = markers[i].getAttribute("category");
				var size = markers[i].getAttribute("size");
				var cause = markers[i].getAttribute("cause");
				var status = markers[i].getAttribute("status");
				var statusdate = markers[i].getAttribute("statusdate");
				var date = markers[i].getAttribute("date");
				//var district = markers[i].getAttribute("district");
				//var region = markers[i].getAttribute("region");
				var type = markers[i].getAttribute("type");
				// create the marker
				var marker = createMarker(point,label, date, cause, status, statusdate, size,  category);
				 if (type!="PB")
				{
				map.addOverlay(marker);
			}
			}
		
			//-------------calculations of new fires---------------
			// == show or hide the categories initially ==
			show("active");
			hide("out");
			show("new");
			// == create the initial sidebar ==
			makeSidebar();
			//sortSidebar();
		});
}