
-@results = JSON.load(open("https://maps.googleapis.com/maps/api/place/autocomplete/json?input=#{URI.encode(@place)}&types=(regions)&language=fr&key=AIzaSyB4D4IEQ6XL0aeSc06aC9nIn-Bzasr-oB8"))

-@results["predictions"].each do |prediction|
  -if prediction["types"][0] == "postal_code" or prediction["types"][0] == "locality" or prediction["types"][0] == "administrative_area_level_2" or prediction["types"][0] == "administrative_area_level_1" #or prediction["types"][0] == "sublocality_level_1" 
    
    -if prediction["types"][0] == "postal_code"
      
      - Geocoder.search(prediction["description"]).each do |result|
        
      
        -place_detail = JSON.load(open("https://maps.googleapis.com/maps/api/place/details/json?placeid=#{prediction['place_id']}&key=AIzaSyB4D4IEQ6XL0aeSc06aC9nIn-Bzasr-oB8"))

        -if place_detail
          -result_parsed = {"lat" => place_detail["result"]["geometry"]["location"]["lat"], "lng" => place_detail["result"]["geometry"]["location"]["lng"], "formatted_address" => place_detail["result"]["formatted_address"] }
          
          -place_detail["result"]["address_components"].each do |component|
            -result_parsed[component["types"][0]] = component['long_name']
            -result_parsed[component["types"][0]+"_short"] = component['short_name']
            
          
          
          =#debug result_parsed
          
          -city =  prediction["description"].gsub(result_parsed["postal_code"], "")
        
          %li
            =link_to "#", :onclick => "$(this).closest('.place_search').find('#place_city').val('');$(this).closest('.place_search').find('#place_id').val('#{prediction['place_id']}');$(this).closest('.place_search').find('#place_cp').val('#{result_parsed["postal_code"]}');$(this).closest('.place_search').find('#place').val($(this).text());$(this).closest('.place_search').find('#place_type').val(2);$('.autocomp').html('');$('.autocomp').hide();return false;" do
              Code postal :
              =result_parsed["postal_code"]
        
   
        -result = Geocoder.search(result_parsed["postal_code"]+","+result_parsed["country"], :types => "(regions)", :components => ["country:#{@country}"], :region => "regions")

        -result.each do |result|
          -if ["administrative_area_level_2","administrative_area_level_1", "locality", "postal_code", "street_address"].include?(result.data["types"][0])
            
  
  
            -if result.data["types"][0] == "postal_code"
              -if result.data["postcode_localities"]
                -result.data["postcode_localities"].each do |locality|
                  %li
                    =link_to "#", :onclick => "$(this).closest('.place_search').find('#place_city').val('#{locality}');$(this).closest('.place_search').find('#place_id').val('#{prediction['place_id']}');$(this).closest('.place_search').find('#place_cp').val('#{result_parsed["postal_code"]}');$(this).closest('.place_search').find('#place').val($(this).text());$(this).closest('.place_search').find('#place_type').val(1);$('.autocomp').html('');$('.autocomp').hide();return false;" do
                      =result.postal_code + " " + locality.strip
    -elsif prediction["types"][0] == "administrative_area_level_1"
      %li
        =link_to "#", :onclick => "$(this).closest('.place_search').find('#place_city').val('');$(this).closest('.place_search').find('#place_id').val('#{prediction['place_id']}');$(this).closest('.place_search').find('#place_city').val('#{prediction["description"]}');$(this).closest('.place_search').find('#place').val($(this).text());$(this).closest('.place_search').find('#place_type').val(4);$('.autocomp').html('');$('.autocomp').hide();return false;" do
          
          =prediction["description"]
    -elsif prediction["types"][0] == "administrative_area_level_2"
      %li
        =link_to "#", :onclick => "$(this).closest('.place_search').find('#place_city').val('');$(this).closest('.place_search').find('#place_id').val('#{prediction['place_id']}');$(this).closest('.place_search').find('#place_cp').val('');$(this).closest('.place_search').find('#place_city').val('#{prediction["description"]}');$(this).closest('.place_search').find('#place').val($(this).text());$(this).closest('.place_search').find('#place_type').val(3);$('.autocomp').html('');$('.autocomp').hide();return false;" do
          
          =prediction["description"]
    -elsif prediction["types"][0] == "locality"
      %li
        =link_to "#", :onclick => "$(this).closest('.place_search').find('#place_city').val('');$(this).closest('.place_search').find('#place_id').val('#{prediction['place_id']}');$(this).closest('.place_search').find('#place_cp').val('');$(this).closest('.place_search').find('#place_city').val('#{prediction["description"]}');$(this).closest('.place_search').find('#place').val($(this).text());$(this).closest('.place_search').find('#place_type').val(5);$('.autocomp').html('');$('.autocomp').hide();return false;" do
          
          =prediction["description"]
    -else
      %p
        =prediction["types"][0]
        =":"
        =prediction["description"]


