/*
 * googlemaps.js
 *
 * 2/9/09 - T.Gryffyn tgryffyn@confluencecorp.com
 *
 * JS for handling placing markers and interacting with Google Maps
 * Requires standard Google Maps JS include
 *
 */

/* <script src="http://maps.google.com/maps?file=api&amp;v=1&amp;key=<?php echo $gmaps_apikey; ?>" type="text/javascript"></script> */


    var map;
    var bounds = new GLatLngBounds();
    var markerHandles = new Array();
    // Create our lettered marker icons
    var icons = new Array();
    icons[""] = new GIcon();
    icons[""].image = "http://www.google.com/mapfiles/marker.png";
    icons[""].shadow="http://www.google.com/mapfiles/shadow50.png";
    icons[""].iconSize=new GSize(20, 34);
    icons[""].shadowSize=new GSize(37, 34);
    icons[""].iconAnchor=new GPoint(9,34);
    icons[""].infoWindowAnchor=new GPoint(9,2);
    icons[""].infoShadowAnchor=new GPoint(18,25);
    icons[""].printImage="http://www.google.com/mapfiles/markerie.gif";
    icons[""].mozPrintImage="http://www.google.com/mapfiles/markerff.gif";
    icons[""].printShadow="http://www.google.com/mapfiles/dithshadow.gif";
    icons[""].transparent="http://www.google.com/mapfiles/markerTransparent.png";
    icons[""].imageMap=[9,0,6,1,4,2,2,4,0,8,0,12,1,14,2,16,5,19,7,23,8,26,9,30,9,34,11,34,11,30,12,26,13,24,14,21,16,18,18,16,20,12,20,8,18,4,16,2,15,1,13,0];

    function initGoogleMaps(markercnt) {
        if (GBrowserIsCompatible()) {
            // resize the map
            map = new GMap(document.getElementById("map"));

            // create the map
            map.addControl(new GSmallMapControl());
//            map.addControl(new GMapTypeControl());
            map.centerAndZoom(new GPoint(-79.359741, 43.907787), 8);
        } else {
            alert("your browser does not support Google Maps!");
        }
// alert("test" + markercnt);
        processMarkers(markercnt);
    }

    function get_icon(iconStr) {
        if ((typeof(iconStr)=="undefined") || (iconStr==null) || (iconStr < "A") || (iconStr > "Z")) {
            // google only has markers A - J
            iconStr = "";
        } else if ((typeof iconStr == "string") && (iconStr.length > 1)) {
            iconStr = "";
        }

        if (!icons[iconStr]) {
            icons[iconStr] = new GIcon(icons[""]);
            icons[iconStr].image = "http://www.google.com/mapfiles/marker"+ iconStr +".png";
        }

        return icons[iconStr];
    }

    // A function to create the marker and set up the event window
    function createMarker(point, name, html, iconStr, id) {
        // FF 1.5 fix
        html = '<div style="white-space:nowrap;">' + html + '</div>';
// alert("point:" + point + " name:" + name + " html:" + html + " iconStr:" + iconStr + " id:" + id);
        var marker = new GMarker(point);
        if (iconStr) {
            marker = new GMarker(point, get_icon(iconStr));
        }

        markerHandles[id] = marker;

        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(html);
        });

        return marker;
    }


    function zoomfit() {
        newzoom = getBoundsZoomLevel(bounds);
        newcenter = bounds.getCenter();
        map.setCenter (newcenter,newzoom);
    }


    function processMarkers(markercnt) {
        map.clearOverlays();
// alert("test2");
        for (var i = 0; i < markercnt; i++) {
            // obtain the attribues of each marker
            var lat   = $('#marker'+i).data('lat');
            var lng   = $('#marker'+i).data('lng');
// alert('setting lat:' + lat + ' lng:'+lng+' i:'+i+' cnt:'+markercnt+' jquery:'+$('#marker'+i).data('lat'));
            var point = new GPoint(lng, lat);
            var html  = $('#marker'+i).data('html');
            var label = $('#marker'+i).data('label');
            var icon  = $('#marker'+i).data('icon');

            // create the marker
            var marker = createMarker(point, label, html, icon, i);
            map.addOverlay(marker);
/*
            alert('ts'+bounds.toSpan());
            alert('gc'+bounds.getCenter());
            alert('sw'+bounds.getSouthWest());
            alert('nw'+bounds.getNorthEast());
*/
            // bounds.extend(point);
            // zoomfit();
/*
            if (markercnt == 1) {
                map.centerAndZoom(new GPoint(lng, lat), 8);
            }
*/

        }
// alert("test3");

    }

    function showmarker(id) {
// alert(id);
        var lat = $('#marker'+id).data('lat');
        var lng = $('#marker'+id).data('lng');
        var html = $('#marker'+id).data('html');
        map.panTo(new GLatLng(lat, lng));
        // markerHandles[id].openInfoWindow(html, 2);
    }
