﻿
//Class which provides a bridge between the Project and the Google Maps API. 
function GoogleMapWrapper() {
    this.arrAddress;
    this.divId;
}
function GoogleMapWrapper(ArrAddress, DivID) {
    this.arrAddress = new Array(ArrAddress.length);
    for (i = 0; i < ArrAddress.length; i++) {        
        var address = new String();
        if (ArrAddress[i].AddressLine.length > 0)       
            address = ArrAddress[i].AddressLine += ",";
        if (ArrAddress[i].PrimaryCity.length > 0)       
            address += ArrAddress[i].PrimaryCity += ",";
        if (ArrAddress[i].Subdivision.length > 0)       
            address += ArrAddress[i].Subdivision;
        if (ArrAddress[i].PostalCode.length > 0)
            address += ArrAddress[i].PostalCode += " ";        
        if (ArrAddress[i].CountryRegion.length > 0)
            address += ArrAddress[i].CountryRegion;       
        this.arrAddress[i] = address;
    }    
    this.divID = DivID;
}
function DisplayGoogleMap() {    
    if (GBrowserIsCompatible()) {
        var htmlforWindow;
        // A function to create the marker and set up the event window        
        function createMarker(point, html) {
            var marker = new GMarker(point);
            GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(html);
            });
            return marker;
        }   
        
        // Set up markers with info windows                                
        geocoder = new GClientGeocoder();
        for (var i = 0; i < this.arrAddress.length; i++) {
            if (geocoder) {
                htmlforWindow = this.arrAddress[i];
                geocoder.getLatLng(this.arrAddress[i], function(point) {
                    if (!point) {
                        MapDiv.innerHTML = "<span class=" + "labels" + ">Address provided for this Business is not valid a address.</span>";
                    }
                    else {
                        var map = new GMap2(document.getElementById("MapDiv"));
                        map.addControl(new GMapTypeControl());
                        map.setCenter(new GLatLng(43.907787, -79.359741), 11);
                        map.setCenter(point, 11);
                        var marker = createMarker(point, htmlforWindow);
                        map.addOverlay(marker);
                    }
                }
            );
            }
        }
    }
    // display a warning if the browser was not compatible
    else {
        alert("Sorry, the Google Maps API is not compatible with this browser");
    }
}
//Class Prototype
GoogleMapWrapper.prototype =
{
    DisplayGoogleMap: DisplayGoogleMap
}

 
