
// init the geocoder
var geocoder = new GClientGeocoder();

// Handle geocoding for standard node locations.
// Takes a map object, and a location field name prefix.
function gmap_input_node_location_automap(map, prefix){
  if($(prefix + 'street').value &&
     $(prefix + 'city').value &&
     $(prefix + 'province').value &&
     $(prefix + 'country').value){
    // If we have all necissary input, try to geocode.
    gmap_input_automap(map, prefix + 'name',
      prefix + 'street', prefix + 'city',
      prefix + 'province', prefix + 'country',
      prefix + 'latitude', prefix + 'longitude');
  }
}

// Generalized form input geocoding. This takes the map object, and the ID's
// of input form elements.
function gmap_input_automap(map, place_name, street,
    city, province, country,
    latitude, longitude){
  var address = '';
  var address_parts = new Array();
  var point_latitude = null;
  var point_longitude = null;
  if($(street).value){
    address_parts.push($(street).value);
  }
  if($(city).value){
    address_parts.push($(city).value);
  }
  if($(province).value){
    var province_value = $(province).value;
    province_value = province_value.replace(/^[a-z]+-/, '');
    address_parts.push(province_value);
  }
  if($(country).value){
    address_parts.push($(country).value);
  }
  // Geocode and plot the point.
  address = address_parts.join(', ');
  geocoder.getLatLng(address,
    function(point) {
      if (!point) {
        alert(address + ' could not be found on the map. Please make sure you have entered the full street name, including the "St." or "Av." following the name.');
      } else {
        if(thispoint){
          map.removeOverlay(thispoint);
        }
        map.setCenter(point);
        map.clearOverlays();
        var marker = new GMarker(point);
        thispoint = marker;
        map.addOverlay(marker);
        marker.openInfoWindowHtml(address);
        $('gmap-latitude').value = point.y;
        $('gmap-longitude').value = point.x;
        map.panTo(point)
        window.location.hash = 'location-street';
      }
    });
}


// A hack for lock type selections...

function check_not_locked_selects(){
  if(($('edit-field_how_was_the_lock_defeated-key').value == 'Not locked.')
    ){
    x = $('edit-field_what_type_of_lock_were_yo-key');
    x.value = 'No Lock';
    x = $('edit-field_what_was_the_bike_locked_-key');
    x.value = 'Bike not locked.';
    x = $('edit-field_how_was_the_bike_locked-key');
    x.value = 'Not locked.';
  }
}

