// Default suff
var gMap = null;
var gGeocoder = null;
var gMapControl = null;
var gMapTypeControl = null;
var gZero = new GLatLng(0.0, 0.0, false);

function initGMaps(mapDiv, mapControl, mapTypeControl) {
  if (GMap2 && GBrowserIsCompatible()) {
    gGeocoder       = new GClientGeocoder();
    gMap            = new GMap2($(mapDiv));
    gMapControl     = (mapControl)? mapControl: new GSmallMapControl();
    gMapTypeControl = mapTypeControl;

    gMap.addControl(gMapControl);
    if (gMapTypeControl) { gMap.addControl(gMapTypeControl); }

    gMap.setCenter(new GLatLng(37.0625, -95.677068, false), 3);
  }
  else {
    $(mapDiv).innerHTML = "Google maps cannot be loaded.<br>Please click the View Larger link below.";
  }
}

var KAddress = Class.create();
KAddress.prototype = {
  initialize: function(gMap, gGeocoder, sName, sAddress, labelText, lat, lng, fGeocodeCallback) {
    this.map     = gMap;
    this.gc      = gGeocoder;
    this.name    = sName;
    this.address = sAddress;
    this.point   = new GLatLng(lat, lng, false);
    if (labelText && labelText.length > 0) {
      this.markerHtml = labelText;
    } else {
      this.markerHtml = this._defaultMarkerHtml();
    }
    this.geocode(fGeocodeCallback);
  },
  _defaultMarkerHtml: function() {
    var sMarkerText = this.address;
    if (this.name && this.name.length > 0) {
      sMarkerText = "<b>"+this.name+"</b><br>"+sMarkerText;
    }
    return sMarkerText;
  },
  geocode: function(fGeocodeCallback) {
    this.gc.getLatLng(this.address,function(gllPoint) {
      if (gllPoint) {
        this.point = gllPoint;
      }
      else if (this.point.equals(gZero)) {
        gMap.getContainer().innerHTML = " Google maps is unable to fine '" + this.address + "'.<br>Please click the View Larger link below.";
        return;
      }
      
      this.marker = this._newMarker(this.point);
      this.mark();
      if (fGeocodeCallback) {
        fGeocodeCallback();
      }
    }.bind(this));
  },
  _newMarker: function(gllPoint) {
    var mk = new GMarker(gllPoint);
    GEvent.addListener(mk,"click",function() {
      this.openInfoWindow(false);
    }.bind(this));
    return mk;
  },
  mark: function() {
    this.map.addOverlay(this.marker);
    this._marked = true;
  },
  openInfoWindow: function(bCenter) {
    if(!this._marked) {
      this.mark();				
    }
    if(bCenter) {
      this.centerOn();
    }
    this.marker.openInfoWindowHtml(this.markerHtml);
    $("gMapsLink").href = "http://maps.google.com?q="+escape(this.address);
  },
  centerOn: function() {
    this.map.setCenter(this.point);
  },
  getOpenInfoWindowEventListener: function() {
    return function() { this.openInfoWindow(true); }.bindAsEventListener(this);
  }
};

var KMarkerHtml = Class.create();
KMarkerHtml.prototype = {
  initialize: function(gMap, lat, lng, sName, labelText, zoomLevel) {
    this.map    = gMap;
	this.point  = new GLatLng(lat, lng, false);
    this.marker = this._newMarker(this.point);
    this.name   = sName;
    this.markerHtml = labelText;
    this.mark();
  },
  _newMarker: function(gllPoint) {
    var mk = new GMarker(gllPoint);
    GEvent.addListener(mk,"click",function() {
      this.openInfoWindow();
    }.bind(this));
    return mk;
  },
  mark: function() {
    this.map.addOverlay(this.marker);
    this._marked = true;
  },
  centerOn: function() {
    this.map.setCenter(this.point, 13);
  },
  openInfoWindow: function() {
    if(! this._marked) {
      this.mark();				
    }

    this.centerOn();
    this.marker.openInfoWindowHtml(this.markerHtml);
  }
};

var KMarker = Class.create();
KMarker.prototype = {
  initialize: function(gMap, lat, lng, sName, domId, zoomLevel) {
    this.map    = gMap;
	this.point  = new GLatLng(lat, lng, false);
    this.marker = this._newMarker(this.point);
    this.name   = sName;
    this.markerHTML = $(domId);
    this.mark();
  },
  _newMarker: function(gllPoint) {
    var mk = new GMarker(gllPoint);
    GEvent.addListener(mk,"click",function() {
      this.openInfoWindow();
    }.bind(this));
    return mk;
  },
  mark: function() {
    this.map.addOverlay(this.marker);
    this._marked = true;
  },
  centerOn: function() {
    this.map.setCenter(this.point, 13);
  },
  openInfoWindow: function() {
    if(! this._marked) {
      this.mark();				
    }

    this.centerOn();
    this.marker.openInfoWindow(this.markerHTML);
  }
};

