function updateMap() {
  var bounds = map.getBounds();
  var southWest = bounds.getSouthWest();
  var northEast = bounds.getNorthEast();
 
  // Send an AJAX request for our locations
  new Ajax.Request('/markers.js', {
    method:'get',
    parameters: {sw: southWest.toUrlValue(), ne: northEast.toUrlValue()},
    onSuccess: function(transport){
      // Remove markers outside of our maps boundaries.
      if(markers.length > 0){
        removeMarkersOutsideOfMapBounds();
      }
 
      // Add our new markers to the map (unless they are already on the map.)
      var json = transport.responseText.evalJSON();
      json.each(function(i) {
        id = i.id;
        if(!markers[id] || markers[id] == null){
          // Marker doesnt exist, add it.
          markers[id] = createMarker(i);
          map.addOverlay(markers[id]);
        }
      });      
    }
  });
}
 
function createMarkerClickHandler(marker, situation) {
  return function() {
    if (situation.url != '') {
      title = '<a href="' + situation.url + '" target="_blank" class="enlace_titulo">' + situation.title + '</a>';
    } else {
      title = '<strong>' + situation.title + '</strong>';
    }
    marker.openInfoWindowHtml(
      '<div>' + title + '<br/>' + situation.description + '</div>'
    );
    return false;
  };
}
 
function createMarker(situation) {
  var latlng = new GLatLng(situation.latitude, situation.longitude);
  markerIcon.image = "http://www.adondevivir.com/images/marker_categories/" + situation.marker_category.filename
  markerOptions = { icon:markerIcon };
  var marker = new GMarker(latlng, markerOptions);
  var handler = createMarkerClickHandler(marker, situation);
  GEvent.addListener(marker, "click", handler);
  return marker;
}
 
function removeMarkersOutsideOfMapBounds() {
  for(i in markers) {
    if(i > 0 && markers[i] && !map.getBounds().containsLatLng(markers[i].getLatLng())) {
      map.removeOverlay(markers[i]);
      markers[i] = null;
    }
  }
}
