﻿var SBMapSearch = {

    map: null,
    marker: null,

    // default latitude, longitude (to show the initial map)
    lat: 52.50,
    lng: 5.8,

    zoom: 8,
    bounds: null,


    // name of the map-container (div)
    mapName: "mapsearch",

    clustermarkers: new Object(),

    initialize: function(isWorldView) {
        this.map = new GMap2($(this.mapName));
        this.map.setCenter(new GLatLng(this.lat, this.lng), this.zoom);
        this.map.setUIToDefault();
        this.mgr = new MarkerManager(this.map);

        if (isWorldView) {
            GEvent.addListener(this.map, "moveend", this.onMapMove.bind(this));
            this.onMapMove();
        }
        else {
            this.onMapStart();
        }

    },

    onMapStart: function() {

        var ccd = getParam('ccd');
        var pid = getParam('pid');
        var rid = getParam('rid');
        var cid = getParam('cid');

        if (ccd == undefined) { ccd = -1; }
        if (pid == undefined) { pid = -1; }
        if (rid == undefined) { rid = -1; }
        if (cid == undefined) { cid = -1; }

        var url = 'map_search.html?_a=getzoom&ccd=' + ccd + '&pid=' + pid + '&rid=' + rid + '&cid=' + cid;

        req = GXmlHttp.create();
        req.open("GET", url, true);
        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                eval(req.responseText);
            }
        };

        req.send(null);

    },

    onMapMove: function() {
        var b = this.map.getBounds();
        var z = this.map.getZoom();
        // find the bottom left tile coordinate
        var tileCoordinate = new GPoint();
        var tilePoint = new GPoint();
        var currentProjection = G_NORMAL_MAP.getProjection();
        G_NORMAL_MAP.getMaximumResolution = function() { return 15 }; 
        tilePoint = currentProjection.fromLatLngToPixel(b.getSouthWest(), z);
        var minX = Math.floor(tilePoint.x / 256);
        var maxY = Math.floor(tilePoint.y / 256);

        tilePoint = currentProjection.fromLatLngToPixel(b.getNorthEast(), z);
        var maxX = Math.floor(tilePoint.x / 256);
        var minY = Math.floor(tilePoint.y / 256);

        var ccd = getParam('ccd');
        var pid = getParam('pid');
        var rid = getParam('rid');
        var cid = getParam('cid');

        if (ccd == undefined) { ccd = -1; }
        if (pid == undefined) { pid = -1; }
        if (rid == undefined) { rid = -1; }
        if (cid == undefined) { cid = -1; }

        var url = 'map_search.html?_a=search&zoom=' + z + '&minx=' + minX + '&maxx=' + maxX + '&miny=' + minY + '&maxy=' + maxY + '&ccd=' + ccd + '&pid=' + pid + '&rid=' + rid + '&cid=' + cid;

        req = GXmlHttp.create();
        req.open("GET", url, true);
        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                eval(req.responseText);
            }
        };
        req.send(null);

    },

    onSchoolClick: function(schoolId) {
        window.location.href = 'http://www.schoolbank.nl/school/overzicht/' + schoolId;
    },

    showClusterWindow: function(quadkey) {
        var tm = this.clustermarkers[quadkey];
        if (!tm) {
            alert('cannot find cluster marker ' + quadkey);
            return;
        }

        if (tm.numberOfItems > 10) {
            // too many results: center and zoom in
            this.map.zoomIn(tm.point, true, true);
        }
        else {

            var ccd = getParam('ccd');
            var pid = getParam('pid');
            var rid = getParam('rid');
            var cid = getParam('cid');

            if (ccd == undefined) { ccd = -1; }
            if (pid == undefined) { pid = -1; }
            if (rid == undefined) { rid = -1; }
            if (cid == undefined) { cid = -1; }

            quadkey = '' + quadkey;
            var req = GXmlHttp.create();
            req.open("GET", "/map_search.html?_a=clusterinfo&key=" + quadkey.substring(1, 30) + '&ccd=' + ccd + '&pid=' + pid + '&rid=' + rid + '&cid=' + cid);
            req.onreadystatechange = function() {
                if (req.readyState == 4) {
                    eval(req.responseText);
                }
            };
            req.send(null);
        }
    },

    IncludeInZoom: function(quadkey, count, tx, ty, dx, dy, schoolId, schoolName) {
        ;
        var z = 10;
        var tilePoint = new GPoint();
        tilePoint.x = (256 * tx) + dx;
        tilePoint.y = (256 * ty) + dy;
        var currentProjection = G_NORMAL_MAP.getProjection();
        G_NORMAL_MAP.getMaximumResolution = function() { return 15 }; 
        var tileCoordinate = currentProjection.fromPixelToLatLng(tilePoint, z);
        if (this.bounds == null) {
            this.bounds = new GLatLngBounds(tileCoordinate);
        }
        else {
            this.bounds.extend(tileCoordinate);
        }


    },

    SetZoom: function() {
        var z = this.map.getBoundsZoomLevel(this.bounds);
        GEvent.addListener(this.map, "moveend", this.onMapMove.bind(this));
        this.map.setCenter(this.bounds.getCenter(), z);
    },

    AddClusterMarker: function(quadkey, count, tx, ty, dx, dy, schoolId, schoolName) {
        quadkey = '7' + quadkey;
        if (!this.clustermarkers[quadkey]) {
            var z = this.map.getZoom();
            var tilePoint = new GPoint();
            tilePoint.x = (256 * tx) + dx;
            tilePoint.y = (256 * ty) + dy;
            var currentProjection = G_NORMAL_MAP.getProjection();
            var tileCoordinate = currentProjection.fromPixelToLatLng(tilePoint, z);
            var numberedIcon = new GIcon(G_DEFAULT_ICON);

            var iconUrl = '';
            var iconType = 'school';
            var iconTitle = schoolName;
            var markerOptions = { url: iconUrl, type: iconType, title: iconTitle, width: 32, height: 32 };
            if (count == 1) {
                markerOptions.js = 'SBMapSearch.onSchoolClick(' + schoolId + ')';
            }
            if (count > 1) {
                markerOptions.js = 'SBMapSearch.showClusterWindow(' + quadkey + ')';
            }


            this.clustermarkers[quadkey] = new ThumbMarker(tileCoordinate, markerOptions, count, quadkey);


            this.mgr.addMarker(this.clustermarkers[quadkey], z, z);


        }

    }


};

