﻿function GoogleMap($element, $zoom) {

  this.element = null;
  this.map = new GMap2(document.getElementById($element));
  this.map.addControl(new GLargeMapControl());
  this.map.addControl(new GMapTypeControl());
  this.geoCoder = new GClientGeocoder();
  this.zoom = $zoom ? $zoom : 10;

  this.relance = false;
  this.html = "";
  this.icon = "";

  var $this = this;

  this.chercheAdresse = function GoogleMap_chercheAdresse($criteres, $zoom) {
    this.zoom = $zoom;
    if (GBrowserIsCompatible())
      this.geoCoder.getLocations($criteres, this.waitResponse);
  }

  this.waitResponse = function GoogleMap_waitResponse($response) {
    $this.map.clearOverlays();
    if (!$response || $response.Status.code != 200) {
      if (!$this.relance) {
        $this.relance = true;
        $this.geoCoder.getLocations($this.ville, $this.addToMap);
      }
    }
    else {
      var $place = $response.Placemark[0];
      $this.addToMap($place.Point.coordinates[1], $place.Point.coordinates[0]);
      return true;
    }
  }

  this.addToMap = function GoogleMap_addToMap($latitude, $longitude) {
    var $point = new GLatLng($latitude, $longitude);
    var $icon = new GIcon();
    $icon.image = this.icon;
    $icon.iconSize = new GSize(40, 40);
    $icon.iconAnchor = new GPoint(20, 20);
    $icon.infoWindowAnchor = new GPoint(20, 2);
    $icon.infoShadowAnchor = new GPoint(18, 25);
    var $marker = new GMarker($point, { icon: $icon });
    $this.map.setCenter($point, this.zoom);
    $this.map.addOverlay($marker);
    $marker.openInfoWindowHtml($this.html);
  }

}
