//Useful links:
// http://code.google.com/apis/maps/documentation/javascript/reference.html#Marker
// http://code.google.com/apis/maps/documentation/javascript/services.html#Geocoding
// http://jqueryui.com/demos/autocomplete/#remote-with-cache
      
var geocoder;
var map;
var marker;
    
function initialize(){
  //GEOCODER
  geocoder = new google.maps.Geocoder();
}
		
$(document).ready(function() { 
  initialize();
  $(function() {
    $("#adres2").autocomplete({
      //This bit uses the geocoder to fetch address values
      source: function(request, response) {
        geocoder.geocode( {'address': request.term + ", "+  document.frm.land2.value }, function(results, status) {
          response($.map(results, function(item) {
            return {
              label:  item.formatted_address,
              value: item.formatted_address,
              latitude: item.geometry.location.lat(),
              longitude: item.geometry.location.lng()
			}
          }));
        })
      },
      //This bit is executed upon selection of an address
      select: function(event, ui) {
        $("#latitude2").val(ui.item.latitude);
        $("#longitude2").val(ui.item.longitude);
        $("#adres2").val(ui.item.formatted_address);
		document.frm.adres2.style.backgroundColor="#E2EAF5";
      }
    });
  });

  $(function() {
    $("#adres1").autocomplete({
      //This bit uses the geocoder to fetch address values
      source: function(request, response) {
        geocoder.geocode( {'address': request.term + ", "+  document.frm.land1.value  }, function(results, status) {
          response($.map(results, function(item) {
            return {
              label:  item.formatted_address,
              value: item.formatted_address,
              latitude: item.geometry.location.lat(),
              longitude: item.geometry.location.lng()			  
            }
          }));
        })
      },
      //This bit is executed upon selection of an address
      select: function(event, ui) {
        $("#latitude1").val(ui.item.latitude);
        $("#longitude1").val(ui.item.longitude);
        $("#adres1").val(ui.item.formatted_address);
		document.frm.adres1.style.backgroundColor="#E2EAF5";
      }
    });
  });
});
