// Variables to hold 
var geocoder;
var map;
var marker = false;
var markers = false;
 
function geocode(address) 
{
	if (marker != false)
	{
		marker.setMap(null);
	}

    geocoder.geocode({'address':address+',france'}, onGeocodeResult);
}

function refresh_input() 
{
	geocoder.geocode({'latLng': marker.getPosition()}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        if (results[0]) {
          $('address').value = results[2].formatted_address;
          $('latitude').value = marker.getPosition().lat();
          $('longitude').value = marker.getPosition().lng();
        }
      }
    });
	if (markers != false)
	{
		for (var i=0; i < markers.length; i++) {
			markers[i].setMap(null);
		};
	}
	// load('/node.php?cssid=saturneo&ajx=saturneo', 'result');
}

function onGeocodeResult(result, status) 
{
	// Get the lattitude and langgitude of the first result (note: real app should check for no result being returned)
	var myLatitudeAndLangitude=new google.maps.LatLng(
		result[0]['geometry']['location']['lat'](),
		result[0]['geometry']['location']['lng']());
 
	// Show a marker in the first result.
	// http://code.google.com/apis/maps/documentation/javascript/overlays.html		
	marker = new google.maps.Marker({
		map:map,
		draggable:true,
		animation:google.maps.Animation.DROP ,
		position:myLatitudeAndLangitude
	});
	map.panTo(myLatitudeAndLangitude);
	// map.setZoom(4);
	
	var myOptions = {
		zoom: 8
	}
	map.setOptions(myOptions);
	
	//Add listener to marker for reverse geocoding
	// google.maps.event.addListener(marker, 'drag', refresh_input);
	google.maps.event.addListener(marker, 'mouseup', refresh_input);
	refresh_input();
	// var infowindow = new google.maps.InfoWindow({
	// 	content: result[0]['formatted_address']
	// });
	// infowindow.open(map,marker);	
}

function gm_pushmarkers(json) 
{
	adresses = eval(json);
	for (var i=0; i < adresses.length; i++) {
		geocoder.geocode({'address':adresses[i]}, moremarkers);
	};
}

function moremarkers(result, status) 
{
	// alert(result);
	var myLatitudeAndLangitude=new google.maps.LatLng(
		result[0]['geometry']['location']['lat'](),
		result[0]['geometry']['location']['lng']());
	
	if (markers == false) markers = new Array();
	count = markers.length;
	
	markers[count] = new google.maps.Marker({
		map:map,
		draggable:false,
		animation:google.maps.Animation.DROP ,
		position:myLatitudeAndLangitude
	});
}
 
// INITIALIZATION PART 
function initialize() {
  var myLatlng = new google.maps.LatLng(46.52863469527167,2.43896484375);
  var myOptions = {
    zoom: 5,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  geocoder = new google.maps.Geocoder();
}
  
function loadGoogleMapScript() {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";
  document.body.appendChild(script);
}
  
window.onload = loadGoogleMapScript;
// INITIALIZATION PART ENDS
