﻿// Call this function when the page has been loaded
function initializeGoogleMap(containerId, address, latitude, longitude, coordinates, zoomLevel, hideControls, mapType) {
    if (GBrowserIsCompatible()) {
        var container = document.getElementById(containerId);
        container.style.overflow = 'hidden';
        var map = new google.maps.Map2(container);
        map.setMapType(mapType);
        if(!hideControls) {
            map.addControl(new GSmallMapControl());
            map.addControl(new GMapTypeControl());
            map.addControl(new GOverviewMapControl());
        }
        if(((longitude != null && longitude != '') && (latitude != null && latitude != '')) || (coordinates != null && coordinates != '')) {
            var point = null;
            if (coordinates != null && coordinates != '') {
                var coordPair = coordinates.split(',');
                point = new GLatLng(coordPair[0], coordPair[1]);
            } else {
                point = new GLatLng(latitude, longitude);
            }
            
            if (!point) {
                alert(address + " not found");
            } else {
                map.setCenter(point, zoomLevel);
                var marker = new GMarker(point);
                map.addOverlay(marker);
            }
        }
        else if(address != null && address != '') {
            var geocoder = new GClientGeocoder();

            function showAddress(address, zoomLevel) {
              geocoder.getLatLng(
                address,
                function(point) {
                  if (!point) {
                    alert(address + " not found");
                  } else {
                    map.setCenter(point, zoomLevel);
                    var marker = new GMarker(point);
                    map.addOverlay(marker);
                  }
                }
              );
            }
        
            showAddress(address, zoomLevel);
        }
    } else {
        alert("Your web browser is not compatible with Google Maps");
    }
}

function showPopupMap(width, height, address, latitude, longitude, coordinates, zoomLevel, hideControls, mapType) {
    Shadowbox.open({
        player: 'html',
        content: '',
        width: width,
        height: height,
        options: { onFinish: function() { initializeGoogleMap('shadowbox_content', address, latitude, longitude, coordinates, zoomLevel, hideControls, mapType); } }
    });
}