﻿
var markerImages = {
    SP: '/Images/Style/Map/sp.png'
}
var markerIcon;
var iconBlue;
var icon;
var map;
var minMax;

function initIcons() {
    icon = new GIcon(G_DEFAULT_ICON);
    icon.image = "http://chart.apis.google.com/chart?cht=mm&chs=24x32&chco=FFFFFF,008CFF,000000&ext=.png";
    opts = {
        "icon": icon,
        "clickable": true,
        "title": "Click to view this workout"
    };
}
$(document).ready(function() {
    initialize();
    minMax = new Object();
    $('#continent').change(function() {
        changeContinent($(this).val());
    });
    initIcons();
    loadWorkoutsByWorld();
});
function initialize() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById('map'));
        map.setCenter(new GLatLng(20, 0), 1);
        //map.setZoom(2);
    }
}

function loadWorkoutsByWorld() {
    jQuery.getJSON('/Json/GetWorkoutsByWorld', {}, workoutsLoaded);
}
var markerCluster = null;
function workoutsLoaded(data) {
    if (markerCluster) markerCluster.clearMarkers();
    map.clearOverlays();
    var markers = [];
    for (var i = 0; i < data.length; i++) {
        if (data[i].CoordinateStart) {
            var point = new GLatLng(data[i].CoordinateStart.Latitude, data[i].CoordinateStart.Longitude);
            var marker = new GMarker(point, { icon: icon, title: 'Click to view workout', clickable: true });
            marker.wid = data[i].Id;
            GEvent.addListener(marker, 'click', function() {
                var link = '/Workouts/Details/' + this.wid;
                window.open(link);
            });

            markers.push(marker);
        }
    }
    markerCluster = new MarkerClusterer(map, markers, { gridSize: 40 });
    if (isWorld) {
        fixedCenterMap();
    }
}

function fixedCenterMap() {
    map.setCenter(new GLatLng(20, 90), 1);
    map.setCenter(new GLatLng(20, 0), 1);
}

function fetchWorkouts(latMin, lngMin, latMax, lngMax) {
    $.getJSON("/Json/GetWorkoutsByRegion",
        { LatMin: latMin, LngMin: lngMin, LatMax: latMax, LngMax: lngMax },
        workoutsLoaded
    );
}
    


    
