/* Map App form Routes to France */

var map;
var gdir;
var geocoder = null;
var addressMarker;



function initialize() {

	if(GBrowserIsCompatible()){
		var locationLatLng     = new GLatLng(49.785755, -2.597890);
		var defaultZoomLevel   = 6;
		
		// default opening text
		document.getElementById("directions").innerHTML = "<br /><center>Below the map, please select your arrival port from the 'Ports' menu then enter your destination name into the 'Destination' text box, now click on the 'Get Directions' button.</center>";
		
		
		var blueIcon    = new GIcon(G_DEFAULT_ICON);
		blueIcon.image  = "http://maps.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";
		
		var greenIcon   = new GIcon(G_DEFAULT_ICON);
		greenIcon.image = "http://maps.google.com/intl/en_us/mapfiles/ms/micons/green-dot.png";
		
		var redIcon   = new GIcon(G_DEFAULT_ICON);
		redIcon.image = "http://maps.google.com/intl/en_us/mapfiles/ms/micons/red-dot.png";
	
		map = new GMap2(document.getElementById("map_canvas"), {size:new GSize(275,345)});
		gdir = new GDirections(map, document.getElementById("directions"));
				
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		
		
				
		// Leaving Portsmouth  		
  		var polyOptions = {geodesic:false};
		var polyline = new GPolyline([new GLatLng(50.798914,-1.091163),  new GLatLng(50.675112,-0.97229),  new GLatLng(49.182863,-0.370679)], "#ff0000", 3, 0.4, polyOptions);
		map.addOverlay(polyline);
		var polyline = new GPolyline([new GLatLng(50.798914,-1.091163), new GLatLng(50.675112,-0.97229),  new GLatLng(49.639093,-1.625298)], "#ff0000", 3, 0.4, polyOptions);
		map.addOverlay(polyline);  		
		var polyline = new GPolyline([new GLatLng(50.798914,-1.091163), new GLatLng(50.675112,-0.97229),  new GLatLng(50.074534,-2.043457), new GLatLng(49.29265,-2.307129), new GLatLng(48.649337,-2.025674)], "#ff0000", 3, 0.4, polyOptions);
		map.addOverlay(polyline);
		
		// Leaving Poole
		var polyline = new GPolyline([new GLatLng(50.719412,-1.98113), new GLatLng(49.639093,-1.625298)], "#009f00", 3, 0.4, polyOptions);
		map.addOverlay(polyline);
		
		// Leaving Plymouth
		var polyline = new GPolyline([new GLatLng(50.370381,-4.142653), new GLatLng(48.726199,-3.985325)], "#0000ff", 3, 0.4, polyOptions);
		map.addOverlay(polyline);
		
		// Portsmouth to Spain
		var polyline = new GPolyline([new GLatLng(50.812414,-1.054687),    new GLatLng(50.504577,-1.043701),    new GLatLng(50.196126,-2.548828), new GLatLng(49.030185,-6.152344), new GLatLng(48.293914,-6.547852), new GLatLng(43.46096,-3.807934)], "#ffffff", 3, 0.4, polyOptions);		
		map.addOverlay(polyline);
		
		// Plymouth to Spain
		var polyline = new GPolyline([new GLatLng(50.341721,-4.130859), new GLatLng(49.030185,-6.152344), new GLatLng(48.293914,-6.547852), new GLatLng(43.46096,-3.807934)], "#ffffff", 3, 0.4, polyOptions);
		map.addOverlay(polyline);
		
		
		// Plymouth to Saint-Malo
		var polyline = new GPolyline([new GLatLng(50.341721,-4.130859), new GLatLng(48.649337,-2.025674)], "#FFFF00", 3, 0.4, polyOptions);
		map.addOverlay(polyline);
		
		
		
		// UK
		map.addOverlay(AddMarker(50.798914,-1.091163, greenIcon, 'Portsmouth, Hampshire'));
		map.addOverlay(AddMarker(50.719412,-1.98113,  greenIcon, 'Poole, Dorset'));
		map.addOverlay(AddMarker(50.370381,-4.142653, greenIcon, 'Plymouth, Devon'));
				
		// France
		map.addOverlay(AddMarker(49.182863,-0.370679, blueIcon, 'Caen, France'));
		map.addOverlay(AddMarker(49.639093,-1.625298, blueIcon, 'Cherbourg, France'));
		map.addOverlay(AddMarker(48.726199,-3.985325, blueIcon, 'Roscoff, France'));
		map.addOverlay(AddMarker(48.649337,-2.025674, blueIcon, 'Saint Malo, France'));
		
		// Spain
		map.addOverlay(AddMarker(43.46096,-3.807934, redIcon, 'Santander, Spain'));
		
		
		GEvent.addListener(gdir, "load", onGDirectionsLoad);
		GEvent.addListener(gdir, "error", handleErrors);

		map.setCenter(locationLatLng, defaultZoomLevel);
	}
}



function AddMarker(lng, lat, icon, text){

	var marker = new GMarker(new GLatLng(lng, lat), {icon: icon });
	GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(text);});
	return marker;
}



function resetMap(rs){
	
	var from = rs.value;
	if(from=='')initialize();
}
    

function setDirections(fromAddress, toAddress, highways, language){
	
	if(fromAddress=='')
		alert('Please select the arrival port you will be driving from.');
	else{
		document.getElementById("directions").innerHTML = ''; // clear default opening text
		
		toAddress = toAddress.replace(',', '');
		toAddress = toAddress.replace('France', '').toLowerCase();
	
		document.getElementById("dirloco").innerHTML = ' to ' + 'France, ' + toAddress;
		
		gdir.load("from: " + fromAddress + " to: " + toAddress, {"locale": language, "avoidHighways": highways});
	}
}



function handleErrors(){

	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		alert("We're sorry but we can't find where you're looking for. Please check your spelling and try again. " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
		alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
		alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_BAD_KEY)
		alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
		alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	else alert("An unknown error occurred.");
}


function onGDirectionsLoad(){ 
      // Use this function to access information about the latest load()
      // results.
}


/*
window.onload = function(){

     initialize();
}
*/
