/**
 * RunTrackr Static Maps/Routes.
 *
 * Utility methods for working with static maps.  Currently the static maps
 * are based off of the Google Static Maps API:
 * http://code.google.com/apis/maps/documentation/staticmaps/
 *
 * @namespace runtrackr.StaticMap.
 * @requires runtrackr.js, Google Maps, jQuery >= 1.2.6
 */

// TODO:
// 1) How to rectifiy if > 50 points? Truncate or use more advanced algorithm
// based on the encoded_levels?

/**
 * Defaults/constants.
 */
runtrackr.StaticMap =
{
  URL: 'http://maps.google.com/staticmap',
  COORDINATES_PRECISION : 6, // NOTE: Encoded polyline provides only 5 decimal points of precision!
  DEFAULT_ZOOM: 13,
  DEFAULT_FORMAT: 'gif', // GIF provides best trade-off between quality and size.
  MAPS_API_KEY: ''
};

/**
 * Try to get the API key if available.
 */
jQuery(function()
{
  // Google Maps API key should always ben stored in an input with this id if
  // using the static maps.
  var mapsApiKey = jQuery('#maps-api-key');
  if (0 == mapsApiKey.length)
  {
    return;
  }

  runtrackr.StaticMap.MAPS_API_KEY = mapsApiKey.val();
});

/**
 * Returns the URL to the Google Maps Static Image with an overlaid path/route
 * corresponding to the encodedPoints supplied.
 *
 * @requires Google Maps.
 * @param encodedPoints the encoded points.
 * @param encodedLevels the encoded levels.
 * @param mapsApiKey the Google Maps API key to use.
 * @param options map options. (optional)
 * @return the URL to the static map.
 */
runtrackr.StaticMap.getImageUrl = function(encodedPoints, encodedLevels, options)
{
  // TODO: Need an array/object MERGE function so that defaults can be
  // automatically overwritten in one-step...

  // Default values.
  options = options ? options : {};
  var size = options.size ? options.size : '300x300';
  var frame = options.frame ? options.frame : 'true';
  var pathColor = options.pathColor ? options.pathColor : '0x0000ff';
  var pathWeight = options.pathWeight ? options.pathWeight : '3';
  var zoom = options.zoom ? options.zoom : runtrackr.StaticMap.DEFAULT_ZOOM;
  var format = options.format ? options.format : runtrackr.StaticMap.DEFAULT_FORMAT;
  var mapsApiKey = options.mapsApiKey ? options.mapsApiKey : runtrackr.StaticMap.MAPS_API_KEY;

  // Convert into a GPolyline so can extract the individual points/coordinates.
  // (Why doesn't Static Maps support the encoded polyline?)
  var route = GPolyline.fromEncoded(
  {
    // These options aren't used.  We just need to decode the encoded polyline
    // to extract the coordinates.
   'color' : '#FF0000',
   'weight' : 3,
   'points' : encodedPoints,
   'levels' : encodedLevels,
   'zoomFactor' : 2,
   'numLevels' : 18
  });

  // Build the list of points for the static maps path.
  var nRoutePoints = route.getVertexCount();
  var points = '';
  var precision = runtrackr.StaticMap.COORDINATES_PRECISION;
  for (var i = 0; i < nRoutePoints; ++i)
  {
    var point = route.getVertex(i);
    points += '|' + point.lat().toFixed(precision) + ',' +
      point.lng().toFixed(precision);
  }

  // Place a marker at the start position and end position.
  var startPoint = route.getVertex(0);
  var endPoint = route.getVertex(nRoutePoints - 1);
  var markers = startPoint.lat().toFixed(precision) + ',' +
    startPoint.lng().toFixed(precision) + ',greens|' +
    endPoint.lat().toFixed(precision) + ',' +
    endPoint.lng().toFixed(precision) + ',redf';

  // Build the query string to obtain the map. (Note that setting the 'src'
  // appears to automatically encode entities properly as required by the DOM)
  var staticMapsQueryString = 'key=' + mapsApiKey + '&markers=' + markers +
    '&zoom=' + zoom + '&frame=' + frame + '&size=' + size + '&format=' + format +
    '&path=rgb:' + pathColor + ',weight:' + pathWeight + points;
  var staticMapsUrl = runtrackr.StaticMap.URL + '?' + staticMapsQueryString;

  return staticMapsUrl;
}