/**
 * Constructor
 */
function ProxyGoogleMaps() {
}

// ******************************************** Properties

ProxyGoogleMaps.prototype._map;
ProxyGoogleMaps.prototype._gdir;
ProxyGoogleMaps.prototype._oMarkers;
ProxyGoogleMaps.prototype._destination;
ProxyGoogleMaps.prototype._inputFrom;
ProxyGoogleMaps.prototype._inputTo;

// ******************************************** Static constants

ProxyGoogleMaps.prototype.CONTROL_SMALL = "control_small";
ProxyGoogleMaps.prototype.CONTROL_LARGE = "control_large";

// ******************************************** Methods

/**
 * Initialize
 */
ProxyGoogleMaps.prototype.initialize = function (oInit) {

  if (GBrowserIsCompatible()) {
  	
  	this._oMarkers = new Object();
  
  	var zoom = (oInit.zoom)?oInit.zoom:13;
  	
	this._map = new GMap2(oInit.divMap);
	this._map.setCenter(new GLatLng(oInit.long, oInit.lat), zoom);

  }
}

/**
 * Create a marker
 */
ProxyGoogleMaps.prototype.createMarker = function (oMarker) {

	var icon = new GIcon();
	icon.image = oMarker.image;
	icon.shadow = oMarker.shadow;
	icon.iconSize = new GSize(oMarker.width, oMarker.height);
	icon.shadowSize = new GSize(oMarker.widthShadow, oMarker.heightShadow);
	icon.iconAnchor = new GPoint(oMarker.xReg,oMarker.yReg);
	icon.infoWindowAnchor = new GPoint(oMarker.xInfoWindow,oMarker.yInfoWindow);
	
	var point = new GLatLng(oMarker.long, oMarker.lat);
	var markerOptions = { title:oMarker.id, icon:icon };
	
	var marker = new GMarker(point, markerOptions);
	
	this._oMarkers[oMarker.id] = marker;

	this._map.addOverlay(marker);

}

/**
 * Create GDirections
 */
ProxyGoogleMaps.prototype.createGDirections = function (div) {
	
	this._gdir = new GDirections(this._map, div);
    GEvent.addListener(this._gdir, "load", this.handleGDirectionsLoad);
    GEvent.addListener(this._gdir, "error", this.handleErrors);
}

/**
 * Create Input Form
 */
ProxyGoogleMaps.prototype.createInputForm = function (inputFrom,inputTo,inputSubmit) {
	var oThis = this;
	this._inputFrom = inputFrom;
	this._inputTo = inputTo;
	inputSubmit.addEventListener("click",function() { oThis.handleInputSubmit() },false);
}

/**
 * Add Controls
 */
ProxyGoogleMaps.prototype.addControl = function (type) {
	switch (type) {
		case this.CONTROL_SMALL:
			this._map.addControl(new GSmallMapControl());
			break;
		case this.CONTROL_LARGE:
			this._map.addControl(new GLargeMapControl());
			break;
	}
}

// ******************************************** Getters and Setters

ProxyGoogleMaps.prototype.getMap = function () {
	return this._map;
}

ProxyGoogleMaps.prototype.getGDir = function () {
	return this._gdir;
}

ProxyGoogleMaps.prototype.getMarker = function (id) {
	return this._oMarkers[id];
}

/**
 * Set default destination
 */
ProxyGoogleMaps.prototype.setDefaultDestination = function (s) {
	this._destination = s;
	this._inputFrom.value = "address or zip";
}

// ******************************************** Handlers

ProxyGoogleMaps.prototype.handleErrors = function () {
	// this is gdir
	if (this.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + this.getStatus().code);
	else if (this.getStatus().code == G_GEO_SERVER_ERROR)
		alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + this.getStatus().code);
	else if (this.getStatus().code == G_GEO_MISSING_QUERY)
		alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + this.getStatus().code);

	// else if (this.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	// alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + this.getStatus().code);
	 
	else if (this.getStatus().code == G_GEO_BAD_KEY)
		alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + this.getStatus().code);

	else if (this.getStatus().code == G_GEO_BAD_REQUEST)
		alert("A directions request could not be successfully parsed.\n Error code: " + this.getStatus().code);
	
	else alert("An unknown error occurred.");
}

ProxyGoogleMaps.prototype.handleGDirectionsLoad = function(){ 
  // Use this function to access information about the latest load()
  // results.
	
  // e.g.
  // document.getElementById("getStatus").innerHTML = this.getStatus().code;
  // and yada yada yada...
}

ProxyGoogleMaps.prototype.handleInputSubmit = function(){
	var sFrom = this._inputFrom.value;
	var sTo = (this._destination)?this._destination:this._inputTo.value;
 	this.setDirections(sFrom,sTo);
}

ProxyGoogleMaps.prototype.setDirections = function(fromAddress, toAddress) {
      this._gdir.load("from: " + fromAddress + " to: " + toAddress);
}
