var init_city_autocomplete = function () {
    $('input[name=city], input[name=ville]').autocomplete({
        source: '/wp-admin/admin-ajax.php?action=office_city_autocomplete',
        minLength: 2,
        select: function(event, ui) {
            $(this).val(ui.item.value);
            $(this.form).submit();
        }
    });
}
var init_region_autocomplete = function () {
    $('input[name=region]').autocomplete({
        source: '/wp-admin/admin-ajax.php?action=office_region_autocomplete',
        minLength: 2,
        select: function() {
            $(this).val(ui.item.value);
            $(this.form).submit();
        }
    });
}
var init_reverse_zipcode_geocode = function() {
    var reverse_postcode_on_submit = function() {
        var valid_fields = $('input#zipcode, input#postal, input#postal2, input#zipcode2', this);
        if(valid_fields.size() == 0) { //not found - this is not the desired form
            return true;
        }
        var $field = $(valid_fields[0]);
        if($field.val() == "") {//val is empty, skip it
            return true;
        }

        //create/retrieve the hidden fields
        var long_field, lat_field;
        if(!$field.data("long-field") || !$field.data("lat-field")) {
            long_field = $('<input type=hidden name=long value="" />');
            lat_field = $('<input type=hidden name=lat value="" />');
            long_field.insertAfter($field);
            lat_field.insertAfter($field);

            $field
                .data('long-field', long_field)
                .data('lat-field', lat_field);
        } else {
            long_field = $field.data("long-field");
            lat_field = $field.data("lat-field");
        }
        var geocoder = new google.maps.Geocoder();
        //disable the field
        $field.attr('readonly', true);
        $field.css("cursor", "wait");
        
        //reverse zipcode
        geocoder.geocode( { 'address': $field.val()}, function(results, status) {
          $field.removeAttr('readonly');
          $field.css("cursor", "");

          if (status == google.maps.GeocoderStatus.OK) {
              if (results[0]) {
                  var latlng = results[0].geometry.location;
                  long_field.val(latlng.lng());
                  lat_field.val(latlng.lat());
                  $($field[0].form).unbind('submit', reverse_postcode_on_submit); //remove this event and submit the form
                  $($field[0].form).submit();
              }
          } else {
              long_field.val('');
              lat_field.val('');
              $($field[0].form).unbind('submit', reverse_postcode_on_submit); //remove this event, no reverse is needed
              $($field[0].form).submit();
          }
        });
        return false; //stop the form submit

    };

    //hook on the home page office search AND the second office search form in the right sidebar
    $('form.office-search-form, #underNav .line2 form:eq(1), .contact .line2 form:eq(1), #courriel form:eq(1)').submit(reverse_postcode_on_submit);
}
jQuery(document).ready(function() {
    init_city_autocomplete();
    init_region_autocomplete();
    init_reverse_zipcode_geocode();
});
