var WhereBox = (function(){
  var oldValue = '';
  var inc=1;
  var displayedInc = 0;
  var pub = {
    lookup: function( e ) {
      var value = this.value;
      if(value == oldValue) return;
      oldValue = value;
      if (!value || /^[0-9\- ]+$/.exec(value)) {
        $('#suggestions').hide();
      } else {
        $.get( "/searchac.php", {city: value,inc:inc++}, function(data){
          if ( data.html && (1*data.inc) >displayedInc ) {
            displayedInc = 1*data.inc;
            $('#suggestions').show();
            $('#autoSuggestionsList').html(data.html);
          }
        });
      }
    },
    hide: function(){ setTimeout(function(){ $('#suggestions').hide(); }, 200); },
    fill: function(value) { $('#where').val(value); pub.hide(); $('#lastname').focus(); },
    init: function(){
      $('#where').focus().blur(pub.hide);
      $('#where').keyup(pub.lookup);
    }
  };
  return pub;
})();

$(WhereBox.init);
