﻿var otgMapping = {
    markersArray: new Array(),
    map: null,
    initialize: function(lat, lng, zoom, boolLoadMarkers) {
        otgMapping.map = new GMap2(document.getElementById("map_canvas"));
        otgMapping.map.setCenter(new GLatLng(lat, lng), zoom);
        //otgMapping.map.setUIToDefault();
        otgMapping.map.addControl(new GLargeMapControl3D());
        otgMapping.map.addControl(new GMapTypeControl());
        // Call the page method to retrieve the locations of the meetings
        // for the last 14 days
        if (boolLoadMarkers == true)
            otgMapping.loadMapMarkers();
    },

    loadMapMarkers: function() {
        PageMethods.GetMeetingMarkers(otgMapping.loadMapMarkersSuccess, function(e) { });
    },

    loadMapMarkersSuccess: function(markers) {
        // add the pushpins to the map
        for (x in markers) {
            var point = new GLatLng(markers[x].latitude, markers[x].longitude);
            var icon = new GIcon(G_DEFAULT_ICON);
            var marker = new GMarker(point, { clickable: true, bouncy: true, icon: icon });
            var markerHtml = (markers[x].speaker != "" ? markers[x].speaker + "<br />" : "") +
                (markers[x].location != "" ? markers[x].location + "<br />" : "") +
                markers[x].address;
            otgMapping.markersArray[markers[x].mid] = marker;
            otgMapping.setMarkerInfo(marker, markers[x].meetingTime, markerHtml + "<br />" +
                                     "<input type=\"image\" style=\"border-width: 0px; margin-top: 10px; margin-bottom: 10px; cursor: pointer;\"" +
                                     " onclick=\"setCurrentMidValue(" + markers[x].mid + ");" +
                                     " loadMeetingData(); return false;\"" +
                                     " src=\"images/button-meeting-details.png\" />");
            otgMapping.map.addOverlay(marker);
        }
    },

    setMarkerInfo: function(marker, title, location) {
        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml("<b>" + title + "</b><br /> " + location + "<br />");
        });
    },

    zoomToMarker: function(mid) {
        // Center and zoom to a marker by meeting ID
        var marker = otgMapping.markersArray[mid];
        if (marker != null) {
            google.maps.Event.trigger(marker, "click");
            //otgMapping.map.setZoom(10);  // Uncomment this line to automatically zoom in on the map as well.
        }
        else {
            // if the marker doesn't exist, try to get it via Ajax
            PageMethods.GetMeetingMarker(mid, otgMapping.loadMapMarkerSuccess, function(e) { alert(e.get_message()); });
        }
    },

    loadMapMarkerSuccess: function(markerData) {
        // Add pushpin to the map and zoom/center on it
        var point = new GLatLng(markerData.latitude, markerData.longitude);
        var icon = new GIcon(G_DEFAULT_ICON);
        var marker = new GMarker(point, { clickable: true, bouncy: true, icon: icon });
        otgMapping.markersArray[markerData.mid] = marker;
        otgMapping.setMarkerInfo(marker, markerData.meetingTime,
                                 markerData.speaker + "<br />" + markerData.location + "<br />" + markerData.address);
        otgMapping.map.addOverlay(marker);
        otgMapping.zoomToMarker(markerData.mid);
    },

    markOnMap: function(address, city, state, zip, country) {
        otgMapping.map.clearOverlays();
        PageMethods.GetMarker(address, city, state, zip, country, otgMapping.loadMapMarkerSuccess, function(e) { alert(e.get_message()); });


    },

    loadMapMarkerSuccess2: function (markerData) {
        // Add pushpin to the map and zoom/center on it
        var point = new GLatLng(markerData.latitude, markerData.longitude);
        var icon = new GIcon(G_DEFAULT_ICON);
        var marker = new GMarker(point, { clickable: true, bouncy: true, icon: icon });
        otgMapping.markersArray[markerData.mid] = marker;
        otgMapping.setMarkerInfo(marker, markerData.meetingTime,
                                 "<br />" + markerData.address);
        otgMapping.map.addOverlay(marker);
        otgMapping.zoomToMarker(markerData.mid);
    },

    markOnMap2: function(address, city, state, zip, country, description) {
        otgMapping.map.clearOverlays();
        PageMethods.GetMarkerForYourLocation(address, city, state, zip, country, description, otgMapping.loadMapMarkerSuccess2, function (e) { alert(e.get_message()); });


    }


}
