/** Map.Directions
* A Singleton class that provides the methods for working with the directions feature.
* 
* @class Map.Directions
*	@singleton
* @returns {void}
*/
Map.Directions = new function() {
	
	this.gDirections = null;	// Class variable to hold the directions object
	this.directionsContainer = null;
	// An object to hold the last user query
	this.lastUserQuery = {
		startAddress: '',
		destinationAddress: '',
		opts: null
  }
	
	/** init
	* Initialize the Map Directions object
	*
	* @method init
	* @returns {void}
	*/
	this.init = function() {
		var self = this;
		var Event = YAHOO.util.Event;
		
		this.directionsContainer = document.getElementById( "dircontainer" );
		this.gDirections = new GDirections( Map.map, document.getElementById( "directions" ) );
		
		// === Array for decoding the failure codes ===
		var reasons=[];
		reasons[G_GEO_SUCCESS]            = "Success";
		reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
		reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
		reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
		reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
		reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
		reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";
		reasons[G_GEO_BAD_REQUEST]        = "A directions request could not be successfully parsed.";
		reasons[G_GEO_MISSING_QUERY]      = "No query was specified in the input.";
		reasons[G_GEO_UNKNOWN_DIRECTIONS] = "The GDirections object could not compute directions between the points.";
		
		// === catch Directions errors ===
		GEvent.addListener( this.gDirections, "error", function() {
			var code = self.gDirections.getStatus().code;
			var reason = "Code " + code;
			if (reasons[ code ] ) {
				reason = reasons[ code ];
			}
		
			alert( "Failed to obtain directions, " + reason );
		} );
		
		GEvent.addListener( this.gDirections, "load", function() {
			//document.documentElement.scrollTop = YAHOO.util.Dom.getY( self.directionsContainer );
			if( !YAHOO.lang.isUndefined( document.documentElement.scrollTop ) )
				document.documentElement.scrollTop = YAHOO.util.Dom.getY( self.directionsContainer );
			else
				document.body.scrollTop = YAHOO.util.Dom.getY( self.directionsContainer );
		} );
		
		// Setup click listeners on the Close Directions and Print Directions Buttons
		Event.addListener( 'closeDirectionsButton', 'click', this.closeDirections, this, true );
		Event.addListener( 'printDirectionsButton', 'click', this.printDirections, this, true );
	}
	
	
	
	
	/** getDirections
	* Shows the directions to or from a marker
	*
	* @method getDirections
	* @returns {void}
	*/
	this.getDirections = function() {
		Map.map.savePosition();		// Save current map position, so when the directions are closed, the user can be returned here
		
		// Set up the walk and avoid highways options
		var opts = {};
		if ( document.getElementById( "walk" ).checked )
			opts.travelMode = G_TRAVEL_MODE_WALKING;
		if ( document.getElementById( "highways" ).checked )
			opts.avoidHighways = true;
			
		// Set the start and destination address values
		this.lastUserQuery.startAddress = document.getElementById( "saddr" ).value;
		this.lastUserQuery.destinationAddress = document.getElementById( "daddr" ).value;
		this.lastUserQuery.opts = opts;
		this.gDirections.load( "from: " + this.lastUserQuery.startAddress + " to: " + this.lastUserQuery.destinationAddress, opts);
		
		this.directionsContainer.style.display = "block";
		
	}
	
	
	/** closeDirections
	* Closes (hides) the directions section
	*
	* @method closeDirections
	* @returns {void}
	*/
	this.closeDirections = function() {
		this.gDirections.clear();
		this.directionsContainer.style.display = "none";
		
		Map.map.returnToSavedPosition();
	}
	
	
	
	/** printDirections
	* Prints the directions in a new window
	*
	* @method printDirections
	* @returns {void}
	*/
	this.printDirections = function() {

		//<a href="http://maps.google.com/maps?f=d&saddr=" + start + "&daddr=" + end + "hl=en&pw=2" /a>
		var query = "http://maps.google.com/maps?f=d&saddr=" + this.lastUserQuery.startAddress  + "&daddr=" + this.lastUserQuery.destinationAddress + "&hl=en&pw=2";
		
		// Open a window for printing
		window.open( query, '', 'scrollbars=yes,menubar=yes,height=600,width=800,resizable=yes,toolbar=yes,location=yes,status=yes' );
	}
}