/**************************************************************************************

			var gmap = new GMaps();
            gmap.mapType = G_NORMAL_MAP;
            gmap.zoomlevel = 12;
            gmap.GMapLoad(map: HTML Object, directions: HTML Object);
		KML	
			gmap.kmlPath = '/pad/naar/kml/bestanden/?optioneel';
			gmap.showKML('kml.xml');
		COORDS
			gmap.showCoords(latitude:decimal, longtitude:decimal);
		ADDRESS
			gmap.showAddress('Land, Adres, Postcode, Plaats');
			
			
***************************************************************************************/

function GMaps(){	
  var that = null;//Referentie naar de class

  this.geocoder = null;//GClientGeocoder Object: Voor het ophalen van Coordinaten of Adres gegevens
  this.map = null;//GMap2 Object: Voor het aanmaken van een mao
  this.mapType = null;
  this.zoomlevel = null;
  this.kml = null;//FileName Kml bestand
  this.kmlPath = null;//Pad naar Kml bestand: Optioneel
  this.gdir = null;//GDirections Object
  this.geoXml = null;//GGeoXml Object: Voor het uitlezen van het KML bestand
  this.latitude = null;
  this.longtitude = null;
  this.address = null;
  this.point = null;//GLatLng Object: bepaald punt op de kaart
  
  this.GMapLoad = function(objMap, objDirections){
	if (GBrowserIsCompatible()) {
		//Om de referentie van de class mee te geven aan een calback functie moeten wij hem globaal maken.
		that = this;
		
		//Centrale Class voor de Api om een map aan te maken.
		that.map = new GMap2(objMap);
	   
		that.map.setMapType(that.mapType);
		
		that.map.addControl(new GSmallMapControl());
		that.map.addControl(new GMapTypeControl());
		
		//GClientGeocoder word gebruikt als communicatie middel met de google server om geocodes voor gespecificeerde adressen op te halen.
		that.geocoder = new GClientGeocoder();			
		//GDirections word gebruikt om route beschrijvingen op te halen en ze in een HML object weer te geven.
		if(objDirections)that.gdir = new GDirections(that.map, objDirections);
	}
  }; 

  this.showKML = function(kml){
	  that.kml = kml;
	  
	  if(!that.kmlPath)that.kmlPath = "/inc/upload/kml/";
	  
	  //Een GGeoXml object voeg geografische content toe van een XML KML bestand.		
	  if(that.kml)	that.geoXml = new GGeoXml("http://"+document.location.hostname+that.kmlPath+that.kml+"?uuid="+Math.random(),setLocation);
  }
    
  //Callback functie voor showKML GGeoXml
  setLocation = function(){
	  //De default viewport van de map verplaatsen naar de coordinaten van het kml bestand.
	  that.geoXml.gotoDefaultViewport(that.map);
	  //Markers, paths, etc over de map heen plaatsen vanuit het KML bestand.
	  that.map.addOverlay(that.geoXml);  
  };
  
  this.showCoords = function(latitude,longtitude){
	that.point = new GLatLng(latitude,longtitude);
	
	that.geocoder.getLocations(that.point, coordsCallback);  	
  };

  //Callback functie voor showCoords geocoder.getLocations
  coordsCallback = function(address){
	that.address = 	address['Placemark'][0]["address"];  
	  
	that.map.setCenter(that.point, that.zoomlevel);
	
	var marker = new GMarker(that.point);
	that.map.addOverlay(marker);
	marker.openInfoWindowHtml(that.address);
  };
    
  //Addres om toveren in coordinaten
  this.showAddress = function(address) {  
	if (address) {
		that.address = address;
		
		that.geocoder.getLatLng(address,geoCallback);
	}	
  };
  
  //Callback voor showAddress geocoder.getLatLng
  geoCallback = function(point){
	//point coordinaten die zijn opgehaald
	if (!point) {
	  alert(that.address + "  niet gevonden.");
	} else {
	  //map centreren op coordinaten met gewenste zoomlevel	
	  that.map.setCenter(point, that.zoomlevel);
	  
	  var marker = new GMarker(point);
	  
	  //markers, paths, etc over de map heen plaatsen
	  that.map.addOverlay(marker);
	  
	  //Informatie venster van de marker openen en adresgegevens in plaatsen
	  marker.openInfoWindowHtml('<strong>Voges Verpakking BV</strong><br />Einsteinstraat 11<br />2181 AA Hillegom, Netherlands');   //that.address
	}
  };
  
  //Routeplanner
  this.setDirections = function(fromAddress, toAddress, locale) {
	if(that.gdir){
		if(!toAddress&&that.address)toAddress = that.address;
		
		//locale: Land code voor specefieke routes.
		if(fromAddress.length>1 && fromAddress.length>1){	
		  //Routes in het HTML Object laden.
		  that.gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale, "getPolyline":true })
		  
		  try{
			  //Knop voor het volgen van de route weergeven na load.
			  if(that.followButton!==null)that.followButton.style.display = "inline";
		  }catch(e){
			  //do nothing
		  }
		}else{
		  alert("Voer een adres in en probeer het nogmaals.");
		}
	}else{
		alert('Kan geen route bepalen omdat het HTML object voor de Directions niet is gezet.');
	}
  };
  
}