//<![CDATA[
if (GBrowserIsCompatible()) {
  var gmarkers = [];
  var gicons = [];

    gicons["threatened"] = new GIcon(G_DEFAULT_ICON,"/files/edc/images/icons/threatenedbuilding.png");
    gicons["lost"] = new GIcon(G_DEFAULT_ICON,"/files/edc/images/icons/lostbuilding.png");
    gicons["saved"] = new GIcon(G_DEFAULT_ICON,"/files/edc/images/icons/savedbuilding.png");

  // A function to create the marker and set up the event window
  function createMarker(point,name,html,category) {
    if(!category) {
	category = "threatened";
    }
    var marker = new GMarker(point,gicons[category]);
    // === Store the category and name info as a marker properties ===
    marker.mycategory = category;                                 
    marker.myname = name;
    GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml("<div class=\"edcinfowindow\">"+html+"</div>");
    });
    var markerid  = gmarkers.length;
    jQuery("div#"+category+"links ul").append('<li><a href="javascript:myclick('+markerid+');">'+name+"</a></li>");
    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;
	document.getElementById(category+"links").style.display='';
  }

  // == 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();
      }
    }
    // == clear the checkbox ==
    document.getElementById(category+"box").checked = false;
	document.getElementById(category+"links").style.display='none';
    // == 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);
    }

  }

  function myclick(i) {
    GEvent.trigger(gmarkers[i],"click");
  }

  // create the map
  var map = new GMap2(document.getElementById("map"));
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());
  map.setCenter(new GLatLng(38.898748, -77.037684), 13);


  // Read the data
  GDownloadUrl("/scripts/ppcmap.php", function(doc) {
    var xmlDoc = GXml.parse(doc);
    var markers = xmlDoc.documentElement.getElementsByTagName("location");
      
    for (var i = 0; i < markers.length; i++) {
      // obtain the attribues of each marker
      if(markers[i].getElementsByTagName("lat") && markers[i].getElementsByTagName("lat").length) {
        var lat = parseFloat(markers[i].getElementsByTagName("lat")[0].textContent);
        var lng = parseFloat(markers[i].getElementsByTagName("lon")[0].textContent);
      } else {
	continue;
      }
	var myprop = markers[i].parentNode;

      var point = new GLatLng(lat,lng);
      //var address = markers[i].getAttribute("address");
      //var name = markers[i].getAttribute("name");
	var name = myprop.getElementsByTagName("title")[0].textContent;
	var address = markers[i].getElementsByTagName("street")[0].textContent;
	var html = "<h1>"+name+"</h1>";
	html += "<h2>"+address+"</h2>";
      html += markers[i].parentNode.getElementsByTagName("body")[0].textContent;

      var category;
      category = "threatened";
      if( myprop.getElementsByTagName("taxonomy").length && myprop.getElementsByTagName("taxonomy")[0].getElementsByTagName("name").length) {
      	category = myprop.getElementsByTagName("taxonomy")[0].getElementsByTagName("name")[0].textContent;
      }
      // create the marker
      var marker = createMarker(point,name,html,category);
      map.addOverlay(marker);
    }

    // == show or hide the categories initially ==
    show("threatened");
    show("lost");
    hide("saved");

  });
}

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

function showSummary(i) {
	//Uses Google Maps API Http Request
	var request = GXmlHttp.create();
	var obj = document.getElementById("property_summary");
	request.open("GET","./worker/summary.php?pid="+i, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			obj.innerHTML = request.responseText;
			window.scroll(0,500);
		}
	}
	request.send(null);
}
// This Javascript is based on code provided by the
// Blackpool Community Church Javascript Team
// http://www.commchurch.freeserve.co.uk/   
// http://econym.googlepages.com/index.htm

//]]>
