﻿BenefulMap.Directions = Class.extend({
	init: function(mapCanvas, routePanel, fromLocation, toLocation, fromAddressField, toAddressField, addressFieldSwapButton, travelModeButtons, directionsHeadline, directionsGoButton) {
		this.googleMap = new GMap($(mapCanvas).get(0));
		this.googleMap.setCenter(toLocation, 3);
		
		this.googleDirs = new GDirections(this.googleMap, $(routePanel).get(0));
		
		this.fromAddressField = fromAddressField;
		this.toAddressField = toAddressField;
		this.addressFieldSwapButton = addressFieldSwapButton;
		this.travelModeButtons = travelModeButtons;
		this.directionsHeadline = directionsHeadline;
		this.directionsGoButton = directionsGoButton;
		this.travelMode = G_TRAVEL_MODE_DRIVING;

		this.addressFieldSwapButtonListener();
		this.directionsGoButtonListener();
		this.travelModeButtonsListener();
		
		this.setDirectionsMode("driving");
		this.directionsQuery();
	},
	getGoogleDirs: function() {
		return this.googleDirs;
	},
	getGoogleMap: function() {
		return this.googleMap;
	},
	getFromAddressField: function() {
		return this.fromAddressField;
	},
	getToAddressField: function() {
		return this.toAddressField;
	},
	getAddressFieldSwapButton: function() {
		return this.addressFieldSwapButton;
	},
	getTravelModeButtons: function() {
		return this.travelModeButtons;
	},
	getDirectionsHeadline: function() {
		return this.directionsHeadline;
	},
	setDirectionsHeadlineText: function(text) {
		$(this.getDirectionsHeadline()).text(text);
	},
	getDirectionsGoButton: function() {
		return this.directionsGoButton;
	},
	getTravelMode: function() {
		return this.travelMode;
	},
	setTravelMode: function(newTravelMode) {
		this.travelMode = newTravelMode;
	},
	setDirectionsMode: function(directionsMode) {
		switch(directionsMode) {
			case "walking":
					this.setTravelMode(G_TRAVEL_MODE_WALKING);
					this.setDirectionsHeadlineText("Walking Directions");
					$(".directions_available li.walking").css("display","none");
					$(".directions_available li.driving").css("display","block");
					break;
			default:
					this.setTravelMode(G_TRAVEL_MODE_DRIVING);
					this.setDirectionsHeadlineText("Driving Directions");
					$(".directions_available li.walking").css("display","block");
					$(".directions_available li.driving").css("display","none");
		}
		
	},
	directionsGoButtonListener: function() {
		var self = this;
		$(this.getToAddressField() + " , " + this.getFromAddressField()).keypress( function(event) {
		    if (event.which == 13) {
                event.preventDefault();
                $(self.getDirectionsGoButton()).click();
            }
		});
		$(this.getDirectionsGoButton()).click(function(event) {
			event.preventDefault();
			self.setDirectionsMode("driving");
			self.directionsQuery();
		});
	},
	addressFieldSwapButtonListener: function() {
		var self = this;
		$(this.getAddressFieldSwapButton()).click(function(event){
			var toAddress = $(self.getToAddressField()).val();
			var fromAddress = $(self.getFromAddressField()).val();

			$(self.getToAddressField()).val(fromAddress);
			$(self.getFromAddressField()).val(toAddress);
		});
	},
	travelModeButtonsListener: function() {
		var self = this;
		$(this.getTravelModeButtons()).click( function(event) {
			event.preventDefault();
			var mode = $(this).attr("href").replace("#","");
			self.setDirectionsMode(mode);
			self.directionsQuery();
		});
	},
	directionsQuery: function() {
		var options = {};
		options.travelMode = this.getTravelMode();
		var query = $(this.getFromAddressField()).val() + " to " +  $(this.getToAddressField()).val();
		console.log("query: ", query);
		var dirs = this.getGoogleDirs();
		dirs.load(query, options);
		var self = this;
		GEvent.addListener(dirs, "load", function(event) {
			var status = dirs.getStatus();
			console.log("success: ", status);
		});
		GEvent.addListener(dirs, "error", function(event) {
			var status = dirs.getStatus();
			console.log("error: ", status);
			self.setDirectionsHeadlineText("Error in requested directions: Please try again");
			$(self.getFromAddressField()).focus();
		});

	}
});

