﻿/* Handles the popup for the scrolley password recovery */

$(document).ready(function() {
    PageInit();
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
});

function PageInit() {
    $(".jViewMapButton").click(function() {
        $(this).parent().parent().parent().parent().find(".GigsDayPopup").animate({ height: 'show', opacity: 'show' }, 'slow')
        LoadMap($(this).parent().parent().parent().parent());
        return false;
    });

    $(".ClosePopup").click(function() {
        $(".GigsDayPopup").animate({ height: 'hide', opacity: 'hide' }, 'slow');
    });
}

function EndRequestHandler() {
    PageInit();
}

function LoadMap(parent) {
    if (GBrowserIsCompatible()) {
        function createMarker(point, html) {
            var marker = new GMarker(point);
            map.addOverlay(marker);
            GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(html);
            });
            return marker;
        }

        var map = new GMap2($(parent).find(".Map")[0]);
        var top, bottom, left, right;
        var centre;
        var defaultMarker;
        var defaultMarkerInfo;
        var defaultZoom = 14;
        var count = 0;

        $(parent).find(".GigMapItem").each(function() {
            count++;

            var latitude = $(this).find("*[id$='Latitude']").val();
            var longitude = $(this).find("*[id$='Longitude']").val();

            top = (top == null) ? latitude : Math.min(top, latitude);
            bottom = (bottom == null) ? latitude : Math.max(bottom, latitude);
            left = (left == null) ? longitude : Math.min(left, longitude);
            right = (right == null) ? longitude : Math.max(right, longitude);

            var markerLocn = new GLatLng(latitude, longitude);
            var details = $(this).find("*[id$='MapData']").html();

            marker = createMarker(markerLocn, details);

            if (count == 1) {
                centre = new GLatLng(latitude, longitude);
                defaultMarker = marker;
                defaultMarkerInfo = details;
            }
        });

        if (count == 1) {
            map.setCenter(centre, defaultZoom);
            defaultMarker.openInfoWindowHtml(defaultMarkerInfo);
        }
        else {
            centre = new GLatLng((top + bottom) / 2, (left + right) / 2);

            /* add 10% to bounded view area */
            left -= (right - left) / 10;
            right += (right - left) / 10;
            top -= (bottom - top) / 10;
            bottom += (bottom - top) / 10;

            var corners = [
                new GLatLng(top, left),
                new GLatLng(top, right),
                new GLatLng(bottom, right),
                new GLatLng(bottom, left)
            ];
            var polygon = new GPolygon(corners);

            map.setCenter(centre, map.getBoundsZoomLevel(polygon.getBounds()));
        }

        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
    }
}