var SBMap = {

    map: null,
    staticMarker: null,
    draggableMarker: null,

    initialize: function(lng, lat) {
        this.map = new GMap2(document.getElementById("map_canvas"));

        this.map.addControl(new GLargeMapControl());

        if (lng == 500) {
            lat = 52.50;
            lng = 5.5;
        }
        
        this.map.setCenter(new GLatLng(lat, lng), 16);
        var point = new GLatLng(lat, lng);

        this.staticMarker = new GMarker(point);
        this.map.addOverlay(this.staticMarker);

    },


    editMarker: function(lng, lat, sid, aid, jax) {

        if (lng == 500) {
            lat = 52.50;
            lng = 5.5;
        }

        this.map.removeOverlay(this.staticMarker);

        var center = new GLatLng(lat, lng);
        this.map.setCenter(center, this.map.getZoom());

        this.draggableMarker = new GMarker(center, { draggable: true });

        GEvent.addListener(this.draggableMarker, "dragstart", function() {
            //this.map.closeInfoWindow();
        });

        GEvent.addListener(this.draggableMarker, "dragend", function() {
            var point = SBMap.draggableMarker.getPoint();

            var newLng = String(point.lng() * 1000000)
            var newLat = String(point.lat() * 1000000)

            newLng = newLng.substr(0, newLng.indexOf('.'))
            newLat = newLat.substr(0, newLat.indexOf('.'))

            if (jax == 0) {
            }
            else {
            }


        });

        this.map.addOverlay(this.draggableMarker);
        return false;
    }

};


var SBStreetViewMap = {

    map: null,
    lat: 52.74,
    lng: 5.8,

    initialize: function(lat, lng) {

        var fenwayPark = new GLatLng(lat, lng);
        panoramaOptions = { latlng: fenwayPark };
        var myPano = new GStreetviewPanorama(document.getElementById("streetmap_canvas"), panoramaOptions);
        GEvent.addListener(myPano, "error", SBStreetViewMap.handleNoFlash);

    },

    handleNoFlash: function(errorCode) {

        if (errorCode == 600) 
        {
            $('streetmap_canvas').innerHTML = '<div><img src=\'/include/images/notavaibable.gif\' style=\'width:320px;height:280px;\'/></div>'
        }
        if (errorCode == 603) 
        {
            $('streetmap_canvas').innerHTML = '<div>Je browser ondersteunt geen Flash</div>';
        }
        
        Element.show('streetmap_canvas');
        
        return;
    }
};

