var map;
var bounds;
var locationCount;

function google_maps_initialize(zoom) {
	bounds = new google.maps.LatLngBounds();
	locationCount = 0;

	var myOptions = {
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		zoom:zoom
	}
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}

function google_maps_addLocation(location) {
	var location = new google.maps.LatLng(location[0], location[1]);
	locationCount++;
	
	var marker = new google.maps.Marker({
	    map: map, 
	    position: location
	});
	
	bounds.extend(location);
	
	if(locationCount>1) {
		map.fitBounds(bounds);
	} else {
		map.setCenter(location);
	}
	
	return marker;
}

//bereken de locatie aan de hand van een adres en sla deze op in de post & laat zien in de map
function google_maps_calculateLocation(address,post_id,admin_url) {
	var geocoder = new google.maps.Geocoder();
	
	geocoder.geocode( { 'address': address}, function(results, status) {
	  if (status == google.maps.GeocoderStatus.OK) {
		google_maps_updateLocation(results[0].geometry.location.lat(),results[0].geometry.location.lng(),post_id,admin_url);
		google_maps_addLocation(new Array(results[0].geometry.location.lat(),results[0].geometry.location.lng()));
	  } else {
	    alert("Geocode was not successful for the following reason: " + status);
	  }
	});
}

function google_maps_updateLocation(lat,lng,post_id,admin_url) {
	var data = {
				action : "google_maps_updateLocation",
			    post_id : post_id,
			    lat : lat,
			    lng : lng
			    };

	jQuery.post(admin_url, data, function(results) {
		//alert(results);
	});
}
