/*
A google map showing locations.
To use it you need the following in the header tag of your html:

<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=GMAPS_KEY" type="text/javascript"></script>
<script src="/media/geo/js/map.js" type="text/javascript"></script>
<script type="text/javascript">
    window.onunload=GUnload
</script>

And somewhere in the body tag:

<div id="map_canvas">
</div>

Then call initmap with the necessary arguments to build the map (e.g. in window.onload).
*/

/*
 * Initialises the map with the specified center and locations.
 */
function initmap(center, locations, maxZoom) {
  if (GBrowserIsCompatible()) {

    if (!center | !center.latitude)
        return;

    if (!maxZoom)
        maxZoom = 13;

    center.latlng = new GLatLng(center.latitude, center.longitude);

    var baseIcon = new GIcon();
    baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    baseIcon.iconSize = new GSize(20, 34);
    baseIcon.shadowSize = new GSize(37, 34);
    baseIcon.iconAnchor = new GPoint(9, 34);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);
    baseIcon.infoShadowAnchor = new GPoint(18, 25);

    var hiltonIcon = new GIcon();
    hiltonIcon.iconSize = new GSize(32, 32);
    hiltonIcon.iconAnchor = new GPoint(16, 16);

    function createMarker(latlng, name, type) {
        var icon = null;

        if ((type == 'hi') || (type == 'gi')) {
            icon = new GIcon(hiltonIcon);
        } else {
            icon = new GIcon(baseIcon);
        }

        icon.image = "/media/geo/marker_" + type + ".png";


        markerOptions = { icon:icon };
        var marker = new GMarker(latlng, markerOptions);
        marker.value = name;
        GEvent.addListener(marker,"click", function() {
            map.openInfoWindowHtml(latlng, name);
        });
        return marker;
    }

    var map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(center.latlng, zoomLevel);
    map.addControl(new GSmallMapControl());
    //map.addControl(new GScaleControl());

    var latlngs = new GLatLngBounds();
    if (center.name)
        map.addOverlay(createMarker(center.latlng, center.name, 'center'));
    latlngs.extend(center.latlng);
    // We are doing lazy displaying here, merging locations with same coordinates
    // and adding them to the map once we detect a new one.
    // Merges only if the list of locations is sorted.
    var merge = undefined;
    for (var i=0; i<locations.length; i++) {
        if (locations[i].latitude == null)
            continue;
        if (merge) {
            if ((merge.latitude == locations[i].latitude) &&
                    (merge.longitude == locations[i].longitude) &&
                    (merge.type == locations[i].type)) {
                merge.name += '<br />' + locations[i].name
            } else {
                var latlng = new GLatLng(merge.latitude, merge.longitude);
                map.addOverlay(createMarker(latlng, merge.name, merge.type));
                latlngs.extend(latlng);
                merge = locations[i];
            }
        } else
            merge = locations[i];
    }
    if (merge) {
        var latlng = new GLatLng(merge.latitude, merge.longitude);
        map.addOverlay(createMarker(latlng, merge.name, merge.type));
        latlngs.extend(latlng);
    }
    var zoomLevel = map.getBoundsZoomLevel(latlngs);
    if (zoomLevel > maxZoom)
        zoomLevel = maxZoom;
    map.setZoom(zoomLevel);

    // Zoom out once if necessary to view all locations
    if (!map.getBounds().contains(latlng)) {
        map.setZoom(zoomLevel-1);
    }

  }
}
