Directions = Class.create({

	gdir: null,
	
	initialize: function(application, map) {

		this.app = application;
		this.map = map;
		this.gMap = this.map.gMap;
					
		this.gdir = new GDirections(this.gMap, null);
		GEvent.addListener(this.gdir, "load", this.onGDirectionsLoad.bind(this));
		GEvent.addListener(this.gdir, "error", this.handleErrors.bind(this));
		
	},
	
	// Error handler for directions overlay
	handleErrors: function() {
		
		gdir = this.gdir;
		
		if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
			alert("Osoitteen paikkaa ei pystytty määrittämään. Tämä voi johtua siitä, että osoite on niin uusi ettei sitä löydy karttapalvelusta, tai siinä voi olla virhe. Tarkista kirjoitusasu.");

		else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
			alert("Osoitteen muuntaminen koordinaateiksi tai reitinhaku ei onnistunut.");
		
		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. 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. Error code: " + gdir.getStatus().code);
		
		else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
			alert("A directions request could not be successfully parsed. Error code: " + gdir.getStatus().code);
		
		else alert("Tuntematon virhe. Yritä myöhemmin uudelleen.");	
		
	},
	
	// We have a successful query, update URL
	onGDirectionsLoad: function() { 
		
		this.app.d = this.gdir.getDistance();
		this.app.t = this.gdir.getDuration();
		this.app.showResults();
		
	},
	
	setDirections: function(from, to) {
		
		// Loads the overlay
		this.gdir.load("from: " + from + " to: " + to);
		
	}
	
});

