// Google Map with bicycle routes
document.observe('dom:loaded', function() {new RouteMap();});

var RouteMap = Class.create({
    routemap: 'routemap',
    defaultLat: 52.1,
    defaultLng: 5.0,
    defaultZoom: 7,

    initialize: function(el)
    {
        // Google map
        if ($(this.routemap)) {
            this.loadMap();
            // watch route selection
            if ($('route'))
                $('route').observe('change', this.changeRoute.bindAsEventListener(this));
        }
    },

    // load Google map
    loadMap: function()
    {
        if (GBrowserIsCompatible()) {
            document.observe('unload', GUnload, false);
            // display map
            this.map = new GMap2($(this.routemap));
            this.map.addControl(new GLargeMapControl());
            this.map.addControl(new GHierarchicalMapTypeControl()); // GMapTypeControl
            this.map.removeMapType(G_SATELLITE_MAP);
            this.map.removeMapType(G_HYBRID_MAP);
            this.map.addMapType(G_PHYSICAL_MAP);
            G_PHYSICAL_MAP.getMinimumResolution = function () { return 7 };
            G_NORMAL_MAP.getMinimumResolution = function () { return 7 };
            this.map.addControl(new GScaleControl());
            this.map.enableScrollWheelZoom();
            this.map.setMapType(G_PHYSICAL_MAP);
            this.map.setCenter(new GLatLng(this.defaultLat, this.defaultLng), this.defaultZoom);
            // create icons
            this.veeIcon = new GIcon();
            this.veeIcon.image = "/skin/vee-point-high.png";
            this.veeIcon.iconSize = new GSize(22, 32);
            this.veeIcon.iconAnchor = new GPoint(3, 31);
            this.veeIcon.infoWindowAnchor = new GPoint(3, 31);
            this.noveeIcon = new GIcon();
            this.noveeIcon.image = "/skin/no_vee-point-high.png";
            this.noveeIcon.iconSize = new GSize(15, 31);
            this.noveeIcon.iconAnchor = new GPoint(3, 30);
            this.noveeIcon.infoWindowAnchor = new GPoint(3, 30);
            this.opendagIcon = new GIcon();
            this.opendagIcon.image = "/skin/opendag.png";
            this.opendagIcon.iconSize = new GSize(43, 40);
            this.opendagIcon.iconAnchor = new GPoint(16, 39);
            this.opendagIcon.infoWindowAnchor = new GPoint(15, 39);
        }
    },

    // update system after changing route
    changeRoute: function(e)
    {
        var elm = Event.element(e);
        var rid = $F(elm);
        var bounds = this.map.getBounds();
        if (rid) { // fetch route data
            new Ajax.Updater('description', '/routes/index.php', 
            {
                parameters: {'routedata': 1, 'rid': rid},
                onComplete: this.updateMap.bind(this)
            });
        }
        else { // clear route data, reset map
            $('description').update();
            this.map.clearOverlays();
            this.map.setCenter(new GLatLng(this.defaultLat, this.defaultLng), this.defaultZoom);
        }
        Event.stop(e);
    },

    // add KML and POI with info windows to the map
    updateMap: function()
    {
        this.map.clearOverlays();
        var kmlFile = $('routekml').getAttribute('name');
        if (kmlFile) {
            var kmlData = new GGeoXml(kmlFile + '?' + (new Date()).valueOf());
            this.map.addOverlay(kmlData);
            kmlData.gotoDefaultViewport(this.map);
        }
        // get point data
        var json = eval($('locationdata').points.value);
        var markers = new Array();
        $A(json.points).each(function(p)
        {
            if (p && p.lat > 0 && p.lon > 0) {
                if (p.opendag > 0)
                    var m = new GMarker(new GLatLng(p.lat, p.lon), {zIndexProcess: function(marker,b) {return -GOverlay.getZIndex(marker.getPoint().lat())}, icon: this.opendagIcon});
                else if (p.veelid > 0)
                    var m = new GMarker(new GLatLng(p.lat, p.lon), {zIndexProcess: function(marker,b) {return -GOverlay.getZIndex(marker.getPoint().lat())}, icon: this.veeIcon});
                else
                    var m = new GMarker(new GLatLng(p.lat, p.lon), {zIndexProcess: function(marker,b) {return -GOverlay.getZIndex(marker.getPoint().lat())}, icon: this.noveeIcon});
                this.map.addOverlay(m);
                m.bindInfoWindow($('info_' + p.lid));
            }
        }, this);
    }

});
