
// object to encapsulate UserLocation functionality

var UserLocation = {
  
  // set this somewhere if you're using a controller that behaves like user
  // locations but isn't at the user locations path
  controller_path: '/user_locations',

  street_name_search_options:
  {
    onSuccess: function(transport, streets)
    {
      // zoom to the street coords as described by server
      Global.map.center(streets[0].lat, streets[0].lng, 17);
      
      // update the 'found street' box and register form handler
      $('street_match').update(transport.responseText);

      $('street').observe('change',
                          function(event) {
                            selected_street = streets.find(function(s) { return s.id == $F('street') });
                            Global.map.center(selected_street.lat, selected_street.lng, 17);
                          });
      
      smform = $('street_match_form');
      smform.observe('submit',
                     function(e) {
                       e.stop();
                       // send the form with AJAX instead
                       smform.request({ onSuccess: UserLocation.update_table });
                     });
    },
    
    
    onFailure: function(transport, message)
    {
      $('street_match').update("<fieldset><h2>Results</h2><p>"+message+"</p></fieldset>");
    }
  },
  
  // call the destroy method remotely
  // and update the table
  //
  destroy: function(id)
  {
    new Ajax.Request(UserLocation.controller_path + '/' + id, {
      method: 'post', parameters: {'_method': 'delete'}, onSuccess: function(transport) {
                         // there are two table rows to remove
                         $('ul_' + id).next().remove();
                         $('ul_' + id).remove();
                       }
      });
  },
  
  update_table: function(transport)
  {
    $('results').update(transport.responseText);
  }
};


// Street name search used by you eye and you know
//
var StreetNameSearch = Class.create({
  initialize: function(options)
  {
    self.form = $('street_finder');
    
    // stop street name form from being submitted
    self.form.observe('submit', function(e) { e.stop() });
    
    // check the street name box periodically 
    new PeriodicalExecuter(function(pe)
                           {
                             street_name = $F('street_name');
                             
                             if ((! street_name) && $('street_match')) { // if there's no value in the box
                               $('street_match').update('');
                             }
                             
                             if (street_name && (pe.last_value != street_name)) { // if changed
                               // display loading status
                               $('street_match').update('<p class="searching">Searching&hellip;</p>');
                               
                               // store this value for future comparison
                               pe.last_value = street_name;
                               
                               // submit street finder form with AJAX
                               $('street_finder').request({
                                 method: 'get',
                                 onSuccess: options.onSuccess,
                                 onFailure: options.onFailure
                               });
                             }
                           }, 
                           2); // seconds to wait between polls
  }
});
