/**
 *
 * @Description            These classes are used to create maps / pins with google maps
 *                             (c) Aussie Tours
 * @Author                    Jochen Vandendriessche
 * @Contact                    jochen.vandendriessche@guideline.net
 * @date                        2007-11-30
 *
 */

// the gMap Class

    var GLOBALMAP = null;
    var GLOBALPIN = null;
    var GLOBALMARKER = null;

    function Gmap(div, p){
        if (GBrowserIsCompatible()) {
              this.map = null;
              this.geocoder = new GClientGeocoder();
              this.div = ebi(div);
              this.mapCenter = 'staden belgium';
              this.zoom = 7;
            this.pins = [];
            this.oPins = [];
            this.pp = 0;
            this.oPinsPlaced = [];
            this.pinsPlaced = 0;
            this.poly = [];
            this.oPoly = [];
            this.init = false;
            this.showzoom = true;
            this.type = true;
            this.view = 'default';
              if (p) {
                  if (p.location) {
                      this.mapCenter = p.location;
                  }
                  if (p.zoom) {
                      this.zoom = p.zoom;
                  }
                if (p.pin){
                    this.pin = true;
                }
                if (p.showZoom){
                    this.showzoom = false;
                }
                if (p.showType){
                    this.type = false;
                }
                if (p.view){
                    this.view = p.view;
                }
              }
              this.drawMap();
      }else{
              window.alert('not compatible');
      }

    }

    Gmap.prototype = {

        drawMap : function(){

                this.map = new GMap2(this.div);
                switch (this.view){
                    case 'satellite':
                       this.map.setMapType(G_SATELLITE_MAP);
                       break;
                    case 'hybrid':
                       this.map.setMapType(G_HYBRID_MAP);
                       break;
                }
                if (!GLOBALMAP) {
                    GLOBALMAP = this;
                    this.geocoder.getLatLng(this.mapCenter, this.setCenter);
                }else{
                    window.setTimeout(bind(this, this.drawMap), 1);
                }


        },

        setCenter : function(p){
                var zoom = parseInt(GLOBALMAP.zoom)
                GLOBALMAP.map.setCenter(p, zoom);
                if (GLOBALMAP.showzoom == true){
                    GLOBALMAP.map.addControl(new GSmallMapControl());
                }
                if (GLOBALMAP.type == true){
                    GLOBALMAP.map.addControl(new GMapTypeControl());
                }
                GLOBALMAP.init = true;
                GLOBALMAP.onfinish();
        },

        onfinish : function(){
            // place all pins
            if (init){
                if (this.pp < this.pins.length){
                    var pin = new Gpin(this, this.pins[this.pp], this.pp);
                    this.oPins.push(pin);
                    window.setTimeout(bind(pin, pin.drawPin), 1);
                }
            }else{
                window.setTimeout(bind(this, this.onfinish), 1);
            }
        },

        pinPoint : function(p){
                 this.pins.push(p);
        },

        polyPoint : function(p){
                this.poly.push(p);
        },

        getDirection : function(d, p){
            var ret = false;
            var div = ebi(d);
            if (p.from && p.to && div){
                var dir = new GDirections(this.map, div);
                if (p.onfoot){
                   dir.load("from: " + p.from + " to: " + p.to, {travelMode:G_TRAVEL_MODE_WALKING});
                }else{
                   dir.load("from: " + p.from + " to: " + p.to);
                }
            }
        }

    }

//////////////////////////////////////////////////////////////

    function GPoly(oMap, points){
        if (oMap){
            this.parent = oMap;
            this.polygon = null;
            this.offset = 0.1;
            this.points = points;
            this.drawPoly();
        }
    }

    GPoly.prototype = {

        drawPoly : function(){
            //this.polygon = new GPolygon(this.points);
            this.polygon = new GPolygon([

                                        ])
            window.alert(this.latlang);
        }
        /*
    var polygon = new GPolygon([
            new GLatLng(lat, lon - lonOffset),
            new GLatLng(lat + latOffset, lon),
            new GLatLng(lat, lon + lonOffset),
            new GLatLng(lat - latOffset, lon),
            new GLatLng(lat, lon - lonOffset)
    ], "#f33f00", 5, 1, "#ff0000", 0.2);
    map.addOverlay(polygon);*/
    }

//////////////////////////////////////////////////////////////

    function Gpin(oMap, p, n){
        if (oMap) {
              this.parent = oMap;
            this.balloon = '';
            this.marker = null;
            this.id = n;
            this.line = true;
            //this.img = "/assets/web/gmaps/tour_pointer.png";
              if (p) {
                  if (p.location) {
                      this.location = p.location
                  }
                if (p.balloon){
                    this.balloon = p.balloon
                }
                if (p.line){
                    this.line = 'false';
                }
                if (p.image){
                    //this.img = p.image;
                }
              }

            this.icon = new GIcon();
            this.icon.image = this.img;
            this.icon.iconSize = new GSize(10, 10);
            this.icon.iconAnchor = new GPoint(5, 5);
            this.icon.infoWindowAnchor = new GPoint(5, 5);

       }
    }

/*

                        GEvent.addListener(marker, "click", function() {
                                  marker.openInfoWindowHtml('<b>Adelaide</b><br /><a href="http://www.joggink.be">hupla</a>');

                            });

 */

    Gpin.prototype = {

        drawPin : function(){
            if (!GLOBALPIN){
                GLOBALPIN = this;
                if (this.location){
                    this.parent.geocoder.getLatLng(this.location, this.placePin);
                }
            }else{
                window.setTimeout(bind(this, this.drawPin), 1);
            }
        },

        placePin : function(point){
            GLOBALPIN.parent.pinsPlaced++;
            if (GLOBALPIN.line != 'false'){
                GLOBALPIN.parent.oPoly.push(point);
                var polyOptions = {geodesic:true};
                var polygon = new GPolyline(GLOBALPIN.parent.oPoly, "#f47e32", 2, 1, polyOptions);
                GLOBALPIN.parent.map.addOverlay(polygon);
            }
            GLOBALPIN.marker = new GMarker(point);
            GLOBALPIN.parent.map.addOverlay(GLOBALPIN.marker);
            var bln = GLOBALPIN.balloon;
            var marker = GLOBALPIN.marker;
            if (GLOBALPIN.balloon){
                GEvent.addListener(GLOBALPIN.marker, "click", function(){
                    marker.openInfoWindowHtml(bln);
                })
            }
            GLOBALPIN.parent.oPinsPlaced[GLOBALPIN.id] = true;
            //ebi('gmapdebug').innerHTML += 'placing dag ' + GLOBALPIN.id + '<br />';
            GLOBALPIN.parent.pp++;
            window.setTimeout(bind(GLOBALPIN.parent, GLOBALPIN.parent.onfinish), 1)
            GLOBALPIN = null;
        }
    }


//////////////////////////////////////////////////////////////
    function ebi(e){
        if (typeof e == "object") {
          return e;
      }else if(document.getElementById(e)){
        return document.getElementById(e);
      }else{
          return false;
      }
    }


    function bind(o, f){
    return function() { f.call(o); }
    }


