Map = {
  selectedId: null,
  metric: false,
  createMode: false,
  movieId: null,

  movie: function() {
    return $('#' + Map.movieId).get(0);
  },

  saveScreenshotResponse: function(response) {
    response = $.evalJSON(response);
    if(response.status == "success") {
      $('#histo_image_gen_link').hide();
      $('#histo_image_container').show();
      $.jGrowl("Success!");
    } else {
      $.jGrowl("Something went wrong.  We have been notified of the issue and will resolve it as soon as possible.");
    }
  },
  triggerKeyDownCommand: function() {
    
  },
  resizeMap: function() {
    var offset = 31;
    var wh = window.innerHeight ? window.innerHeight-offset : $(window).height()-offset;
    $('.fullpage_left_col,.fullpage_right_col').height(wh);
    $('#map,#new_route_map').height(wh);
    if ($('#save_route_form').length != 0) { 
      $('.fullpage_left_col,.fullpage_right_col').css('height', $(document).height()-offset);
    }
    if ($('#sidebar_pages').length != 0) {
      if ($('#playback_controller').length == 0) {
        $('#sidebar_pages').height(wh-35);
      } else {
        $('#sidebar_pages').height(wh-135);
      }
    }
  },
  
  speedLabel: function(value, label) {
    var newVal = this.metric ? value * 1 : value * 0.621371192;
    return newVal.toFixed(1) + (label ? (this.metric ? " kph" : " mph") : '');
  },

  longLabel: function(value, label) {
    var newVal = this.metric ? value * .001 : value * 0.000621371192;
    return newVal.toFixed(1) + (label ? (label == 'full' ? (this.metric ? ' km' : ' miles') : (this.metric ? " km" : " mi")) : '');
  },

  shortLabel: function(value, label) {
    var newVal = this.metric ? value : value * 3.2808399;
    return Math.round(newVal) + (label ? (label == 'full' ? (this.metric ? ' meters' : ' feet') : (this.metric ? " m" : " ft")) : '');
  },

  toggleOverlay: function(self, type) {
    if($(self).is(':checked'))
      Map.movie().enableOverlay(type);
    else
      Map.movie().disableOverlay(type);
  },

  gotoLocation: function() {
    var loc = $('#location').val();
    Map.movie().gotoLocation(loc);
  },

  setUnits: function(units) {
    Map.movie().setUnits(units);
  },

  loadRouteWithCallback: function(path, activityId, callback) {
    if (Map.movie() && Map.movie().isMapReady && Map.movie().isMapReady()) {
      Map.movie().loadJsonRouteWithCallback(path, activityId, callback);
    } else {
      setTimeout("Map.loadRoute('" + path + "', '" + activityId + "', '" + callback + "')", 200);
    }
  },

  loadRoute: function(path) {
    if (Map.movie() && Map.movie().isMapReady && Map.movie().isMapReady()) {
      Map.movie().loadJsonRoute(path);
    } else {
      setTimeout("Map.loadRoute('" + path + "')", 200);
    }
  },

  clearMap: function(after_cb) {
    if (Map.movie() && Map.movie().isMapReady && Map.movie().isMapReady()) {
      Map.movie().clearMap();
      if(after_cb) after_cb();
    } else {
      setTimeout(function() { Map.clearMap(after_cb) }, 200);
    }
  },
  
  callWhenReady: function(fn) {
    if (Map.movie() && Map.movie().isMapReady && (Map.movie().isMapReady() == true)) {
      fn();
    } else {
      setTimeout(function() { Map.callWhenReady(fn); }, 100);
    }
  },

  geocodingResponse: function(response) {
    if(response['details'] != null) {
      /* no details object if response didn't return a result
       * details object has some good info in it, including
       * city, state/province, country, postalcode.
       * more information found at:
       * http://code.google.com/apis/maps/documentation/services.html
      */

      if(response['details'].Country) {
        $('#user_country_code').val(response['details'].Country.CountryNameCode);
        if(response['details'].Country.AdministrativeArea) {
          var aa = response['details'].Country.AdministrativeArea;
          $('#user_administrative_area').val(aa.AdministrativeAreaName);
          if(aa.Locality) {
            $('#user_locality').val(aa.Locality.LocalityName);
          } else if(aa.SubAdministrativeArea && aa.SubAdministrativeArea.Locality) {
            var l = aa.SubAdministrativeArea.Locality;
            $('#user_locality').val(l.LocalityName);
            if(l.PostalCode)
              $('#user_postal_code').val(l.PostalCode.PostalCodeNumber);
            else
              $('#user_postal_code').val('');
          } else {
            $('#user_locality').val('');
          }
        } else {
          $('#user_administrative_area').val('');
        }
      } else {
        $('#user_country_code').val('');
      }
      $('#user_lat').val(response['lat']);
      $('#user_lng').val(response['lng']);
      $('#map').get(0).setCenterLatLng(response['lat'], response['lng']);
    } else {
      //something went wrong, do something!
      $('#search-error').text('We could not find ' + response['location'] + '.  Please modify the search and try again.');
    }
  }
};
