﻿

//google.load("maps", "2");

jQuery(document).unload(function() {
    google.maps.Unload();
});

var map;

//jQuery(document).ready(function() {

// create the map.
function GMload() {
    if (GBrowserIsCompatible()) {

        if (!hasMap) return;

        var map = new GMap2(document.getElementById("map"));
        //map.addControl(new GLargeMapControl());
        //map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(homeSpatialInfo.latitude, homeSpatialInfo.longitude), 10);
        map.setUIToDefault();
        // Create a base icon for all of our markers that specifies the
        // shadow, icon dimensions, etc.
        var baseIcon = new GIcon();
        baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        baseIcon.iconSize = new GSize(20, 34);
        baseIcon.shadowSize = new GSize(37, 34);
        baseIcon.iconAnchor = new GPoint(9, 34);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);
        baseIcon.infoShadowAnchor = new GPoint(18, 25);

        // Creates a marker whose info window displays the letter corresponding// to the given index.
        function createMarker(point, name, address, urladdress, locationNumber) {
            var icon = new GIcon(baseIcon);
            icon.image = "./content/images/markers/marker" + locationNumber + ".png";
            var marker = new GMarker(point, icon);
            GEvent.addListener(marker, "click", function() {

                map.closeInfoWindow();
                map.panTo(marker.getLatLng());

                var windowData = '<h3>' + name + '</h3><br><div style="width:200px">'

                var addr = address.split("|");

                for (var i in addr) {
                    windowData += addr[i] + '<br />';
                }

                windowData += '<br /><a style="color: #000000" target="_blank" href="http://maps.google.com/maps?f=q&hl=en&q=from:' + homeSpatialInfo.fromAddress + '+to:' + urladdress + '">Directions</a></div>'

                marker.openInfoWindowHtml(windowData);
            });
            return marker;
        }

        // Load all the markers from the JSON ProximityLocations variable
        var bounds = map.getBounds();
        var southWest = bounds.getSouthWest();
        var northEast = bounds.getNorthEast();
        var lngSpan = northEast.lng() - southWest.lng();
        var latSpan = northEast.lat() - southWest.lat();
        for (var i = 0; i < ProximityLocations.locations.length; i++) {
            var point = new GLatLng(ProximityLocations.locations[i].latitude, ProximityLocations.locations[i].longitude);
            map.addOverlay(
                    createMarker(
                        point,
                        ProximityLocations.locations[i].name,
                        ProximityLocations.locations[i].address,
                        ProximityLocations.locations[i].urladdress, i + 1));
        }


    }
}



//})
