var autocompleter = null;
var lineListDisplay = null;

function indicateResultsLoading(stationName) {
    $('station_name').update(stationName);
    $$('.connected_lines li').each(function(li) {
      li.remove();
    });
    $('line_spinner').show();
    $('lines_for_station').show();
}


Event.observe(window, 'load', function() {

  $$('#search_form .submit')[0].disable();
  $('stop_area_search_user_input').activate();

  lineListDisplay = new LineListDisplay();
  
  autocompleter = new Ajax.Autocompleter("stop_area_search_user_input",
                     'ac_choices',
                     '/stop_area_searches',
                    {
                      paramName: 'stop_area_search[user_input]',
                      minChars: 2,
                      indicator: 'spinner',
                      method:'post',
                      afterUpdateElement:lineListDisplay.refresh,
                      select:'name'
                    });
});

function getStopName(liElement) {
  return liElement.select(".name")[0].innerHTML;
}

var LineListDisplay =  Class.create({
  refresh: function(inputElement, liElement) {
    var stopName = getStopName(liElement);

      if ( liElement.id.match( /stop_(\d+)/)!=null) {
        var stop_area_id  = RegExp.$1;
        inputElement.value = stopName;
        new Ajax.Request( '/stop_areas/'+stop_area_id+'/stop_area_line_searches',
            { method: 'get',
              onLoading: function(transport) {
              indicateResultsLoading(stopName);
        }});
      }
  }
});





