

/* 
 * Load the postcards
 * 
 * This reads the hash to figure out what it should be loading.
 * 
 */
function loadPostcards(person_id){

  // always close the info window, if one's open (it looks odd otherwise)
  map.closeInfoWindow();
	
	hide_people_grid();

  // show the bouncing icon...
  $('#loading').show();
	$('#showing_text').html('Loading postcards...');
  
  // clear any existing overlays from the map
  clusterer.removeMarkers();
  map.clearOverlays();
  
	var vals = decomposeHash();
	
	person_id = vals['person'];
	country_id = vals['country'];
  
  var url = '/view/postcards_json';
	
	var extras = new Array();
	
  if (person_id) 
    extras.push('person=' + person_id);
  if (country_id) 
    extras.push('country=' + country_id);
  
	url += "?"+joinStrings(extras, "&");
	
	jQuery.getJSON(url, function(locations){
    var added = addMarkersFromJSON(locations, clusterer, map);
    $('#loading').hide();
      
    if (!person_id) {
      $('#showing_text').html('Showing '+added+' postcards from everyone');
    }
  });
	
  
  if (person_id > 0) {
  
    // load the postcard path for this person
    jQuery.getJSON('/view/person_json/' + person_id, function(person){
      map.addOverlay(new GPolyline.fromEncoded({
        color: "#3333cc",
        weight: 8,
        points: person.encoded_points,
        levels: person.encoded_levels,
        zoomFactor: person.zoomFactor,
        numLevels: person.numLevels
      }));
      
      // deal with null postcard_km, which means we don't know
      if (person.postcard_km) {
        $('#showing_text').html('Showing ' + person.placed_card_count + ' postcards covering ' + person.postcard_km + 'km from ' + person.name);
      } else {
        $('#showing_text').html('Showing ' + person.placed_card_count + ' postcards from ' + person.name);
      }
    });
    
  }
}

function addMarkersFromJSON(locations, clusterer, map){
  var markers = new Array();
  
	var bounds = new GLatLngBounds();
	
  var count = 0;
  
  for (i = 0; i < locations.length; i++) {
    var p_loc = locations[i];
    var marker;
    
    if (p_loc.postcards.length == 0) {
      // no cards here, ignore the location
    
    } else if (p_loc.postcards.length == 1) {
      // just one postcard here
      var postcard = p_loc.postcards[0];
      marker = create_postcard_marker(p_loc.latitude, p_loc.longitude, icons[postcard.icon_id], postcard.comment, postcard.id);
      
    } else {
      // there are more than one postcards in this location
      marker = create_location_marker(p_loc.latitude, p_loc.longitude, p_loc.id);
    }
    markers.push(marker);
		
		// expand our bounds to include the newly added marker
		bounds.extend(marker.getLatLng());
    
    count += p_loc.postcards.length;
  }
  
  clusterer.addMarkers(markers);
  clusterer.refresh(true);
	
	if(!bounds.isEmpty()) {
		zoomMapTo(bounds);
	}
  
  return count;
}

function zoomMapTo(bounds) {
	var zoomLevel = map.getBoundsZoomLevel(bounds);
	
	// zoom in one level - it's nicer
	//zoomLevel -= 1;

  // don't zoom too far in - it just looks silly
	if(zoomLevel > 16) {
		zoomLevel = 16;
	}
	
	map.setZoom(zoomLevel);
	map.setCenter(bounds.getCenter());
}

function create_postcard_marker(lat, lng, icon, person_name, id){
  var latlng = new GLatLng(lat, lng);
  var marker = new GMarker(latlng, {
    icon: icon,
    title: person_name
  });
  
  if (!placingCard) {
    GEvent.addListener(marker, "click", function(){
      setHashValue("postcard", id);
    });
  }
  
  return marker;
}

function create_location_marker(lat, lng, id){
  var latlng = new GLatLng(lat, lng);
  var marker = new GMarker(latlng, {
    icon: lotsIcon
  });
  
  if (!placingCard) {
    GEvent.addListener(marker, "click", function(){
			jQuery.get('view/location/' + id, function(data){
        marker.openInfoWindowHtml(data);
      });
    });
  }
  
  return marker;
}
