// Copyright 2009 Google Inc.
// All Rights Reserved.
// Owner: webgroup@google.com

/**
 * @fileoverview Map for the Google Online Marketing Challenge website.
 *
 * @author amcgrath@google.com (Adam Mcgrath)
 */

var gweb = gweb || {};


/**
 * Gomc namespace.
 * @type {object}
 */
gweb.gomc = gweb.gomc || {};


/**
 * The main map class.
 * @constructor
 * @param {string} el The id of the map element.
 * @param {string} kml The uri of the kml file (must be a fully qualified url).
 */
gweb.gomc.Map = function(el, kml) {
  this.el = document.getElementById(el);
  this.kml = kml;
  google.load('maps', '2');
  google.setOnLoadCallback(this.createApiLoadHandler_());
};


/**
 * Lookup for locations to zoom the map into in format: [lat, long, zoom].
 * @private
 * @type {object}
 */
gweb.gomc.Map.locations_ = {
  'northamerica': [37, -90, 3],
  'africa': [0, 20, 2],
  'southamerica': [-22, -70, 2],
  'europe': [48, 14, 4],
  'asia': [-4, 116, 2],
  'world': [36, 4, 1]
};


/**
 * Creates a handler for the Google AJAX API loading.
 * @return {function} A function that loads the map.
 * @private
 */
gweb.gomc.Map.prototype.createApiLoadHandler_ = function() {
  var self = this;
  return function() {
    self.loadMap_();
  };
};


/**
 * Creates a Google Maps map and overlays the kml on it.
 * @private
 */
gweb.gomc.Map.prototype.loadMap_ = function() {
  this.map = new google.maps.Map2(this.el);
  this.map.addControl(new google.maps.LargeMapControl());
  this.map.setCenter(new google.maps.LatLng(30, 6), 1);
  this.map.addOverlay(new google.maps.GeoXml(this.kml));
};


/**
 * Zooms to a given location if available.
 * @param {string} loc The location.
 */
gweb.gomc.Map.prototype.zoomTo = function(loc) {
  var arr = gweb.gomc.Map.locations_[loc];
  if (arr && arr.length == 3) {
    this.map.setCenter(new google.maps.LatLng(arr[0], arr[1]), arr[2]);
  }
};

