﻿var map, cluster, eventListeners = [], markersArray = [], icon;

function initmap(W, H, MarkerText, MarkerDrag, ZOOM) {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById('map'));
        map.setCenter(new GLatLng(0, 0), 0, G_NORMAL_MAP);
        map.addControl(new GSmallMapControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT));
        GEvent.addListener(map, 'zoomend', function() { map.closeInfoWindow(); });

        function myClusterClick(args) {
            map.setCenter(args.clusterMarker.getLatLng(), map.getBoundsZoomLevel(args.clusterMarker.clusterGroupBounds))
        }

        //	create a ClusterMarker
        cluster = new ClusterMarker(map, { clusterMarkerTitle: 'Click to see info about %count locations', clusterMarkerClick: myClusterClick });

        icon = new GIcon();
        icon.iconSize = new GSize(20, 34);
        icon.iconAnchor = new GPoint(10, 30);
        icon.infoWindowAnchor = new GPoint(10, 8);

        initCluster();
    }
}

var _currentMarker=null;
function newMarker(markerLocation, title, markerIcon) {
    var marker = new GMarker(markerLocation, { title: 'School', icon: markerIcon });
    eventListeners.push(GEvent.addListener(marker, 'click', function() {
        var _clusterid = parseInt(title);
        _currentMarker = marker;
        marker.openInfoWindowHtml('<div id=\"School_' + _clusterid + '\"><img src=\"'+'/img/bigrotation2.gif\" alt=\"Loading...\"/></div>');
        Service.SchoolMiniProfile(parseInt(title), RenderClusterProfile);
    }));
    return marker;
}
function RenderClusterProfile(o)    {
    _currentMarker.openInfoWindowHtml(o+"&nbsp;");
}

function toggleClustering() {
    cluster.clusteringEnabled = !cluster.clusteringEnabled;
    cluster.refresh(true);
}

function initCluster() {

    if (MarkerArray == "")
        return;
    var _arr = MarkerArray.split('|');
    var baseIcon = new GIcon();
    baseIcon.shadow = null;
    baseIcon.iconSize = new GSize(19, 23);
    baseIcon.iconAnchor = new GPoint(10, 20);
    baseIcon.infoWindowAnchor = new GPoint(10, 20);

    markersArray = [];
    for (i = eventListeners.length - 1; i >= 0; i--) {
        GEvent.removeListener(eventListeners[i]);
    }
    eventListeners = [];
    var json = [], lat, lng;

    for (var i = 0; i < _arr.length; i++) {
        var t = _arr[i].split(',');
        json[i] = { 'id': t[2], 'lat': parseFloat(t[0]), 'lng': parseFloat(t[1]) };
    }

    var marker, newIcon, j = 1, title, lat, lng;
    for (var i = 0; i < json.length; i++) {
        var newIcon = new GIcon(baseIcon);
        newIcon.image = "/img/t.png";
        lat = Math.round(json[i].lat * 100) / 100;
        lng = Math.round(json[i].lng * 100) / 100;
        title = json[i].id;
        marker = newMarker(new GLatLng(json[i].lat, json[i].lng), title, newIcon);
        markersArray.push(marker);
    }
    cluster.removeMarkers();
    cluster.addMarkers(markersArray);
    cluster.fitMapToMarkers();
    map.savePosition();
}