var SBPersonMapLB = {

    map: null,
    staticMarker: null,
    draggableMarker: null,
    geocoder: null,
    country: null,
    

    initialize: function(lng, lat) {
        this.map = new GMap2(document.getElementById("personmap_lb_canvas"));

        this.map.addControl(new GLargeMapControl());
        
        geocoder = new GClientGeocoder();

        if (lng == 500) {
            lat = 52.50;
            lng = 5.5;
        }
        
        var point = new GLatLng(lat, lng);
        this.map.setCenter(point, 16);

        this.staticMarker = new GMarker(point);
        this.map.addOverlay(this.staticMarker);
        
    },
    
    getCountry: function(latlng) {
      if (latlng != null) {
        geocoder.getLocations(latlng, this.returnCountry);
      }
    },
    
    returnCountry: function(response) {
      if (!response || response.Status.code != 200) {
        alert("Status Code:" + response.Status.code);
      } else {
        place = response.Placemark[0];      
        alert(place.AddressDetails.Country.CountryNameCode);
      }
    },

    editMarker: function(lng, lat, uid, jax) {

        if (lng == 500) {
            lat = 52.50;
            lng = 5.5;
        }

        this.map.removeOverlay(this.staticMarker);

        var center = new GLatLng(lat, lng);
        this.map.setCenter(center, this.map.getZoom());

        this.draggableMarker = new GMarker(center, { draggable: true });

        GEvent.addListener(this.draggableMarker, "dragstart", function() {
            //this.map.closeInfoWindow();
        });

        GEvent.addListener(this.draggableMarker, "dragend", function() {
            var point = SBPersonMapLB.draggableMarker.getPoint();

            var newLng = String(point.lng() * 1000000)
            var newLat = String(point.lat() * 1000000)

            newLng = newLng.substr(0, newLng.indexOf('.'))
            newLat = newLat.substr(0, newLat.indexOf('.'))

            if (jax == 0) {
            }
            else {
            }

        });

        this.map.addOverlay(this.draggableMarker);
        return false;
    }
   
};

