﻿google.load("maps", "2", { "other_params": "sensor=false" });
//google.setOnLoadCallback(initializeGeneralMap);

var baseIcon;

var marker;

// Creates a marker whose info window displays the letter corresponding
// to the given index.
function createMarker(point, index, venueID) {
    // Create a lettered icon for this point using our icon class
    var letter = String.fromCharCode("A".charCodeAt(0) + index);
    var letteredIcon = new google.maps.Icon(baseIcon);
    letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
    // Set up our GMarkerOptions object
    markerOptions = { icon: letteredIcon };
    var marker = new google.maps.Marker(point, markerOptions);
    if (venueID != null) {
        google.maps.Event.addListener(marker, "click", function() {
            showOffice(venueID);
        });
    }
    return marker;
}

function showOffice(venueID) {
    //alert(venueID);
    window.location = "Locations.aspx?VenueID=" + venueID.toString();
}

function initializeGeneralMap() {

    if (google.maps.BrowserIsCompatible()) {


        // Create a base icon for all of our markers that specifies the
        // shadow, icon dimensions, etc.
        baseIcon = new google.maps.Icon(G_DEFAULT_ICON);
        baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        baseIcon.iconSize = new google.maps.Size(20, 34);
        baseIcon.shadowSize = new google.maps.Size(37, 34);
        baseIcon.iconAnchor = new google.maps.Point(9, 34);
        baseIcon.infoWindowAnchor = new google.maps.Point(9, 2);

        var map = new google.maps.Map2(document.getElementById("GeneralMap"));
        map.disableDragging();

        //create marker data
        var markerData = getMarkerData();

        //add markers and calculate centre of map
        var minLat = 360;
        var minLng = 360;
        var maxLat = 0;
        var maxLng = 0;
        var numMarkers = markerData.length;
        for (var i = 0; i < numMarkers; i++) {
            var place = markerData[i];
            var thisLat = place["posn"][0];
            var thisLng = place["posn"][1];
            var point = new google.maps.LatLng(thisLat, thisLng);
            var marker = createMarker(point, i, place["venueID"]);
//            google.maps.Event.addListener(marker, "click", function() {
//                showOffice(place["venueID"]);
            //            });
//            google.maps.Event.addListener(marker, "click", function() {
//                showOffice(i);
//            });
            map.addOverlay(marker);
            minLat = Math.min(minLat, thisLat);
            //alert('thisLat = ' + thisLat.toString() + '; minLat = ' + minLat.toString());
            minLng = Math.min(minLng, thisLng);
            maxLat = Math.max(maxLat, thisLat);
            maxLng = Math.max(maxLng, thisLng);
        }
        var midLat = (minLat + maxLat) / 2;
        var midLng = (minLng + maxLng) / 2;
        var minLatLng = new google.maps.LatLng(minLat, minLng);
        var maxLatLng = new google.maps.LatLng(maxLat, maxLng);
        var markersLatLngBounds = new google.maps.LatLngBounds(minLatLng, maxLatLng);

        var zoom = 15;  //reasonable street level to start with

//        map.setCenter(new google.maps.LatLng(midLat, midLng), zoom);
        map.setCenter(new google.maps.LatLng(51.65, -0.4), zoom);
        //        alert('minLat = ' + minLat.toString() + '; minLng = ' + minLng.toString() + '; maxLat = ' + maxLat.toString() + '; maxLng = ' + maxLng.toString());

        while (map.getBounds().containsBounds(markersLatLngBounds) == false && zoom > -1) {
            zoom--;
            map.setZoom(zoom);
        }

    }
}

function initializeTestMap() {
    var map = new google.maps.Map2(document.getElementById("GeneralMap"));
    map.setCenter(new google.maps.LatLng(50.964509, -0.130209), 14);
}

function initializeVenueMap() {

    if (google.maps.BrowserIsCompatible()) {

        // Create a base icon for all of our markers that specifies the
        // shadow, icon dimensions, etc.
        baseIcon = new google.maps.Icon(G_DEFAULT_ICON);
        baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        baseIcon.iconSize = new google.maps.Size(20, 34);
        baseIcon.shadowSize = new google.maps.Size(37, 34);
        baseIcon.iconAnchor = new google.maps.Point(9, 34);
        baseIcon.infoWindowAnchor = new google.maps.Point(9, 2);

        var map = new google.maps.Map2(document.getElementById("VenueMap"));
        map.disableDragging();

        //create marker data
        var place = getMarkerData();

        var thisLat = place["posn"][0];
        var thisLng = place["posn"][1];
        var point = new google.maps.LatLng(thisLat, thisLng);
        var marker = createMarker(point, place["index"], null);
//        google.maps.Event.addListener(marker, "click", function() {
//        alert(place["postcode"]);
//        });
        map.addOverlay(marker);

        var zoom = 15;  //reasonable street level to start with

        map.setCenter(new google.maps.LatLng(thisLat, thisLng), zoom);

    }
}

if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
