//////////////////////////////////////////////
// Site map objects and functions.
//////////////////////////////////////////////

//Global map instance
var map;

/////////////////////////////////////////////////////////////

/**
 * Map callback initalizer class
 */
function MapInitializerCallback(divId, mapPlacer) {
  this._divId = divId;

  if (mapPlacer != null) {
    this._placer = mapPlacer;
  } else {
    this._placer = new MapPlacer(-34.649285, -58.619784, 13);
  }
}

MapInitializerCallback.prototype = new InitializerCallback();

MapInitializerCallback.prototype._divId;

MapInitializerCallback.prototype._placer;

MapInitializerCallback.prototype.load = function() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById(this._divId));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    this._placer.place();
    map.enableScrollWheelZoom();
  }
}

MapInitializerCallback.prototype.unload = function() {
  GUnload();
}

/////////////////////////////////////////////////////////////

/**
 * MapAttributes class
 */
function MapAttributes() {
}

MapAttributes.prototype.getCenter = function() {
  return map.getCenter();
}

MapAttributes.prototype.getBounds = function() {
  return map.getBounds();
}

/////////////////////////////////////////////////////////////

/**
 * LocationMarkersCreator class
 */
function LocationMarkersCreator(data) {
  //clear previous marker
  map.clearOverlays();

  //add new ones
  for (i = 0 ; i < data.length ; i++) {
    marker = new Marker(data[i]);
    marker.addTo(map);
  }
}

/////////////////////////////////////////////////////////////

/**
 * Marker class
 */
function Marker(location) {
  this._location = location;
  this._markerInfo = new MarkerInfo(location);
}

Marker.prototype._location;
Marker.prototype._markerInfo;

Marker.prototype.addTo = function(aMap) {
  // build icon
  markerIcon = new GIcon(G_DEFAULT_ICON);
  markerIcon.image = "/static/images/markers/" + this._location.image;
  markerOptions = { icon: markerIcon };

  //build marker
  gMarker = new GMarker(this.buildLatLng(), markerOptions);

  //add info window
  this._markerInfo.addTo(gMarker);

  //place it on map
  aMap.addOverlay(gMarker);
}

Marker.prototype.addToAndPan = function(aMap) {
  // call addTo
  this.addTo(aMap);

  //pan map to new spot
  aMap.panTo(this.buildLatLng());
}

Marker.prototype.buildLatLng = function() {
  return new GLatLng(this._location.lat, this._location.lng);
}

/////////////////////////////////////////////////////////////

/**
 * MarkerInfo
 */
function MarkerInfo(location) {
  this._location = location;
}

MarkerInfo.prototype._location;

MarkerInfo.prototype.addTo = function(aMarker) {
  info = "<b>".concat(this._location.name).concat("</b><br/>");
  info = info.concat("<i>").concat(this._location.address).concat("</i>");

  aMarker.bindInfoWindowHtml(info, {});
}

/////////////////////////////////////////////////////////////

/**
 * LocationMarkerCreator
 */
function LocationMarkerCreator(location) {
  //clear previous marker
  map.clearOverlays();

  //add new one and center map on it
  marker = new Marker(location);
  marker.addToAndPan(map);
}

/////////////////////////////////////////////////////////////

/**
 * MapPlacer
 */
function MapPlacer(lat, lng, zoom) {
  this._lat = lat;
  this._lng = lng;
  this._zoom = zoom;
}

MapPlacer.prototype._lat;

MapPlacer.prototype._lng;

MapPlacer.prototype._zoom;

MapPlacer.prototype.place = function() {
  map.setCenter(new GLatLng(this._lat, this._lng), this._zoom);
}
