var RADIUS_1_PIC = "/images/camp_g.png"
var RADIUS_2_PIC = "/images/camp_y.png"
var RADIUS_3_PIC = "/images/camp_r.png"
var RADIUS_PIC_SHADOW = "/images/camp_shadow.png"

function deleteXML(file){
	//var fso = new ActiveXObject("Scripting.FileSystemObject");
	//fso.DeleteFile(file, true);
}

if (GBrowserIsCompatible()) {
      // this variable will collect the html which will eventualkly be placed in the sidebar
      //var sidebar_html = "";
    
      // arrays to hold copies of the markers and html used by the sidebar
      // because the function closure trick doesnt work there
      var gmarkers = [];
      var htmls = [];
      var i = 0;
      // arrays to hold variants of the info window html with get direction forms open
      var to_htmls = [];
      var from_htmls = [];
	  
	  // Create a base icon for all of our markers that specifies the
	  // shadow, icon dimensions, etc.
	  var baseIcon = new GIcon();
	  baseIcon.shadow = RADIUS_PIC_SHADOW;
	  baseIcon.iconSize = new GSize(24, 26);
	  baseIcon.shadowSize = new GSize(37, 26);
	  baseIcon.iconAnchor = new GPoint(9, 26);
	  baseIcon.infoWindowAnchor = new GPoint(9, 2);
	  baseIcon.infoShadowAnchor = new GPoint(18, 25);


      // A function to create the marker and set up the event window
      function createMarker(point,name,html,iconPicture) {
	  	// Create a lettered icon for this point using our icon class
		var icon = new GIcon(baseIcon);
		icon.image = iconPicture;
		var marker = new GMarker(point, icon);

	  
	  
        //var marker = new GMarker(point);
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
		
		        // The info window version with the "to here" form open
        to_htmls[i] = html + '<br>Directions: <b>To here</b> - <a href="javascript:fromhere(' + i + ')">From here</a>' +
           '<br>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
           '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT">' +
           '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + 
                  // "(" + name + ")" + 
           '"/>';
        // The info window version with the "to here" form open
        from_htmls[i] = html + '<br>Directions: <a href="javascript:tohere(' + i + ')">To here</a> - <b>From here</b>' +
           '<br>End address:<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
           '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT">' +
           '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() +
                  // "(" + name + ")" + 
           '"/>';
        // The inactive version of the direction info
        html = html + '<br>Directions: <a href="javascript:tohere('+i+')">To here</a> - <a href="javascript:fromhere('+i+')">From here</a>';

        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
        // save the info we need to use later for the sidebar
        gmarkers[i] = marker;
        htmls[i] = html;
        // add a line to the sidebar html
        //sidebar_html += '<a href="javascript:myclick(' + i + ')">' + name + '</a><br>';
        i++;
        return marker;
      }


      // This function picks up the click and opens the corresponding info window
      function myclick(i) {
        gmarkers[i].openInfoWindowHtml(htmls[i]);
      }
	  // functions that open the directions forms
      function tohere(i) {
        gmarkers[i].openInfoWindowHtml(to_htmls[i]);
      }
      function fromhere(i) {
        gmarkers[i].openInfoWindowHtml(from_htmls[i]);
      }
	  
	  function loadmap(fileName, zipLatitude, zipLongitude, radius, radius2, zoomlevel){
		  // create the map
		  var map = new GMap2(document.getElementById("googlemap"));
		  map.addControl(new GLargeMapControl());
		  map.addControl(new GMapTypeControl());
		  map.setCenter(new GLatLng(zipLatitude, zipLongitude), zoomlevel);
	  
		  // Read the data from example.xml
		  var request = GXmlHttp.create();
		  request.open("GET", fileName, true);
		  //request.open("GET", "/map/googlecampmapback.xml", true);
		  //request.open("GET", "/files/maps/F76E6DFE-B07E-451E-B4D0-47B33C18A433.xml", true);
		  request.onreadystatechange = function() {
			if (request.readyState == 4) {
			  var xmlDoc = request.responseXML;
			  // obtain the array of markers and loop through it
			  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 html = markers[i].getAttribute("html");
				var label = markers[i].getAttribute("label");
				var distance = parseInt(markers[i].getAttribute("distance"));
				
				if (distance < parseInt(radius)){
					var campIcon = RADIUS_1_PIC;
				}
				else if (distance > parseInt(radius) && distance < parseInt(radius2)){
					var campIcon = RADIUS_2_PIC;
				}
				else{
					var campIcon = RADIUS_3_PIC;
				}
				
				// create the marker
				var marker = createMarker(point,label,html,campIcon);
				map.addOverlay(marker);
			  }
			  // put the assembled sidebar_html contents into the sidebar div
			  //document.getElementById("sidebar").innerHTML = sidebar_html;
			}
		  }
		  request.send(null);
		}      
    }

    else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }