﻿var map = null;
var geocoder = null;
var lastPoint = null;

function load(divGoogleId) {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById(divGoogleId));
        map.setCenter(new GLatLng(51.7519, 5.5419), 8);
        map.enableDoubleClickZoom();
        map.enableScrollWheelZoom();
        geocoder = new GClientGeocoder();
    }
}

function showGoogleMap(locations, divGoogleId, divErrorId) {
    var divGoogleMap = $get(divGoogleId);
    if (divGoogleMap != null) {
        if (divGoogleMap.style.display != 'none') {
            divGoogleMap.style.display = 'none';
        }
    }

    // Reset error label visibility
    var errorLabel = $get(divErrorId);
    if (errorLabel != null) {
        if (errorLabel.style.display != 'none') {
            errorLabel.style.display = 'none';
        }
    }
    
    if (divGoogleMap != null) {
        divGoogleMap.className = "";

        for (var i = 0; i < locations.length; i++) {
            load(divGoogleId);
            if (geocoder) {
                geocoder.getLatLng(
                          locations[i],
                          function(point) {
                              if (!point) {
                                  // Make error visible
                                  if (errorLabel != null) {
                                      if (errorLabel.style.display != 'block') {
                                          errorLabel.style.display = 'block';
                                      }
                                  }
                              } else {
                                  // Make google div visible
                                  if (divGoogleMap != null) {
                                      if (divGoogleMap.style.display != 'block') {
                                          divGoogleMap.style.display = 'block';
                                      }
                                  }

                                  var newIcon = MapIconMaker.createMarkerIcon({ width: 32, height: 32, primaryColor: "#FF6600" });
                                  var marker = new GMarker(point, { icon: newIcon });
                                  map.addOverlay(marker);
                                  GEvent.addListener(marker, 'click', function() {
                                      marker.openInfoWindowHtml(locations[i]);
                                  });

                                  lastPoint = point;
                                  map.checkResize();
                                  map.setCenter(point, 6);
                              }
                          }
                        );
            }
        }
    }
}
