//
// googlemaps_helper.js
//
GmapHelper = function(data) {
  this.init(data);
};

GmapHelper.prototype = {
  data : null,
  map : null,
  geocdr : null,
  
  init : function (data) {
    this.data = data;
    this.map = new GMap2(document.getElementById(this.data.id));
    this.map.enableContinuousZoom();
    this.map.addControl(new GSmallZoomControl());
    this.map.addControl(new GOverviewMapControl());
    this.geocdr = new GClientGeocoder();
    if (this.data.text) {
      this.infoHTML = '<span style="font-size : small">' + this.data.text + '</span>';
    }
  },
  
  show : function () {
    var map = this.map;
    var nfo = this.infoHTML;
    this.geocdr.getLatLng(this.data.location, 
      function (latlng) {
        if (latlng) {
          map.setCenter(latlng, 13);
          if (nfo) {
            var marker = new GMarker(latlng);
            map.addOverlay(marker);
            marker.title = nfo;
            marker.openInfoWindowHtml(nfo);
            GEvent.addListener(marker, 'click', function() {
              this.openInfoWindowHtml(this.title);
            });
          }
        } else {
          document.getElementById(this.data.id).innerHTML = '<p>the address cannnot be converted to geocoding.</p>';
        }
      }
    );
  }
};

function GmapHelperBeforeUnload(func) {
  if (GBrowserIsCompatible()) {
    window.attachEvent ?
    window.attachEvent('onbeforeunload',func) : 
    window.addEventListener('beforeunload',func,false);
  }
}

function GmapHelperOnLoad(func) {
  if (GBrowserIsCompatible()) {
    window.attachEvent ?
    window.attachEvent('onload', func) : 
    window.addEventListener('load', func, false);
  } else {
    document.getElementById(this.data.id).innerHTML = '<p>the address cannnot be converted to geocoding.</p>';
  }
}
