initializeAutocomplete = (id) ->
  element = document.getElementById(id)
  if element
    autocomplete = new (google.maps.places.Autocomplete)(element, types: [ '(regions)'])
    google.maps.event.addListener autocomplete, 'place_changed', onPlaceChanged
  return

onPlaceChanged = ->
  place = @getPlace()
  console.log(place.geometry.location["D"])
  
  latlng = new (google.maps.LatLng)(place.geometry.location["k"], place.geometry.location["D"])
  geocoder = new google.maps.Geocoder()
  geocoder.geocode { 'latLng': latlng }, (results, status) ->
    if status == google.maps.GeocoderStatus.OK
      if results[0]
        
        elt = results[0].address_components
        for i of elt
          `i = i`
          if elt[i].types[0] == 'postal_code'
            alert  elt[i].long_name
            document.getElementById('cp').value = elt[i].long_name
          if elt[i].types[0] == 'locality'
            document.getElementById('adr').value = elt[i].long_name
          if elt[i].types[0] == 'administrative_area_level_2'
            document.getElementById('dpt').value = elt[i].long_name
          if elt[i].types[0] == 'country'
            document.getElementById('pays').value = elt[i].long_name
      
    else
      alert 'Geocoder failed due to: ' + status
    return

  # ---
  # generated by js2coffee 2.0.3
  
  
  
  for i of place.address_components
    component = place.address_components[i]
    for j of component.types
      # Some types are ["country", "political"]
     
      
      type_element = document.getElementById(component.types[j])
      if type_element
       
        type_element.value = component.long_name
        
        alert "tesr"
        
  return

google.maps.event.addDomListener window, 'load', ->
  initializeAutocomplete 'user_input_autocomplete_address'
  return