/**
 * cSimpleMap - Versimpelde Goegol Maps
 *
 * @author Jeroen van der Geer jeroen@swis.nl
 * @version 0.001b ;-)
 *
**/

function cSimpleMap(oMap){

    /**
    *   @desc Initialisatie van de class.
    *   @param oMap Id van de map div
    **/
    this.initialize = function(oMap){
        this.map = new GMap2(document.getElementById(oMap));
        this.geocoder = new GClientGeocoder();
        this._powered_by_geostart = function(oMap){ this.oMap = oMap; };
        this._infoWindowWidth = 0;
    }

    /**
    *   @desc Zet het map type.
    *   @param oMapType (G_NORMAL_MAP, G_NORMAL_MAP, G_HYBRID_MAP, G_PHYSICAL_MAP)
    **/
    this.setMapType = function(oMapType){
        if(G_DEFAULT_MAP_TYPES.inArray(oMapType) || oMapType == G_PHYSICAL_MAP){
            this.map.setMapType(oMapType);
        }
        else{
            alert("Onbekend map type: '"+oMapType+"'");
        }
    }

    /**
    *   @desc de 'private' function die daadwerkelijk de kaart centreerd op basis van een GLatLng.
    *   @param point GLatLng van de locatie
    *   @param iZoom Zoomniveau
    **/
    this._setCenterMap = function(point, iZoom){
        this.map.setCenter(point, iZoom);
    };

    /**
    *   @desc de 'public' function die lat + long omzet in een GLatLng en daarna de map hier op laat centreren.
    *   @param aLatLng Array met Lat en Long
    *   @param iZoom Zoomniveau
    **/
    this.setCenter = function(sLat, sLong, iZoom){
        var point = new GLatLng(sLat, sLong);
        this._setCenterMap(point, iZoom);
    };


    /**
    *   @desc de 'public' function die een adres omzet in een GLatLng en daarna de map hier op laat centreren.
    *   @param sAddress string met adres
    *   @param iZoom Zoomniveau
    **/
    this.setCenterByAddress = function(sAddress, iZoom){
        var self = this;
            this.geocoder.getLatLng(
                sAddress,
                function(point) {
                  if (!point) {
                    alert(sAddress + " kon niet worden gevonden");
                  } else {
                        self._setCenterMap(point, iZoom);
                  }
                }
              );
      };

    /**
    *   @desc voeg een control toe aan de kaart
    *   @param oControl het control wat toegevoegd moet worden aan de kaart. (Bijv. GSmallMapControl)
    **/
    this.addControl = function(oControl){
        this.map.addControl(new oControl());
    };

    /**
    *   @desc voeg het PoweredByGeostart logo toe aan de kaart
    **/
    this.addPoweredByGeostart = function(){

        this._powered_by_geostart.prototype = new GControl(true, false);
        this._powered_by_geostart.prototype.initialize = function () {
            var oContainer = document.createElement("div");
            oContainer.innerHTML    = "<a target='_blank' href='http://www.geostart.nl' style='display:block;height:29px;width:90px;'><img class='png' style='border:0;' src='"+sBaseUrl+"GeoStart/images/controls/powered_by_geostart.png'></a>";

            oContainer.id           = "powered_by_geostart";
            oContainer.style.cssText= "width:89px;text-decoration:none;border:none;height:29px;cursor:pointer;padding-bottom:2px;_padding-bottom:0px;";

            this.oMap.getContainer().appendChild(oContainer);

            return oContainer;
        }

        this._powered_by_geostart.prototype.getDefaultPosition = function () {
                return new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(67,3));
        };

        this.map.addControl(new this._powered_by_geostart(this.map));

    };


   /**
    *   @desc Bepaal de breedte van het infowindowtje
    *   @param iWidth De breedte van de infowindow in pixels
    **/
   this.setInfoWindowWidth = function(iWidth){
        this._infoWindowWidth = iWidth;
   }

   /**
    *   @desc Start de initialisatie
    **/
   this.initialize(oMap);

}


