/**
 * Last Location and Route Map
 */
function loadLastLocation()
{
	// Check Map Element
	var lastLocation = $('lastLocationMap');
	
	if (GBrowserIsCompatible() && lastLocation) {
		// Initialize Map
		lastLocation = new GMap2(lastLocation);
        lastLocation.setCenter(new GLatLng(0, 0), 0);
        lastLocation.addControl(new GSmallMapControl());
        
        // Get route and last location from database
        new Ajax.Request('includes/googlemaps.inc.php?action=route',
        {
        	method: 'get',
        	onSuccess: function (transport) {
        		var entries = eval(transport.responseText);
        		
        		// Modify map only when there are entries
        		if (entries) {
					var lastPoint = new GLatLng(entries[0].latitude, entries[0].longitude);
					var route = new Array();
					
					for (id in entries) {
						var point = new GLatLng(entries[id].latitude, entries[id].longitude);
						// Add point to route
						route[id] = point;
        			}
        			
        			// Add marker/route and center the map
	        		lastLocation.addOverlay(new GMarker(lastPoint));
        			lastLocation.addOverlay(new GPolyline(route, "#ff0000", 3, 1, "{geodesign:true}"));
    				lastLocation.setCenter(lastPoint, 4);
           		}
        	},
        	onFailure: function() {
        		alert('Error in loadLastLocation()');
        	}
        });
	}
}

/**
 * Accommodation Map
 */
var accommodationLatitude = false;
var accommodationLongitude = false;
function loadAccommodationLocation()
{
	var accommodationLocation = $('accommodationLocationMap');
	if (GBrowserIsCompatible() && accommodationLocation) {
		
		// Initialize Map
		accommodationLocation = new GMap2(accommodationLocation);
        accommodationLocation.setCenter(new GLatLng(accommodationLatitude, accommodationLongitude), 14);
        accommodationLocation.addControl(new GSmallMapControl());
	accommodationLocation.addControl(new GMapTypeControl());
        
        // Add Accommodation Marker
        var point = new GLatLng(accommodationLatitude, accommodationLongitude);
		accommodationLocation.addOverlay(new GMarker(point));
	}
}

/**
 * Sight Map
 */
var sightLatitude = false;
var sightLongitude = false;
function loadSightLocation()
{
	var sightLocation = $('sightLocationMap');
	if (GBrowserIsCompatible() && sightLocation) {
		
		// Initialize Map
		sightLocation = new GMap2(sightLocation);
        sightLocation.setCenter(new GLatLng(sightLatitude, sightLongitude), 14);
        sightLocation.addControl(new GSmallMapControl());
	sightLocation.addControl(new GMapTypeControl());
        
        // Add Sight Marker
        var point = new GLatLng(sightLatitude, sightLongitude);
		sightLocation.addOverlay(new GMarker(point));
	}
}

/**
 * Single Photo Map in Album
 */
var photoLatitude;
var photoLongitude;
function loadPhotoLocation()
{
	photoLocation = $('photoLocationMap');
	if (GBrowserIsCompatible() && photoLocation) {
		photoPoint = new GLatLng(photoLatitude, photoLongitude)
		
		// Initialize Map
		photoLocation = new GMap2(photoLocation);
        photoLocation.setCenter(photoPoint, 4);
        photoLocation.addControl(new GSmallMapControl());
	photoLocation.addControl(new GMapTypeControl());
        
        // Set Selected Photo
        if (photoLatitude && photoLongitude) {
			photoLocation.addOverlay(new GMarker(photoPoint));
        }
	}
}

var entryLocation = false;
var entry = new Object();
var photoList = new Object();
var entryPhotoMarkers = new Array();
function loadEntryLocation()
{
	entryLocation = $('entryLocationMap');
	if (GBrowserIsCompatible() && entryLocation) {
		// Get route and last location from database
        new Ajax.Request('includes/entry.inc.php?action=entry&id='+id,
        {
        	method: 'get',
        	onSuccess: function (transport) {
        		var output = eval(transport.responseText);
        		entry = output.entry;
				photoList = output.photoList;
				
				// Map Boundaries
				var bounds = new GLatLngBounds();
				// Entry Point
				var point = new GLatLng(entry.latitude, entry.longitude);
				
				
				// Initialize Map
				entryLocation = new GMap2(entryLocation);
		        entryLocation.setCenter(point, 9);
		        entryLocation.addControl(new GSmallMapControl());
			entryLocation.addControl(new GMapTypeControl());				
		        
		        // Add Entry Marker
				entryLocation.addOverlay(new GMarker(point));
				bounds.extend(point);
		
				// Add Photo Markers
				if (photoList != false) {
					// Icon for Photo Markers
					var photoIcon = new GIcon(G_DEFAULT_ICON);
					photoIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
					photoIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
					photoIcon.iconSize = new GSize(12, 20);
					photoIcon.shadowSize = new GSize(22, 20);
					photoIcon.iconAnchor = new GPoint(6, 20);
					photoIcon.infoWindowAnchor = new GPoint(5, 1);
					
					for (id in photoList) {
						if (photoList[id].latitude && photoList[id].longitude) {
							var photoPoint = new GLatLng(photoList[id].latitude, photoList[id].longitude);
							var marker = new GMarker(photoPoint, {icon:photoIcon});
							entryPhotoMarkers[id] = marker;
							entryLocation.addOverlay(marker);
							bounds.extend(photoPoint);
						}
					}
					
					// Define Zoom Level
					if (entryLocation.getBoundsZoomLevel(bounds) < 13) {
						var zoomLevel = entryLocation.getBoundsZoomLevel(bounds)-1;
					} else {
						var zoomLevel = 13;
					}
					
					// Define Map
					entryLocation.setCenter(bounds.getCenter(), zoomLevel);
				}
			}
        });
	}
}

var highlightMarker = false;
function enablePhotoMarkerHighlight(id)
{
	if (entryPhotoMarkers[id]) {
		var photoIcon = new GIcon(G_DEFAULT_ICON);
		photoIcon.image = "http://labs.google.com/ridefinder/images/mm_20_blue.png";
		photoIcon.shadow = null;
		photoIcon.iconSize = new GSize(12, 20);
		photoIcon.shadowSize = new GSize(22, 20);
		photoIcon.iconAnchor = new GPoint(6, 20);
		var point = new GLatLng(photoList[id].latitude, photoList[id].longitude);
		var marker = new GMarker(point, {icon:photoIcon});
		highlightMarker = marker;
		entryLocation.addOverlay(marker);
	}
}

function disablePhotoMarkerHighlight()
{
	if (highlightMarker) {
		entryLocation.removeOverlay(highlightMarker);
		highlightMarker = false;
	}
}
