

function moveMode(postcard_id){

  clusterer.removeMarkers();
  map.clearOverlays();
  
  $('#loading').hide();
  
  var url = '/view/postcard/' + postcard_id
  
  
  // get the current location of the card
  jQuery.get(url, function(data){
    // the lat and lng is encoded in a comment at the top of the response... 
    var latlng_array = data.match(lat_lng_match)[0].split(",")
    
    var white_icon = new GIcon(G_DEFAULT_ICON);
    white_icon.image = "images/pushpins/webhues/216.png";
    
    var current_latlng = new GLatLng(latlng_array[0], latlng_array[1]);
    
    // add an icon where the current loc is
    map.addOverlay(new GMarker(current_latlng, white_icon, true));
    
    // do we already have a suggested position?
    var new_latlng = suggested_latlng();
    if (new_latlng) {
      // zoom us around the markers
      var bounds = new GLatLngBounds();
      bounds.extend(current_latlng);
      bounds.extend(new_latlng);
      map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
      
      place_dragable_at(new_latlng, postcard_id);
    }
    else {
      // put it at the current loc
      var marker = place_dragable_at(current_latlng, postcard_id);
      marker.openInfoWindowHtml("<p><b>Correct this postcard's location</b></p><p>Drag the icon to where it should be.</p><p>Enter a reason below and then submit your correction.</p>");
    }
    
    jQuery.get("/view/suggestion_form", function(data){
      $('#bottom_panel').html(data);
      document.forms[0]['suggested_location[postcard_id]'].value = postcard_id;
      if (new_latlng) 
        setLatLngOnForm(new_latlng);
    });
  });
  
}

function setLatLngOnForm(new_latLng){
  document.forms[0]['location[latitude]'].value = new_latLng.lat();
  document.forms[0]['location[longitude]'].value = new_latLng.lng();
  $('suggestion_submit').disabled = "";
}

function suggested_latlng(){
  var match = window.location.hash.match(/movecard-(\d+)-lat([-\d\.]+).lng([-\d\.]+)/);
  if (match) {
    marker = new GLatLng(match[2], match[3]);
    marker.postcard_id = match[1];
    return marker;
  }
  else     
    return null;
}

function place_dragable_at(latLng, postcard_id){
  var dragable = new GMarker(latLng, {
    draggable: true,
    dragCrossMove: false,
    zIndexProcess: zIndex
  });
  
  GEvent.addListener(dragable, "dragstart", function(new_latLng){
    map.closeInfoWindow();
  });
  
  GEvent.addListener(dragable, "dragend", function(new_latLng){
    setLatLngOnForm(new_latLng);
  });
  
  map.addOverlay(dragable);
  
  return dragable;
}

// we always want this marker in front
function zIndex(marker){
  return GOverlay.getZIndex(marker.getPoint().lat()) + 1;
}

function submit_moved_card(form){

  $('#bottom_panel').html('<div style="float: right; padding: 15px;"><span>Thank you. Your change has been submitted.</span></div>');
  
	jQuery.post("/view/submit_suggestion", $(form).serialize(), function(data){
		      // wait a second...     
      setTimeout(function(){
        $('#bottom_panel').html(data);
        loadPostcards(0);
      }, 2000);

	});
}
