Autocompleter.Location = Class.create(Autocompleter.Local, {

  bounds: function() {
    var sw = new google.maps.LatLng(47.270210, 5.866240);
    var ne = new google.maps.LatLng(55.058140, 15.042050);
    if (!this.cachedBounds) {
      this.cachedBounds = new google.maps.LatLngBounds(sw, ne);
    }
    return this.cachedBounds;
  },

  getUpdatedChoices: function() {

    var self = this;

    var geocoder = new google.maps.Geocoder();

    
    geocoder.geocode({'address': this.getToken() + ", Deutschland", language:"DE", region: 'DE'}, function(results, status) {

      self.options.array = results.map(function(obj) {
        return obj.formatted_address.replace(", Deutschland", "");
      });

        self.updateChoices(self.options.selector(self));
    });
  }
});

imedo.Initializer.doAfterInit(function() {

  if ($("provider_query")){
    new Ajax.Autocompleter("provider_query", "expertise_choices", "/practice/provider_search/expertises_completer", {
      onShow: function(element, update) {
        // Circumvents bug in ie8 regarding positioning whereby top and left values are incorrectly
        // set for hidden elements
        update.show();

        if(!update.style.position || update.style.position=='absolute') {
          update.style.position = 'absolute';
          Position.clone(element, update, {
            setHeight: false,
            offsetTop: element.offsetHeight
          });
        }
        Effect.Appear(update,{duration:0.15});
      }
    });
  }

  // Can't set a listener directly on the a.suggestion links as they are loaded into the page at a later time

  if ($("expertise_choices")) {
    Event.observe("expertise_choices", "click", function(event) {
      var el = Event.element(event);
      if (el.hasClassName("suggestion")) {
        $("provider_query").value = el.innerHTML;
        event.stop();
      }
    });
  }
});

