/**
 * Map
 * 
 * Wrapper for basic Google maps functions
 *
 * @author Dom Hastings
 */
var Map = {
  /**
   * map
   * 
   * @var object Container for the GMap2 object
   */
  map: null,
  /**
   * gdir
   * 
   * @var object Container for the GDirections object
   */
  gdir: null,
  
	/**
   * setDirections
   * 
   * Runs the get directions request
   *
   * @param fromAddress string The location to get directions from
   * @param toAddress string The location to get directions to
   * @param locale string The locale of the request (unused)
   * @return void
   * @author Dom Hastings
   */
  setDirections: function(fromAddress, toAddress, locale) {
    $('#directions').html('');
    this.gdir.load('from: ' + fromAddress + ' to: ' + toAddress);
  },

	/**
   * displayError
   * 
   * Displays the passed error
   *
   * @param err string The error message to display
   * @return void
   * @author Dom Hastings
   */
  displayError: function(err) {
    $('#directions').html('<p>' + err + '</p>');
    $('#directions').show();
  },

	/**
   * handleErrors
   * 
   * Handles any exceptions
   *
   * @return void
   * @author Dom Hastings
   */
  handleErrors: function() {
    if (this.gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
      this.displayError('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.\nError code: ' + this.gdir.getStatus().code);

    } else if (this.gdir.getStatus().code == G_GEO_SERVER_ERROR) {
      this.displayError('A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: ' + this.gdir.getStatus().code);

    } else if (this.gdir.getStatus().code == G_GEO_MISSING_QUERY) {
      this.displayError('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: ' + this.gdir.getStatus().code);

    // } else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS) { // <--- Doc bug... this is either not defined, or Doc is wrong
    //   alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);

    } else if (this.gdir.getStatus().code == G_GEO_BAD_KEY) {
      this.displayError('The given key is either invalid or does not match the domain for which it was given. \n Error code: ' + this.gdir.getStatus().code);

    } else if (this.gdir.getStatus().code == G_GEO_BAD_REQUEST) {
      this.displayError('A directions request could not be successfully parsed.\n Error code: ' + this.gdir.getStatus().code);

    } else {
      this.displayError('An unknown error occurred.');
    }
  },

	/**
   * onGDirectionsLoad
   * 
   * Function called when the directions have been loaded
   *
   * @return void
   * @author Dom Hastings
   */
  onGDirectionsLoad: function() {
    $('#directions').show();
  },

	/**
   * initialize
   * 
   * Loads the map location lat, lng into element el
   *
   * @param el object The id of the element to load the map into
   * @param lat float The latitude value for the starting location
   * @param lng float The longitude value for the starting location
   * @return void
   * @author Dom Hastings
   */
  initialize: function(el, lat, lng) {
    if (GBrowserIsCompatible()) {
      this.map = new GMap2(document.getElementById(el));
      this.map.addControl(new GSmallMapControl());
      this.map.addControl(new GMapTypeControl());
      this.map.setCenter(new GLatLng(lat, lng), 14);
      this.gdir = new GDirections(this.map, document.getElementById('directions'));

      GEvent.addListener(this.gdir, 'load', function() {
        Map.onGDirectionsLoad();
      });

      GEvent.addListener(this.gdir, 'error', function() {
        Map.handleErrors();
      });
    }
  }
}
