// ======================================================================================
// VisioGoogleMapper
// --------------------------------------------------------------------------------------
// Doel : Maakt een GoogleMaps Object, waarmee je o.a. de route kunt laten berekenen.
// Door : L.P.L. Mazzacani
//
// Maakt gebruik van:
//	- Google Maps
//  - JQuery
// 
// Known issues: 
//
// Ideën voor uitbreiding:
//
// Aanpassingen/uitbreidingen :
//	2009.02.19.LM : Eerste versie (Edmond's voorbeeld in een Object gepropt)
// --------------------------------------------------------------------------------------
// (c)2009, Visio BV
// ======================================================================================
function TVisio_GoogleMapper090219LM (
	AKaartElementID,					// Bijvoorbeeld '#map'
	ARouteBeschrijvingElementID,		// Bijvoorbeeld '#route'
	AAdres,								// Bijvoorbeeld 'Geestweg 62, 2371 AG Roelofarendsveen
	AZoomLevel							// Bijvoorbeeld 13
) {

	// Instelbare properties:
	this.KaartElementID              = AKaartElementID;
	this.RoutebeschrijvingElementID	 = ARouteBeschrijvingElementID;
	this.LocatieAdres                = AAdres;
	this.ZoomLevel                   = AZoomLevel;
	
	this.msg_AdresNietGevonden		 = 'Het ingevoerde adres is niet gevonden.';
	
	// Object Variabelen (properties) definiëren
	this.map          = null;			// Het GoogleMaps Object.
	this.gdir         = null;			// Het Routeplanner Googlemaps Object
	this.geocoder     = null; 			// De Geocoder (bepaalt lattitude en logitude voor een adres)
	this.icons        = new Array();    // Meerdere Icons

	// Voor latere referenties naar 'dit'.
	var self = this;

	// Privates (so please do not (ab)use
	this.priv_iconcounter = 0;
	
	// Methods...
	//---------------------------------------------------------------------
	
	// AddMarker
	this.AddMarker=function(AAdres, AMarkerHtml, AZoomlevel) {
		if ((this.map) && (this.geocoder)) {
 			this.geocoder.getLatLng (
				AAdres,
              	function(point) {
                	if (!point) {
                  		alert(AAdres + " niet gevonden");
                } else {
                  self.map.setCenter(point, AZoomlevel);
                  var marker = new GMarker(point);
                  self.map.addOverlay(marker);
                  marker.openInfoWindowHtml(AMarkerHtml);
                }
              }
            );			
		}
	}

	
	this.AddIcon=function(AIconImage, AIconWidth, AIconHeight, AIconAnchorX, AIconAnchorY, AIconShadowImage, AIconShadowWidth, AIconShadowHeight, /*AIconShadowAnchorX, AIconShadowAnchorY,*/ AInfoWindowAnchorX, AInfoWindowAnchorY) {
		if (this.IconByImage(AIconImage)) {
			return this.IconByImage(AIconImage); // Bestond al :)
		} else { 
			this.icons[this.priv_iconcounter] = new(GIcon);
			if (this.icons[this.priv_iconcounter]) {
				this.icons[this.priv_iconcounter].image        = AIconImage;
				this.icons[this.priv_iconcounter].iconSize     = new GSize(AIconWidth, AIconHeight);
				this.icons[this.priv_iconcounter].iconAnchor   = new GPoint(AIconAnchorX, AIconAnchorY);
				this.icons[this.priv_iconcounter].shadow       = AIconShadowImage;
				this.icons[this.priv_iconcounter].shadowSize   = new GSize(AIconShadowWidth, AIconShadowHeight);
				// this.icons[this.priv_iconcounter].shadowAnchor = new GPoint(AIconShadowAnchorX, AIconShadowAnchorY);
				this.icons[this.priv_iconcounter].infoWindowAnchor = new GPoint(AInfoWindowAnchorX, AInfoWindowAnchorY);
				this.priv_iconcounter ++;
			}
		}
	}
	
	// IconCount
	this.IconCount=function(){ return this.priv_iconcounter; }
	
	// IconByID
	this.IconByID=function(AIconID) {
		if ((AIconID < this.IconCount()) && (AIconID >= 0) ) {
			return this.icons[AIconID];
		} else {
			return null;
		}
	}
	
	// IconByImage
	this.IconByImage=function(AIconImage) {
		intIconID = 0;
		while (intIconID <= this.IconCount) {
			if (this.IconByID[intIconID].image == AIconImage) {
				return this.IconByID[intIconID];
			}
		}
		return null;	// Jammer dan, bestaat niet :)
	}
	
	// AddMarkerWithIcon 
	this.AddMarkerWithIcon=function(AAdres, AMarkerHtml, AZoomLevel, AIconID) { 
		if ((this.map) && (this.geocoder)) {
			this.geocoder.getLatLng(
				AAdres,
				function(point) {
					if (!point) {
						alert(this.msg_AdresNietGevonden);
					} else {
						if (self.IconByID(AIconID)) {
							self.map.setCenter(point, AZoomLevel);
							var marker = new GMarker(point, self.IconByID(AIconID));
							self.map.addOverlay(marker);
							if ((AMarkerHtml) && (AMarkerHtml != '')) {
								marker.openInfoWindowHtml(AMarkerHtml);
							}
						} else {
							self.AddMarker(AAdres, AMarkerHtml, AZoomLevel);			
						}
					}
				}
			);
		}
	}
	
	
	/* AddCustomisedMarker
	this.AddCustomisedMarker=function(AAdres, AMarkerHtml, AZoomlevel, AIconImage, AIconWidth, AIconHeight, AIconAnchorX, AIconAnchorY, AIconShadowImage, AIconShadowWidth, AIconShadowHeight, AInfoWindowAnchorX, AInfoWindowAnchorY) {
		if ((this.map) && (this.geocoder)) {
			icon = new GIcon();
			if (icon) {
				icon.image      = AIconImage;
				icon.iconSize   = new GSize(AIconWidth, AIconHeight);
				icon.iconAnchor = new GPoint(AIconAnchorX, AIconAnchorY);
				icon.shadow     = AIconShadowImage;
				icon.shadowSize = new GSize(AIconShadowWidth, AIconShadowHeight);
				icon.infoWindowAnchor = new GPoint(AInfoWindowAnchorX, AInfoWindowAnchorY);
				
				
				this.geocoder.getLatLng ( 
					AAdres,
					function(point) {
						if (!point) {
							alert(msg_AdresNietGevonden);
						} else {
							self.map.setCenter(point, AZoomlevel);
							var marker = new GMarker(point, icon);
							self.map.addOverlay(marker);
							marker.openInfoWindowHtml(AMarkerHtml);
						}
					}
				);			
			} else {
			// Dan maar gewoon :)
				this.AddMarker(AGPSPosition, AMarkerHtml, AZoomlevel);			
			}
		}
	}
	*/

	/*	
	// SetCenter
	this.SetCenter=function(AGPSPosition, AZoomlevel) {
		if ((this.map) && (AGPSPosition) && (AZoomlevel > 0) && (AZoomlevel < 15)) {
			this.map.setCenter(AGPSPosition, AZoomlevel);
		}
	}
	*/
	
	// NIET AANROEPEN, Dit is "On Create" 
	this.OnCreate=function() {
		if (GBrowserIsCompatible()) {
			this.map      = new GMap2(document.getElementById(this.KaartElementID));
			this.geocoder = new GClientGeocoder();
			this.gdir     = new GDirections(this.map, document.getElementById(this.RoutebeschrijvingElementID));	
			
			// 20090302lm : Zoom etc.
			this.map.addControl(new GMapTypeControl(1));
			this.map.addControl(new GLargeMapControl());
			this.map.addControl(new GScaleControl(256));
			new GKeyboardHandler(this.map);
			this.map.enableContinuousZoom();
			this.map.enableDoubleClickZoom(); 
			this.map.enableScrollWheelZoom();
			
			
//			// Eventhandlers koppelen...
			GEvent.addListener(self.gdir, "load", self.onGDirectionsLoad);
        	GEvent.addListener(self.gdir, "error", self.handleErrors);					
		}
	}

	// RouteInformatie tonen...	
	this.setDirections=function(AFromAddress, ALocale) {
	  gdir = new GDirections(this.map, document.getElementById(this.RoutebeschrijvingElementID));
	  if (gdir) {
			gdir.load("from: " + AFromAddress + " to: " + this.LocatieAdres, { "locale": ALocale });			
			this.checkAddress(AFromAddress);
	  }
	}

	this.checkAddress=function(AAdres) {
		if (this.geocoder) {
			this.geocoder.getLatLng (
				AAdres,
				function(point) {
					if (!point) {
						alert (AAdres + " niet gevonden, probeer iets anders :)");
						return false;
					} else {	
						return true;
					}
				}
			);
		}
	}


    this.handleErrors=function(){
       if (gdir.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: " + gdir.getStatus().code);
       else if (gdir.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: " + gdir.getStatus().code);
       else if (gdir.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: " + gdir.getStatus().code);
    //   else if (gdir.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: " + gdir.getStatus().code);
       else if (gdir.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: " + gdir.getStatus().code);
       else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
         alert("A directions request could not be successfully parsed.n Error code: " + gdir.getStatus().code);
       else alert("An unknown error occurred.");
    }                        
	
	
	this.drawmap=function(AMarkerHTML) {
		// alert(this.GPSPositionFromAdress(this.Adres).x);
		this.AddMarker(this.LocatieAdres, AMarkerHTML, this.ZoomLevel);
	}
	
	
	// De "OnCreate" afvuren.
	this.OnCreate();

}	
