window.addressOriginalValue = '';
$(function(){
       addressOriginalValue = $('#address_input').val();
    $("#address_input").myautocomplete(def_path + 'ajax/myaddresses', {
            mustMatch: false,
            selectFirst: true,
            max: 30
        }
            ).focus(
                    function(){
                            if($(this).val() == addressOriginalValue)
                                    $(this).val('');
                    }
            ).blur(
                    function(){
                            if($(this).val() == '')
                                    $(this).val(addressOriginalValue);
                    }
    );
    $("#address_input").result(function(event, data, formatted) {
        if (data)
            $("#apn-id").val(data[1]);
        myautocomplete_hook();
    });
    
    $('#toggleProperty').click(function(){
                    $('#address_form').submit(); return false;
               });
    
    $("#city_input").myautocomplete(def_path + 'ajax/cities', {
        width:150,
        selectFirst:true,
        mustMatch: true,

        max: 30
    }).     focus( function(){
                            if($(this).val() == '')
                                    $(this).val('');
                    }
            ).blur(
                    function(){
                            if($(this).val() == '')
                                    $(this).val('');
                    }
    );
    $("#city_input").result(function(event, data, formatted) {
        if (data)
         city_myautocomplete_hook();
    });   
    
    
    //for testing setting to true
    address_by_location = false;
    
    if (address_by_location){
        navigator.geolocation.getCurrentPosition(reverseGeocode);
    }
    
});

function myautocomplete_hook(){
    return true; 
}
function city_myautocomplete_hook(){
    return true; 
}
/**
 *  If address_by_location is true, this function is used to look up my address
 *
 *  **/





function reverseGeocode(position){
   
    
    local_addresses = [];
    latlng = "latlng="+position.coords.latitude + "," + position.coords.longitude;
    
    //$.getJSON('http://maps.googleapis.com/maps/api/geocode/json?' + latlng + '&sensor=true',                function(data){                    local_addresses=data.results;});
  
  
  
  
  
    /*build the html list
    var local_addresses_div = document.createElement('div');
    local_addresses_div.id='local_addresses';
    
    var local_addresses_ul = document.createElement('ul');
    
    for( i = 0; i < local_addresses.length; i++){
            newElement = document.createElement('li')
            newElement.innerHTML = local_addresses[i].formatted_address;
            local_addresses_div.appendChild(newElement);
        }
        document.body.appendChild(local_addresses_div);*/
  }
     

    


/*

var test_data = {
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "305",
               "short_name" : "305",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Gate 5 Rd",
               "short_name" : "Gate 5 Rd",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Sausalito",
               "short_name" : "Sausalito",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Southeast Marin",
               "short_name" : "Southeast Marin",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Marin",
               "short_name" : "Marin",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94965",
               "short_name" : "94965",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "305 Gate 5 Rd, Sausalito, CA 94965, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.8701480,
               "lng" : -122.5001720
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.87149698029150,
                  "lng" : -122.4988230197085
               },
               "southwest" : {
                  "lat" : 37.86879901970850,
                  "lng" : -122.5015209802915
               }
            }
         },
         "types" : [ "street_address" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Sausalito",
               "short_name" : "Sausalito",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Southeast Marin",
               "short_name" : "Southeast Marin",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Marin",
               "short_name" : "Marin",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Sausalito, CA, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 37.8729090,
                  "lng" : -122.4750820
               },
               "southwest" : {
                  "lat" : 37.8439270,
                  "lng" : -122.5120840
               }
            },
            "location" : {
               "lat" : 37.85909370,
               "lng" : -122.48525070
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.8729090,
                  "lng" : -122.4750820
               },
               "southwest" : {
                  "lat" : 37.8439270,
                  "lng" : -122.5120840
               }
            }
         },
         "types" : [ "locality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "94965",
               "short_name" : "94965",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Sausalito",
               "short_name" : "Sausalito",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Sausalito, CA 94965, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 37.91306489999999,
                  "lng" : -122.4716280
               },
               "southwest" : {
                  "lat" : 37.82303590,
                  "lng" : -122.6212140
               }
            },
            "location" : {
               "lat" : 37.86504140,
               "lng" : -122.56555490
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.91306489999999,
                  "lng" : -122.4716280
               },
               "southwest" : {
                  "lat" : 37.82303590,
                  "lng" : -122.6212140
               }
            }
         },
         "types" : [ "postal_code" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Southeast Marin",
               "short_name" : "Southeast Marin",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Marin",
               "short_name" : "Marin",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Southeast Marin, CA, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 37.9305240,
                  "lng" : -122.4186820
               },
               "southwest" : {
                  "lat" : 37.8323270,
                  "lng" : -122.5770890
               }
            },
            "location" : {
               "lat" : 37.89000340,
               "lng" : -122.46936880
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.9305240,
                  "lng" : -122.4186820
               },
               "southwest" : {
                  "lat" : 37.8323270,
                  "lng" : -122.5770890
               }
            }
         },
         "types" : [ "administrative_area_level_3", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Marin",
               "short_name" : "Marin",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Marin, California, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 38.3209450,
                  "lng" : -122.3474540
               },
               "southwest" : {
                  "lat" : 37.81512800000001,
                  "lng" : -123.1345230
               }
            },
            "location" : {
               "lat" : 38.0834030,
               "lng" : -122.76330360
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 38.3209450,
                  "lng" : -122.3474540
               },
               "southwest" : {
                  "lat" : 37.81512800000001,
                  "lng" : -123.1345230
               }
            }
         },
         "types" : [ "administrative_area_level_2", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "California, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 42.00951690,
                  "lng" : -114.1312110
               },
               "southwest" : {
                  "lat" : 32.5288320,
                  "lng" : -124.4820030
               }
            },
            "location" : {
               "lat" : 36.7782610,
               "lng" : -119.41793240
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 42.00951690,
                  "lng" : -114.1312110
               },
               "southwest" : {
                  "lat" : 32.5288320,
                  "lng" : -124.4820030
               }
            }
         },
         "types" : [ "administrative_area_level_1", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "United States",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 71.53879999999999,
                  "lng" : -66.88507489999999
               },
               "southwest" : {
                  "lat" : 18.77630,
                  "lng" : 170.59570
               }
            },
            "location" : {
               "lat" : 37.090240,
               "lng" : -95.7128910
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 71.53879999999999,
                  "lng" : -66.88507489999999
               },
               "southwest" : {
                  "lat" : 18.77630,
                  "lng" : 170.59570
               }
            }
         },
         "types" : [ "country", "political" ]
      }
   ],
   "status" : "OK"
}
*/










