﻿//// ------- Required Variables.

var mapContainerID = "locMap";
var startingLongitude = "-114.127006";
var startingLatitude = "51.073054";
var defaultZoomLevel = 13;
var map = null;
var mapDiv = null;
var mapMarkerRequestObject = null;
var geocoder = null;
var gmarkers = [];
var htmls = [];
var bounds = new GLatLngBounds();       // This variable is used to track which area of the map to display.



//// ------- Map Initialization.

// Create the google map.
function createMap() {
    if (GBrowserIsCompatible()) {   
        // iniitialize the main map.
        mapDiv = document.getElementById(mapContainerID);
        map = new GMap2(mapDiv);
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        var moveEnd = GEvent.addListener(map, "load", onMapLoad());          
    } else {
        $("#locMap").html("Your browser isn't compatible with google maps.");        
    }
}

// Function gets called once the google map has been initialized.
function onMapLoad() {
    
    //Centre the map.
    centreMapToDefault();

    // setup the mouse wheel features.
    map.enableScrollWheelZoom();
    GEvent.addDomListener(mapDiv, "DOMMouseScroll", onWheelEvent);
    mapDiv.onmousewheel = onWheelEvent;

    //Load the map markers
    LoadMapMarkers();

}

// The event handler for when someone scrolls the mouse wheel.
function onWheelEvent(e) {
    if (!e) e = window.event;
    if (e.preventDefault) e.preventDefault();
    e.returnValue = false;
}




//// ------- Map Position/Zoom Methods.

// Reset the map zoom level to the default level.
function resetMapZoomLevel() {
    map.setZoom(defaultZoomLevel);
}

// Centre the map to the default level.
function centreMapToDefault() {
    map.setCenter(new GLatLng(startingLatitude, startingLongitude), defaultZoomLevel);
}





//// ------- Map Marker Population and Handlers.

// This function is used to load the map markers xml.
// The getMarkerXMLPath javascript function must be 
// implemented on the asp.net page because of the relative path.
function LoadMapMarkers() {
    
    // Read the xml path from the asp.net page.
    var mapXMLPath = getMarkerXMLPath();   
   
    //Create the xmlhttp request object.
    mapMarkerRequestObject = GXmlHttp.create();
    mapMarkerRequestObject.open("GET", mapXMLPath, true);
    
    //Setup the state change handler.
    mapMarkerRequestObject.onreadystatechange = function() {
    if (mapMarkerRequestObject.readyState == 4) {
            
            var xmlDoc = GXml.parse(mapMarkerRequestObject.responseText);
            
            // obtain the array of markers and loop through it adding each marker to the map.
            var markers = xmlDoc.documentElement.getElementsByTagName("marker");
            for (var i = 0; i < markers.length; i++) {
                // obtain the attribues of each marker
                var address = markers[i].getAttribute("address");
                var html = markers[i].getAttribute("html");
                var id = markers[i].getAttribute("id");
                addMarkerByAddress(address, html, id);
            }
            
        }
    }
    mapMarkerRequestObject.send(null);
}


// This function is used to add a marker to the map using just an address.
// There is no caching on this function. Every request is geocoded.
function addMarkerByAddress(address, toolTipHtml, markerID) {
    geocoder = new GClientGeocoder();
    geocoder.getLatLng(address,
    function(point) {
        if (!point) {
            //Add error handling here if the address can't be geocoded.
            alert(address + " not found");
        } else {
            addMarker(point, toolTipHtml, markerID);            
        }
    }
  );
}


// Add a single marker to the map.
function addMarker(point, toolTipHtml, markerID) {
   
   // Create the marker and setup the click handler.
    var marker = new GMarker(point);
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(toolTipHtml);
        map.panTo(marker.getPoint());       
    });

    // Add the point to the existing bounds.
    bounds.extend(point);

    // Add the marker to the map overlay.
    map.addOverlay(marker);

    // Store the marker and html into an associative array to access later.
    gmarkers[markerID] = marker;
    htmls[markerID] = toolTipHtml;

    // Now we'll change the map view port so that
    // it shows all the points in our bounds collection.
    map.setZoom(map.getBoundsZoomLevel(bounds)-1);
    map.setCenter(bounds.getCenter());

}

//Call this function with a specific marker id to open the tooltip.
function openToolTipByID(markerID) {
   
    // Get the marker from the array.
    var markerToOpen = gmarkers[markerID];
    
    // Open the info window with the correct html.
    if (markerToOpen){
        markerToOpen.openInfoWindowHtml(htmls[markerID]);
        map.panTo(markerToOpen.getPoint());
        resetMapZoomLevel();
    }   
}


//
