var myAppId = '/guid/9202a8c04000641f8000000005b7ad68';
var urlTestArgs = {}; // no args needed for testing

// TEMPLATE:sample_header.js
// Written by Will Moffat for Metaweb Technologies
mjt.error_reporting.developer = "Will";
mjt.error_reporting.email     = "will_mjt_bugs@hamstersoup.com";
mjt.error_reporting.enabled   = true; // mjt errors will be reported
mjt.error_reporting.app_id = myAppId; // create your own topic on Freebase for your app
mjt.sanitytest.set_query( urlTestArgs ); 
// END:sample_header.js

mjt.sanitytest.verify = function() {
    return (
	$('#map').find('*').length > 100 &&          // a complex map is loaded
	$('#top:contains(Robert Cook)').length==1  // and robert is on the map
    )};


function init() {
    groupByLocation();
    loadMap();
    addLocations();
    mjt.run('top');
}


function addLocations() {
    $.each(id2place, function() { //this = place
      var point = new GLatLng(this.geolocation.latitude , this.geolocation.longitude);
      map.addOverlay(new GMarker(point));
    });
}

var id2place = {};
function groupByLocation() {
    $.each(q.result, function() { // this = person
      var place = this.place_of_birth;
      if (!locations[place.id]) {
	 // we found a new location
	 locations[place.id] = [this];
	 id2place[place.id]  = place;
      } else {
	 locations[place.id].push(this);
      }
    });
}

// Not used!
function moveMapToPlace(placeId) {
   if (!placeId) { return; }
   var place = id2place[placeId];
   map.panTo(new GLatLng(place.geolocation.latitude, place.geolocation.longitude));
}

var map;

function loadMap() {
   map = new GMap2(document.getElementById("map"));
   map.addControl(new GSmallMapControl());

   map.setCenter(new GLatLng(37.4419, -122.1419), 3);
}
