$(document).ready(function () {
    $('body').attr('onunload', "GUnload();");
});

google.load("maps", "2", { "other_params": "sensor=true" });
google.setOnLoadCallback(getGyms);

var gyms;
var points;
var map;
var latLngBounds

function initalizeMap(Id) {

    map = new GMap2(document.getElementById("map"));
    latLngBounds = new GLatLngBounds();
    points = [];

    var zoom = 13;

    if (Id != undefined && Id > 0) {
        $.each(gyms, function () {
            if (this.Id == Id) {
                addMarker(this);
            }
        });
    }
    else {
        $.each(gyms, function () {
            addMarker(this);
        });
    }

    var bounds = new GBounds(points);
    var center = new GLatLng((latLngBounds.getSouthWest().lat() + latLngBounds.getNorthEast().lat()) / 2., (latLngBounds.getSouthWest().lng() + latLngBounds.getNorthEast().lng()) / 2.);

    if (Id == undefined)
        zoom = map.getBoundsZoomLevel(latLngBounds, map.getSize());

    map.setCenter(center, zoom);
    map.addControl(new GLargeMapControl());
    map.addControl(new GScaleControl());
    map.addControl(new GMapTypeControl());

}

function addMarker(gym) {

    var point = new GLatLng(gym.Address.Latitude, gym.Address.Longitude);

    if(gym.Name == 'Mountain View Christian' && $('#locations').val() == '0')
    {
	point = new GLatLng(36.15344, gym.Address.Longitude);
    }

    var icon = new GIcon(G_DEFAULT_ICON);
    icon.image = gym.ImagePath;
   
    icon.iconSize = new GSize(52, 52);

    var marker = new GMarker(point, { icon: icon, title: gym.Name, clickable: true });

    var html = "<b>" + gym.Name + "</b><br/>" + gym.Address.FullAddress + "<br/><a href=" + gym.Address.GoogleDirectionsUrl + ">Directions</a>";

    GEvent.addListener(marker, "mouseover", function () {
        marker.openInfoWindowHtml(html);
    });

    map.addOverlay(marker);
    latLngBounds.extend(point);

    points.push(point);
}

function getGyms() {

    var Id = parseInt($('#locations').val());

    if (Id > 0) {

        $.each(gyms, function () {

            if (this.Id == Id) {

                $('.Gym')
                            .find('.Image img').attr('src', this.ImagePath).end()
                            .find('.Details .Name').html(this.Name).end()
                            .find('.Details .Address').html(this.Address.FullAddress).end()
                            .find('.Details .DirectionLink a').attr('href', this.Address.GoogleDirectionsUrl).end()
                            .parent().show();

                initalizeMap(Id);

                return;
            }
        });

    }
    else {

        $.ajax({
            url: '/Ajax/GetGyms',
            type: "GET",
            contentType: "application/json",
            dataType: 'json',
            success: function (data) {
                gyms = data;
                initalizeMap();

                $('.Location').hide();
            }
        });
    }
}
