(function($) {
  var map = new base2.Package(this, {
    name:    "map",
    version: "1.0",
    parent:  dashfly,
    imports: "dashfly",
    exports: "MapManager"
  });
  
  eval(this.imports);

  var geocoder = new GClientGeocoder();

  var MapManager = base2.Base.extend({
	  getMap: function() { return mainMapObj; },
  	  
	  centerPointsInMap: function(points) {
		  var line = new GPolyline(points);
		  var zoom = this.getMap().getBoundsZoomLevel(line.getBounds());
		  this.getMap().setCenter(line.getBounds().getCenter(), zoom);
	  },
	  
	  geocode: function(address, block, context) {
		  geocoder.getLocations(address.GetAddress(), function(response) {
			  var found = (response.Status.code == G_GEO_SUCCESS);
			  if (response && found) {
				  var place = response.Placemark[0];
				  var coord = place.Point.coordinates;
				  address.SetActualAddress(place.address);
				  address.SetLatLng(new GLatLng(coord[1], coord[0]));
				  address.SetAccuracy(response.Placemark[0].AddressDetails.Accuracy)
			  }
			  if ($.isFunction(block)) {
				  block.call(context, address, found);
			  }
		  });
	  },
	  
	  reverseGeocode: function(address, block, context) {
		  geocoder.getLocations(address.GetLatLng(), function(response) {
			  var found = (response.Status.code == G_GEO_SUCCESS);
			  if (response && found) {
				  var place = response.Placemark[0];
				  var location = place.address.replace(", USA", "");
				  address.SetAddress(location);
				  address.SetActualAddress(location);
				  address.SetAddressLabel(location);
			  }
			  if ($.isFunction(block)) {
				  block.call(context, address, found);
			  }
		  });
	  }	  
  });
  
  var MapModule = Module.extend({
	  load: function() {
	  	 this.addService(new MapManager());
	  }
  });
  
  eval(this.exports);
})(jQuery);