var SBPersonMap = {

    pmap: null,
    marker: null,
    staticMarker: new Object(),

    // default latitude, longitude (to show the initial map)
    lat: 52.50,
    lng: 5.8,

    zoom: 8,
    bounds: null,
    sid: null,

    // name of the map-container (div)
    mapName: "person_map_canvas",
    

    clustermarkers: new Object(),

    initialize: function(slng,slat,sid) {
        this.sid = sid;
        this.pmap = new GMap2($(this.mapName));
        this.pmap.setCenter(new GLatLng(this.lat, this.lng), this.zoom);
        this.pmap.addControl(new GLargeMapControl());
        this.pmap.enableScrollWheelZoom();	
       
        this.pmgr = new MarkerManager(this.pmap);
       
        this.addSchoolMarker(slng,slat);
        
        this.onMapStart(); 
        
        //click listeners 
        GEvent.addListener(this.pmap,'dblclick',function(){ SBPersonMap.pmap.disableInfoWindow(); });
        GEvent.addListener(this.pmap,'click',function(){ SBPersonMap.pmap.enableInfoWindow(); });
    }, 
    
    addSchoolMarker: function(lng, lat) {
        var point = new GLatLng(lat, lng);      
        var iconUrl = '';
        var iconType = 'school';
        var iconTitle = schoolname;       
        var markerOptions = { url: iconUrl, type: iconType, title: iconTitle, width: 32, height: 32};
        markerOptions.js = 'SBPersonMap.showSchoolWindow()';
        staticMarker = new ThumbSchoolMarker(point, markerOptions);
        this.pmap.addOverlay(staticMarker);
    },
    
    onMapStart: function() {
        if (this.sid == undefined) { this.sid = -1; }
        var url = '/map_search.html?_a=getzoomperson&sid=' + this.sid;

        req = GXmlHttp.create();
        req.open("GET", url, true);
        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                eval(req.responseText);
            }
        };

        req.send(null);
    },

    onMapMove: function() {
        var b = this.pmap.getBounds();
        var z = this.pmap.getZoom();
        // find the bottom left tile coordinate
        var tileCoordinate = new GPoint();
        var tilePoint = new GPoint();
        var currentProjection = G_NORMAL_MAP.getProjection();
        G_NORMAL_MAP.getMaximumResolution = function() { return 15 }; 
        tilePoint = currentProjection.fromLatLngToPixel(b.getSouthWest(), z);
        var minX = Math.floor(tilePoint.x / 256);
        var maxY = Math.floor(tilePoint.y / 256);

        tilePoint = currentProjection.fromLatLngToPixel(b.getNorthEast(), z);
        var maxX = Math.floor(tilePoint.x / 256);
        var minY = Math.floor(tilePoint.y / 256);      

        if (this.sid == undefined) { this.sid = -1; }
        var randomnumber = Math.floor(Math.random()*111);
        var url = '/map_search.html?_a=searchperson&zoom=' + z + '&minx=' + minX + '&maxx=' + maxX + '&miny=' + minY + '&maxy=' + maxY + '&sid=' + this.sid  + '&ran=' + randomnumber;

        req = GXmlHttp.create();
        req.open("GET", url, true);
        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                eval(req.responseText);
            }
        };
        req.send(null);
    },

    onPersonClick: function(PersonUrl) {
        window.location.href = PersonUrl;
    },
    
    showPersonWindow: function(quadkey, PersonId) {        
        quadkey = '' + quadkey;
        var randomnumber = Math.floor(Math.random()*111);
        var req = GXmlHttp.create();
        req.open("GET", "/map_search.html?_a=personinfo&key=" + quadkey.substring(1, 30) + '&pid=' + PersonId + '&ran=' + randomnumber);
        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                eval(req.responseText);
            }
        };
        req.send(null);
    },

    showClusterWindow: function(quadkey) {
        var tm = this.clustermarkers[quadkey];
        if (!tm) {
            alert('cannot find cluster marker ' + quadkey);
            return;
        }

        if (tm.numberOfItems > 10) {
            // too many results: center and zoom in
            this.pmap.zoomIn(tm.point, true, true);
        }
        else {

            if (this.sid == undefined) { this.sid = -1; }

            quadkey = '' + quadkey;
            var randomnumber = Math.floor(Math.random()*111);
            var req = GXmlHttp.create();
            req.open("GET", "/map_search.html?_a=personclusterinfo&key=" + quadkey.substring(1, 30) + '&sid=' + this.sid + '&ran=' + randomnumber);
            req.onreadystatechange = function() {
                if (req.readyState == 4) {
                    eval(req.responseText);
                }
            };
            req.send(null);
        }
    },
    
    showSchoolWindow: function() {
       staticMarker.openInfoWindowHtml('<b>' + schoolname + '</b><br/>' + schoolstreet + '<br/>'  + schoolpostcode + schoolcity + '<br/>' + '<br/>');
    },


    IncludeInZoom: function(tx, ty, dx, dy) {
        ;
        var z = 10;
        var tilePoint = new GPoint();
        tilePoint.x = (256 * tx) + dx;
        tilePoint.y = (256 * ty) + dy;
        var currentProjection = G_NORMAL_MAP.getProjection();
        G_NORMAL_MAP.getMaximumResolution = function() { return 15 }; 
        var tileCoordinate = currentProjection.fromPixelToLatLng(tilePoint, z);
        if (this.bounds == null) {
            this.bounds = new GLatLngBounds(tileCoordinate);
        }
        else {
            this.bounds.extend(tileCoordinate);
        }


    },

    SetZoom: function() {
        var z = this.pmap.getBoundsZoomLevel(this.bounds);
        GEvent.addListener(this.pmap, "moveend", this.onMapMove.bind(this));
        this.pmap.setCenter(this.bounds.getCenter(), z);
    },

    AddClusterMarker: function(quadkey, count, tx, ty, dx, dy, PersonId, PersonName, PersonUrl, Gender, plusaccess) {
        quadkey = '7' + quadkey;
        if (!this.clustermarkers[quadkey]) {
            var z = this.pmap.getZoom();
            var tilePoint = new GPoint();
            tilePoint.x = (256 * tx) + dx;
            tilePoint.y = (256 * ty) + dy;
            var currentProjection = G_NORMAL_MAP.getProjection();
            var tileCoordinate = currentProjection.fromPixelToLatLng(tilePoint, z);
            var numberedIcon = new GIcon(G_DEFAULT_ICON);
            
            var you = '';
            if(PersonId == currentPersonId)
            {
              you = '_you';
            }

            var iconUrl = '';
            var iconType = 'person';
            var iconTitle = PersonName;
            var iconGender = Gender;
            var markerOptions = { url: iconUrl, type: iconType, title: iconTitle, width: 32, height: 32, gender: iconGender, you: you, plus: plusaccess };
            if (count == 1) {
                markerOptions.js = 'SBPersonMap.showPersonWindow(' + quadkey + ',' + PersonId + ')';
            }
            if (count > 1) {
                markerOptions.js = 'SBPersonMap.showClusterWindow(' + quadkey + ')';
            }

            this.clustermarkers[quadkey] = new ThumbMarker(tileCoordinate, markerOptions, count, quadkey);

            this.pmgr.addMarker(this.clustermarkers[quadkey], z, z);


        }

    }


};



