/**
 * @author Leon
 */
 	var map;
    var gdir;
    var geocoder = null;
    var addressMarker;
    var ffall = 0;
    var km = 0
 
    var StartRate = 1.50;
    var TaxiMi = 1.35;
	var TaxiMi2 = 1.80;
    
    function initialize() {
      if (GBrowserIsCompatible()) {      
        map = new GMap2(document.getElementById("map_canvas"));
        //map.disableDragging()
        //map.setUIToDefault();
        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "addoverlay", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
      }
    }
    
    function setDirections(fromAddress, toAddress) {
        if(fromAddress == ""){
           alert("Both Pick up address and Destination address are required.");
		} else if(toAddress == ""){
            alert("Both Pick up address and Destination address are required.");
		} else {
      		gdir.load("from: " + fromAddress + " to: " + toAddress, {"locale": "uk"});
			onGDirectionsLoad();
        }
    }
    
    function handleErrors(){
		if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
			alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.");
		} 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.");
		} 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.");
		} 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.");
		} else if (gdir.getStatus().code == G_GEO_BAD_REQUEST) {
			alert("Both Pick up address and Destination address are required.");
		} else {
			alert("An unknown error occurred.");
		}
    }
 
function onGDirectionsLoad(){
	var init = 0;
	//Works out the distance in miles thanks to meters/1609
	distance = gdir.getDistance().meters/1609;
	distance = distance.toFixed(2);
	if(distance <= 6) {
		final = distance*TaxiMi;
	} else if(distance > 6 && distance <= 13) {
		init = 6*TaxiMi;
		carry = distance-6;
		if(carry > 0) {
			final = 0;
			for(i = carry; i > 0; i--) {
				final = final+TaxiMi2;
			}
		}
	} else if(distance > 13) {
		init = 6*TaxiMi;
		init = init+7*TaxiMi2;
		leftover = distance-13;
		if(leftover > 0) {
			final = 0;
			for(i = leftover; i > 0; i--) {
				final = final+TaxiMi;
			}
		}
	}
	final = final + 1.50;
	final = final + init;
    minutes = Math.round(gdir.getDuration().seconds/60); 
	document.getElementById("tDistance").innerHTML = "&pound;" + final.toFixed(2);
	document.getElementById("tDuration").innerHTML = minutes + " minutes";
	var poly = gdir.getPolyline();
   	var baseUrl = "http://maps.google.com/staticmap?";

   	params = [];
   	var markersArray = [];
   	markersArray.push(poly.getVertex(0).toUrlValue(5) + ",greena");
   	markersArray.push(poly.getVertex(poly.getVertexCount()-1).toUrlValue(5) + ",greenb");
 
   	var polyParams = "rgba:0x0000FF80,weight:5|";
   	var polyLatLngs = [];
   	for (var j = 0; j < poly.getVertexCount(); j++) {
     	polyLatLngs.push(poly.getVertex(j).lat().toFixed(5) + "," + poly.getVertex(j).lng().toFixed(5));
   	}
 
   	baseUrl += params.join("&");
}
function resetpage() {
javascript:history.go(0)
javascript:document.forms[0].reset()
}
 
function calculate() {
javascript:setDirections(fromAddress=(document.getElementById('from').value)+' England',toAddress=(document.getElementById('to').value)+' England');
}
 
