var FormView = Class.create({
  initialize: function() {
//    Event.observe('itinerary_form', 'submit', this.submit.bindAsEventListener(this));
//    Event.observe('search_button', 'click', this.submit.bindAsEventListener(this));

    if ( itinerariesFormTimeModeShort) {
      if ($('time_mode_swapper')) {
        Event.observe('time_mode_swapper', 'click', this.swapTimeModeShort.bindAsEventListener(this));
      }
    } else {
      if ($$(".departure_or_arrival a").size()==1) {
        var element = $$(".departure_or_arrival a").first();
        Event.observe(element, 'click', this.swapTimeModeDetailed.bindAsEventListener(this));
      }
    }

    if ($('invert'))
      $('invert').observe('click', this.onInvert.bindAsEventListener(this));

    autocompleter_indicator = null;
    if ($('spinner'))
      autocompleter_indicator = 'spinner';

    this.mapView = null;
    this.autocompleters = $H({});
    ['from', 'to' ].each(function(fromOrTo) {
      this.autocompleters.set(fromOrTo, new Ajax.Autocompleter(fromOrTo + "String",
                             fromOrTo + 'String_choices',
                             geocoding,
                            {
                              paramName: 'search_string',
                              minChars: 2,
                              frequency: 0.4,
                              indicator:autocompleter_indicator,
                              method:'get',
                              afterUpdateElement: this.setHiddenFields.bind(this),
                              select:'name'
                            }));

      // the from and to input must also observe the 'keydown' event
      // to reset the hidden fields as soon as their value change
      Event.observe(fromOrTo + "String", 'keydown', this.fromOrToChanged.bindAsEventListener(this));

    }.bind(this));
  },

  fromOrToChanged: function(event) {
    var fromOrTo = event.element().id.sub('String', '');
    if (this.autocompleters.get(fromOrTo).changed)
      this.resetHiddenFileds(fromOrTo);
  },

  resetHiddenFileds: function(fromOrTo) {
    $(fromOrTo + 'Id').value = "";
    $(fromOrTo + 'Type').value = "address";
    $(fromOrTo + 'Lat').value = "";
    $(fromOrTo + 'Lng').value = "";
  },

  inputFromOrTo: function(input) {
    return $(input).id.sub('String', '');
  },

  setHiddenFields: function(input, li) {
    var fromOrTo = this.inputFromOrTo(input);
    var parts = $(li).id.split('_');
    var id = parts.pop();
    var type = parts.join('_');
    $(fromOrTo+'Type').value = type;
    $(fromOrTo+'Id').value = id;
    $(fromOrTo+'Lat').value = parseFloat($(li).lat);
    $(fromOrTo+'Lng').value = parseFloat($(li).lng);
  },

  onInvert: function(event) {
    var fromString = $F('fromString');
    var fromType   = $F('fromType');
    var fromId     = $F('fromId');
    var fromLat    = $F('fromLat');
    var fromLng    = $F('fromLng');
    var toString   = $F('toString');
    var toType     = $F('toType');
    var toId       = $F('toId');
    var toLat      = $F('toLat');
    var toLng      = $F('toLng');

    $('fromString').value = toString;
    $('fromType').value   = toType;
    $('fromId').value     = toId;
    $('fromLat').value    = toLat;
    $('fromLng').value    = toLng;
    $('toString').value   = fromString;
    $('toType').value     = fromType;
    $('toId').value       = fromId;
    $('toLat').value      = fromLat;
    $('toLng').value      = fromLng;

    event.stop();
    return false;
  },

  swapTimeModeShort: function(event) {
    Event.stop(event);
    var current_mode = $('itinerary_time_mode').value;
    var new_mode = $w('departure_on arrival_on').without(current_mode).first();

    $$('#time_mode_swapper span.' + current_mode).first().addClassName('hide');
    $$('#time_mode_swapper span.' + new_mode).first().removeClassName('hide');

    $('itinerary_time_mode').value = new_mode;
  },

  swapTimeModeDetailed: function(event) {
    Event.stop(event);
    var current_mode = $('itinerary_time_mode').value;
    var new_mode = $w('departure_on arrival_on').without(current_mode).first();
    $('itinerary_time_mode').value = new_mode;
    
    var new_mode_name = $$(".departure_or_arrival a").first().innerHTML;
    var current_mode_name = $$(".departure_or_arrival span").first().innerHTML;
    $$(".departure_or_arrival a").first().update( current_mode_name.toLowerCase());
    $$(".departure_or_arrival span").first().update( new_mode_name.capitalize());
  },

  setExtremity: function(way, stop_type, stop_id, stop_string, lat, lng) {
    var orientation = way == "departure" ? "from" : "to";
    $(orientation+'String').value = stop_string;
    $(orientation+'Type').value   = stop_type;
    $(orientation+'Id').value     = stop_id;
    $(orientation+'Lat').value    = lat;
    $(orientation+'Lng').value    = lng;
  }
});




