﻿var map = null;
var layer = null;
var clusterIcon = '';
var eventIcon = '';

function GetMap(url, eventIconPath, clusterIconPath) {
    eventIcon = eventIconPath;
    clusterIcon = clusterIconPath;
    map = new VEMap('deMap');
    map.ShowMessageBox = false;
    map.LoadMap();
    map.SetInfoBoxStyles();
    layer = new VEShapeLayer();

    var veLayerSpec = new VEShapeSourceSpecification(VEDataType.GeoRSS, url, layer);
    map.ImportShapeLayerData(veLayerSpec, onFeedLoad);

    var clusterOptions = new VEClusteringOptions();
    clusterOptions.Callback = onClusteringCallback;
    layer.SetClusteringConfiguration(VEClusteringType.Grid, clusterOptions);

    map.ShowAllShapeLayers();
}

function onFeedLoad(layer) {
    var numShapes = layer.GetShapeCount();
    for (var i = 0; i < numShapes; ++i) {
        var s = layer.GetShapeByIndex(i);
        s.SetCustomIcon(eventIcon);
    }
    map.ShowMessageBox = true;
}

function onClusteringCallback(clusters) {
    try {
        if (clusters != null) {
            for (var i = 0; i < clusters.length; i++) {
                var cluster = clusters[i];
                var clusterShape = clusters[i].GetClusterShape();
                var description = "";
                for (var s = 0; s < cluster.Shapes.length; s++) {
                    var shape = cluster.Shapes[s];
                    description += "<br/><a href='javascript:ShowPin(\"" + shape.GetID() + "\")'>" + shape.Title + "</a>";
                }
                clusterShape.SetTitle(cluster.Shapes.length + " events near here");
                clusterShape.SetDescription(description);
                clusterShape.SetCustomIcon(clusterIcon);
            }
        }
    } catch (e) {
        alert(e.message);
    }
}

function ShowPin(pinID) {
    var pin = map.GetShapeByID(pinID);
    if (pin != null)
        map.ShowInfoBox(pin);
} 
