
var _this_CGeoCoder;
var GeoCode_Addressvalid = true;

function CGeocoder()
{
  this.ClientGeocoder = new GClientGeocoder();
  this.objLattitude = null;
  this.objLongitude = null;
}

CGeocoder.prototype.ValidateAddress = function(objLattitude, objLongitude, AddressString)
{
  this.objLattitude = objLattitude;
  this.objLongitude = objLongitude;
  _this_CGeoCoder = this;
  GeoCode_Addressvalid = false;
  this.ClientGeocoder.getLocations(AddressString, this.CallbackValidateAddress);
}

CGeocoder.prototype.CallbackValidateAddress = function(GLatLng)
{
  if(!GLatLng || GLatLng.Status.code != 200) {
  //  alert("Adress could not be resolved!");
    //document.getElementById(this.FocusId).focus();
    return;
  }

  if(GLatLng.Placemark.length > 1) {
  //  alert("Adress could not be resolved!");
    //document.getElementById(this.FocusId).focus();
    return;
  }

  GeoCode_Addressvalid = true;

  var place = GLatLng.Placemark[0];
  _this_CGeoCoder.objLattitude.value = place.Point.coordinates[1]; //GLatLng.lat();
  _this_CGeoCoder.objLongitude.value = place.Point.coordinates[0]; //GLatLng.lng();
}

function CGoogleMap(DivId)
{
  this.ObjGMap = new GMap2(document.getElementById(DivId));
  this.ObjGMarkerManager = new GMarkerManager(this.ObjGMap);
  this.BoundsMap = new GLatLngBounds();
}

CGoogleMap.prototype.getBoundsZoomLevel = function(bounds)
{
  return this.ObjGMap.getBoundsZoomLevel(bounds);
}

CGoogleMap.prototype.setCenter = function(point, zoom)
{
  this.ObjGMap.setCenter(point, zoom);
}

CGoogleMap.prototype.SetLargeMapControls = function ()
{
  this.ObjGMap.addMapType(G_PHYSICAL_MAP);
  var mapControl = new GHierarchicalMapTypeControl();

  mapControl.clearRelationships();
  mapControl.addRelationship(G_SATELLITE_MAP, G_HYBRID_MAP, "Labels", false);
  this.ObjGMap.addControl(mapControl);

  this.ObjGMap.addControl(new GLargeMapControl());
  this.ObjGMap.addControl(new GScaleControl());

  this.ObjGMap.setMapType(G_PHYSICAL_MAP);
}

CGoogleMap.prototype.SetSmallMapControls = function ()
{
  this.ObjGMap.addMapType(G_PHYSICAL_MAP);
  var mapControl = new GHierarchicalMapTypeControl();

  mapControl.clearRelationships();
  mapControl.addRelationship(G_SATELLITE_MAP, G_HYBRID_MAP, "Labels", false);
  this.ObjGMap.addControl(mapControl);

  this.ObjGMap.addControl(new GSmallMapControl());
  this.ObjGMap.setMapType(G_PHYSICAL_MAP);
}

CGoogleMap.prototype.AddMarker = function(Description, MarkImgUrl, Lat, Lng, InfoHtml, DetailUrl)
{
  var LatLng = new GLatLng(Lat, Lng);
  var Icon = new GIcon();
  Icon.image = MarkImgUrl;
  Icon.iconAnchor = new GPoint(0, 0);
  Icon.infoWindowAnchor = new GPoint(10, 0);

  var Marker = new GMarker(LatLng, {icon:Icon, title:Description});
  this.ObjGMarkerManager.addMarker(Marker, 1);

  GEvent.addListener(Marker, "mouseover", function() { Marker.openInfoWindowHtml( InfoHtml); })
  GEvent.addListener(Marker, "mouseout", function() { Marker.closeInfoWindow(); })
  GEvent.addListener(Marker, "click", function() { window.location = DetailUrl; })

  this.BoundsMap.extend(LatLng);
}

CGoogleMap.prototype.FitMapToMarkers = function() {
  this.ObjGMap.setCenter(this.BoundsMap.getCenter(), this.ObjGMap.getBoundsZoomLevel(this.BoundsMap));
}

CGoogleMap.prototype.CenterMapToMarker = function(lat, lng) {
  this.ObjGMap.setCenter(new GLatLng(lat, lng), 13);
}
