$(document).ready(function(){
	// Contact Map
	load_google_map($('.map-block'), $('.map-block .map'));
	
	// Aiport to Fauxquets Route Map
	load_google_map($('.ftc-1128 .image-panel-4'), $('.ftc-1128 .image-panel-4 .map'), {
		origin: '49.433306,-2.596514', // Airport
		destination: '49.45020137786428, -2.588850412963893', // Campsite
		travelMode: google.maps.TravelMode.DRIVING,
		unitSystem: google.maps.UnitSystem.IMPERIAL
	});
	
	// Port to Fauxquets Route Map
	load_google_map($('.ftc-1129 .image-panel-4'), $('.ftc-1129 .image-panel-4 .map'), {
		origin: '49.457288,-2.529023', // Port
		destination: '49.45020137786428, -2.588850412963893', // Campsite
		travelMode: google.maps.TravelMode.DRIVING,
		unitSystem: google.maps.UnitSystem.IMPERIAL
	});
});

// Load the google map
function load_google_map(parent, map, route){

	if(parent.length > 0 && map.length > 0){

		parent.show();
		
		var center = new google.maps.LatLng(49.45020137786428, -2.588850412963893);
		var myOptions = {
			zoom: 15,
			center: center,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};

		var map = new google.maps.Map(map[0], myOptions);
		
		var directionsDisplay = new google.maps.DirectionsRenderer();
		
		if(route != null){
		
			var directionsService = new google.maps.DirectionsService;
			directionsService.route(route, function(result, status) {
				if (status == google.maps.DirectionsStatus.OK) {
					directionsDisplay.setDirections(result);
				}
			});
			
			directionsDisplay.setMap(map);

		}else{
		
			/* var image = new google.maps.MarkerImage(
				'/<?php print $directory ?>/images/map-marker.png',
				new google.maps.Size(41, 35), // This marker is 41 pixels wide by 35 pixels tall.
				new google.maps.Point(0,0), // The origin for this image is 0,0.
				new google.maps.Point(10, 33); // The anchor for this image is the base of the flagpole at 0,35.
			); */
			
			var mapMarker = new google.maps.Marker({
				position: center,
				map: map,
				//icon: image,
				draggable: false
			});
			google.maps.event.addListener(mapMarker, 'dragend', function(mapMarker){
				console.log(this.getPosition().lat() + ", " + this.getPosition().lng());
			});
		
		}

		
	
	}
}
