﻿var map = null;
var geocoder = null;
var bounds = null;

function initializeGMap(idcanvas) {
    if (GBrowserIsCompatible()) {
        // Map creation
        map = new GMap2(document.getElementById(idcanvas));
        // Initialize geocoder
        geocoder = new GClientGeocoder();
        // Initialize map bounds
        bounds = new GLatLngBounds();
        // Initialize map control
        map.setUIToDefault();
        //Center the map to default location
        map.setCenter(new GLatLng(46.058368, -71.950253), 12);
    }
}

// Extends the bounds of the map so every markers can be seen
function extendMap(point) {
    bounds.extend(point);
    map.setZoom(map.getBoundsZoomLevel(bounds));
    map.setCenter(bounds.getCenter());
}

// Creates a new marker
function createMarker(point, title, iconFile, html, display) {
    var marker;
    if (iconFile == "") {
        marker = new GMarker(point, { title: title });
    } else {
        var markerIcon = new GIcon(G_DEFAULT_ICON, iconFile);
        markerIcon.iconSize = new GSize(33, 33);
        markerIcon.iconAnchor = new GPoint(0, 33);
        markerIcon.shadow = "images/icon/shadow.png";
        markerIcon.shadowSize = new GSize(40, 45);
        // markerIcon.printimage =
        // markerIcon.mozPrintimage =

        marker = new GMarker(point, { icon: markerIcon, title: title });
    }

    // make the marker clickable
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
    });

    // Display the html description if needed
    if (display == true) {
        map.openInfoWindowHtml(point, html);
    }

    // Add the marker to the map
    map.addOverlay(marker);

    // Extend the bounds of the map to include the newly created marker
    extendMap(point);
}

// Creates a new marker from an address
function createMarkerAdd(address, title, iconFile, html, display) {
    if (geocoder) {
        geocoder.getLatLng(
       		address,
          	function(point) {
          	    if (point) {
          	        createMarker(point, title, iconFile, html, display);
          	        // Uncomment to display the lat/lng of each addresses
          	        // alert("Address: " + address + "\nLat: " + point.y + "\nLng: " + point.x);
          	    }
          	}
   		);
    }
}

// Creates a new marker from latitude and longitude
function createMarkerLatLng(lat, lng, title, iconFile, html, display) {
    var point = new GLatLng(lat, lng);
    createMarker(point, title, iconFile, html, display);
}



