/* Googlemaps overzicht template code */
var divMap = false;
var map = false;
var bounds = false;
var googleMapsAvailable = true;

var useLargeMapControl = "ja";
var useMapTypeControl = "ja";
var useScaleControl = "ja";
var zoomCorrectie = "1";
var baseIcon = false;
var index = 0;

$(document).ready(function() {

    // Create a base icon for all of our markers that specifies the
    // shadow, icon dimensions, etc.
window.onunload = google.maps.Unload; 
    baseIcon = new GIcon(G_DEFAULT_ICON);
    initializeGoogleMaps("google_maps");

	

});

function initializeGoogleMaps(divMapId) {
    divMap = document.getElementById(divMapId);

    // Check if Google Maps is able to be used on the current browser
    if (GBrowserIsCompatible() && divMap) {
        // Create a map

        map = new GMap2(divMap);

        // Set the initial location
        bounds = new GLatLngBounds();


        //map.setCenter(new GLatLng(locations[0].lat, locations[0].lng), 13);

        // Add markers
        for (var i = 0; i < locations.length; i++) {
            if (locations[i].name != "last") {

                var geocoder = new GClientGeocoder();
                geocoder.getLatLng(locations[i].address,
                  function(point) {
                      if (!point) {
                          //alert(address + " not found");
                          index++;
                      }
                      else {
                          map.addOverlay(createMarker(point, index));
                          bounds.extend(point);
                          index++;

                          // Set zoomlvl correction, maybe the view has to be zoomed out a little bit
                          var zoomCorrectieType = false;

                          if (!isNaN(zoomCorrectie)) {
                              zoomCorrectie = parseInt(zoomCorrectie);
                          }
                          else {
                              zoomCorrectieType = zoomCorrectie;
                              zoomCorrectie = 0;
                          }

                          var currentZoomLevel = map.getBoundsZoomLevel(bounds);

                          if ((currentZoomLevel - zoomCorrectie) >= 1 && !zoomCorrectieType) {
                              currentZoomLevel = currentZoomLevel - zoomCorrectie;
                          }
                          else if (zoomCorrectieType == "Max") {
                              currentZoomLevel = 1;
                          }

                          map.setCenter(bounds.getCenter(), currentZoomLevel);
                      }
                  }
                );
            }
        }

        

        if (useLargeMapControl == "ja") {
            map.addControl(new GLargeMapControl());
        }
        if (useMapTypeControl == "ja") {
            map.addControl(new GMapTypeControl());
        }
        if (useScaleControl == "ja") {
            map.addControl(new GScaleControl());
        }
    }
}

// Creates a marker at the given point
// The five markers show a secret message when clicked
// but that message is not within the marker's instance data
function createMarker(point, index) {
    var blueIcon = new GIcon(baseIcon);

    //blueIcon.image = "/images/gmaps/gmaps_marker_" + locations[index].markercolor + "_" + locations[index].markerchar + ".png";
    markerOptions = { icon: blueIcon };

    var marker = new GMarker(point, markerOptions);

    GEvent.addListener(marker, "click", function() {
        var myHtml = '<div><b>' + locations[index].name + '</b>' +
			'<br>' + locations[index].address + '<br>' +
			'<a target="_blank" ' +
			'href="' + locations[index].website + '">Details</a>' +
			'<br></div>';
        map.openInfoWindowHtml(point, myHtml);
    });

    return marker;
}

// Formats info to HTML friendly
function formatAddressForMaps(location) {
    var address = location.street + ' ' + location.city + ' ' + location.zip + ' ' + location.country;

    return escape(address.replace(' ', '+'));
}

// Adjusts zoom level to view all
function zoomToAll() {
    map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
}