/**
 * cSimpleMarker - Extensie op cSimpleMap
 *
 * @author Jeroen van der Geer jeroen@swis.nl
 * @version 0.001b ;-)
 *
**/
function cSimpleMarker(oMap){

        /**
        *   @desc Initialisatie van de class.
        *   @param oMap map object waarop het extend
        **/
        this.initialize = function(oMap){
            this.geocoder = new GClientGeocoder();
            this._markerContainer = new Array;

            this.marker = new Object;
            this._markerSize = "";
            this._markerShadowSize = "";
            this._markerImage = "";
            this._markerShadowImage = "";
        }

        /**
        *   @desc Zet de grootte van de marker
        *   @param iWidth breedte in pixels
        *   @param iHeight hoogte in pixels
        **/
        this.setMarkerSize = function(iWidth, iHeight){
            this._markerSize = new GSize(iWidth, iHeight);
        };


        /**
        *   @desc Zet de grootte van de marker schaduw
        *   @param iWidth breedte in pixels
        *   @param iHeight hoogte in pixels
        **/
        this.setMarkerShadowSize = function(iWidth, iHeight){
            this._markerShadowSize = new GSize(iWidth, iHeight);
        };

        /**
        *   @desc Zet het plaatje voor de marker
        *   @param sImage URL van het plaatje (mag relatief)
        **/
        this.setMarkerImage = function(sImage){
            this._markerImage = sImage;
        };

       /**
        *   @desc Marker op de kaart zetten op basis van Lat Long
        *   @param sLat Latitude voor de marker
        *   @param sLong Longitude voor de marker
        *   @param sInfoWindow De tekst voor de InfoWindow (HTML mag!)
        *   @param bOpenOnLoad Of de InfoWindow onload geopent moet worden (true / false). Standaard false.
        **/
        this.addMarker = function(sLat, sLong, sInfoWindow, bOpenOnLoad){
            var point = new GLatLng(sLat,sLong);
            var oMarker = this._addMarkerToMap(point);

            if(typeof(sInfoWindow) != "undefined"){
                oMarker.html = sInfoWindow;
                this._addInfoWindowToMarker(oMarker, bOpenOnLoad);
            }
        };


        /**
        *   @desc Marker op de kaart zetten op basis van een adres
        *   @param oMap map object waarop het extend
        **/
        this.addMarkerByAddress = function(sAddress, sInfoWindow, bOpenOnLoad){
            var self = this;
            this.geocoder.getLatLng(
                    sAddress,
                    function(point) {
                      if (!point) {
                        alert(sAddress + " kon niet worden gevonden");
                      } else {
                            var oMarker = self._addMarkerToMap(point);
                            if(sInfoWindow != ""){
                                oMarker.html = sInfoWindow;
                                self._addInfoWindowToMarker(oMarker, bOpenOnLoad);
                            }
                      }
                    }
                  );
        };

        /**
        *   @desc 'Private' function die de infowindow aan de marker toevoegd
        *   @param oMarker marker object waar de infowindow aan toegevoegd moet worden
        *   @param bOpenOnload Of de infowindow onload geopent moet worden
        **/
        this._addInfoWindowToMarker = function(oMarker, bOpenOnLoad){
                GEvent.addListener(oMarker, "click", function(){
                    oMarker.openInfoWindowHtml(oMarker.html);
                });

                if(bOpenOnLoad === true){
                    oMarker.openInfoWindowHtml(oMarker.html);
                }
        };

        /**
        *   @desc 'Private' function die daadwerkelijk de marker op de kaart zet
        *   @param point de GLatLng waar de marker moet worden geplaatst.
        **/
        this._addMarkerToMap = function(point){
            var icon = G_DEFAULT_ICON;
            if(this._markerImage != ""){ icon.image = this._markerImage; }
            if(this._markerShadowImage != ""){ icon.shadow = this._markerShadowImage; }
            if(this._markerSize != ""){ icon.iconSize = this._markerSize; }
            if(this._markerShadowSize != ""){ icon.shadowSize = this._markerShadowSize; }
            icon.iconAnchor = new GPoint(25, 25);

            var marker = new GMarker(point, {icon:icon});
            oMap.map.addOverlay(marker);
            this._markerContainer.push(marker);

            return marker;
        };

        /**
        *   @desc Verwijder 1 of meer markers van de kaart
        *   @param aMarkers array met marker ID's
        **/
        this.removeMarkers = function(aMarkers){
            if (aMarkers.constructor.toString().indexOf("Array") == -1){
              for(i in this._markerContainer){
                    oMap.map.removeOverlay(this._markerContainer[i]);
              }
            }
        };

        /**
        *   @desc Initialisatie starten
        **/
        this.initialize(oMap);
}

Array.prototype.inArray = function (value) {
    var i;
    for (i=0; i < this.length; i++) {
        if (this[i] === value) {
            return true;
        }
    }
    return false;
};


