﻿var isWorld = true;
var toggleCountryWorld = function() {
    if (isWorld) {
        $("#toggleCW").html("Switch to World");
        toMyCountry();
    }
    else {
        $("#toggleCW").html("Switch to My Country");
        toWorld();
    }
    isWorld = !isWorld;
}
$(document).ready(
        function() {
            $("#toggleCW").click(toggleCountryWorld);
        }
    );

function toMyCountry(adr) {
    $("#workoutsFrom").html("<b>My Country</b>");
    var geocoder = new GClientGeocoder();
    var address = document.getElementById("myCountry").value;
    if (adr) { address = adr; }
    geocoder.setBaseCountryCode(address);
    geocoder.getLocations(
            address,
            function(results) {
                if (results && results.Status.code == 200) {
                    var box = results.Placemark[0].ExtendedData.LatLonBox;
                    var bounds = new GLatLngBounds(
                        new GLatLng(box.south, box.west),
                        new GLatLng(box.north, box.east)
                    );
                    map.setCenter(bounds.getCenter());
                    map.setZoom(map.getBoundsZoomLevel(bounds));
                    fetchWorkouts(box.south, box.west, box.north, box.east);
                }
            }
        );

    $.get("/Community/TopList", { code: address },
            function(data) {
                $("#top-users").html(data);
            }
        );
}
function toWorld() {
    $("#workoutsFrom").html("<b>World</b>");
    $.get("/Community/TopList", null,
            function(data) {
                $("#top-users").html(data);
            }
        );
    map.setCenter(new GLatLng(20, 0));
    map.setZoom(1);
    jQuery.getJSON('/Json/GetWorkoutsByWorld', {}, workoutsLoaded);
}