﻿var map;
var trafficInfo = null;

function fMarkers() {
    this.markers = new Array();
    this.getLength = function () { return this.markers.length; };
    this.pushValue = function (v) { this.markers.push(v); }
    this.getValue = function (i) { return this.markers[i]; }
    this.getLastValue = function () { return this.markers[this.markers.length - 1]; }
    this.getValueById = function (ID) {
        var i;
        for (i = 0; i < this.markers.length; i++) {
            if (this.markers[i].value == ID) {
                return this.markers[i];
            }
        }
        return null;
    }
    this.removeValueById = function (ID) {
        var i;
        for (i = 0; i < this.markers.length; i++) {
            if (this.markers[i].value == ID) {
                this.markers.splice(i, 1);
            }
        }
        return null;
    }
}

if (GBrowserIsCompatible()) {
    var markers = new fMarkers();

    function CreateMarker(point, icon1, InfoHTML, bDraggable, sTitle) {
        var marker = new GMarker(point, { icon: icon1, draggable: bDraggable, title: sTitle });
        if (InfoHTML != '') {
            GEvent.addListener(marker, "click", function () { this.openInfoWindowHtml(InfoHTML); });
        }
        GEvent.addListener(marker, "dragend", function () { GService.SetLatLon(map.ObjectKey, this.value, this.getLatLng().y, this.getLatLng().x); RaiseEvent('PushpinMoved', this.value); });
        return marker;
    }

    function OpenInfoWindow(id, InfoHTML) {

        var marker = markers.getValueById(id);
        if (marker != null) {
            marker.openInfoWindowHtml(InfoHTML);
        }
    }

    function fGetGoogleObject(result, userContext) {
        map.setCenter(new GLatLng(result.CenterPoint.Latitude, result.CenterPoint.Longitude), result.ZoomLevel);

        if (result.ShowMapTypesControl) {
            map.addControl(new GMapTypeControl());
        }

        if (result.ShowZoomControl) {
            map.addControl(new GSmallMapControl());
        }

        map.setMapType(eval(result.MapType));

        var i;
        if (markers != null) {
            for (i = 0; i < markers.getLength(); i++) {
                var cmark = markers.getValue(i);
                if (cmark != null) {
                    map.removeOverlay(cmark);
                }
            }
        }

        markers = new fMarkers();

        for (i = 0; i < result.Points.length; i++) {
            var myIcon_google;

            var myPoint = new GLatLng(result.Points[i].Latitude, result.Points[i].Longitude);

            myIcon_google = null;
            if (result.Points[i].IconImage != '') {
                myIcon_google = new GIcon(G_DEFAULT_ICON);
                markerOptions = { icon: myIcon_google };

                myIcon_google.iconSize = new GSize(result.Points[i].IconImageWidth, result.Points[i].IconImageHeight);
                myIcon_google.image = result.Points[i].IconImage;
                myIcon_google.shadow = result.Points[i].IconShadowImage;
                myIcon_google.shadowSize = new GSize(result.Points[i].IconShadowWidth, result.Points[i].IconShadowHeight);
                myIcon_google.iconAnchor = new GPoint(result.Points[i].IconAnchorPosX, result.Points[i].IconAnchorPosY);
                myIcon_google.infoWindowAnchor = new GPoint(result.Points[i].InfoWindowAnchorPosX, result.Points[i].InfoWindowAnchorPosY);
            }

            var marker = CreateMarker(myPoint, myIcon_google, result.Points[i].InfoHTML, result.Points[i].Draggable, result.Points[i].ToolTip);
            marker.value = result.Points[i].ID;
            markers.pushValue(marker);
            map.addOverlay(marker);
        }

        if (result.ShowTraffic) {
            trafficInfo = new GTrafficOverlay();
            map.addOverlay(trafficInfo);
        }
        if (result.AutomaticBoundaryAndZoom) {
            RecenterAndZoom(true, result);
        }
    }

    function DrawGoogleMap() {

        if (GBrowserIsCompatible()) {
            var containerDiv = document.getElementById("GoogleMap_Div");
            map = new GMap2(containerDiv);
            map.ObjectKey = containerDiv.getAttribute("gmapkey");

            geocoder = new GClientGeocoder();

            GService.GetGoogleObject(map.ObjectKey, fGetGoogleObject);
        }
    }

    function fGetGoogleObjectOptimized(result, userContext) {
        if (result.RecenterMap) {
            map.setCenter(new GLatLng(result.CenterPoint.Latitude, result.CenterPoint.Longitude), result.ZoomLevel);
            GService.RecenterMapComplete(map.ObjectKey);
        }

        map.setMapType(eval(result.MapType));

        if (result.ShowTraffic) {
            trafficInfo = new GTrafficOverlay();
            map.addOverlay(trafficInfo);
        }
        else {
            if (trafficInfo != null) {
                map.removeOverlay(trafficInfo);
                trafficInfo = null;
            }
        }

        var i;
        for (i = 0; i < result.Points.length; i++) {
            //Create icon
            var myIcon_google;

            var myPoint = new GLatLng(result.Points[i].Latitude, result.Points[i].Longitude);

            myIcon_google = null;
            if (result.Points[i].IconImage != '') {
                myIcon_google = new GIcon(G_DEFAULT_ICON);
                markerOptions = { icon: myIcon_google };

                myIcon_google.iconSize = new GSize(result.Points[i].IconImageWidth, result.Points[i].IconImageHeight);
                myIcon_google.image = result.Points[i].IconImage;
                myIcon_google.shadow = result.Points[i].IconShadowImage;
                myIcon_google.shadowSize = new GSize(result.Points[i].IconShadowWidth, result.Points[i].IconShadowHeight);
                myIcon_google.iconAnchor = new GPoint(result.Points[i].IconAnchorPosX, result.Points[i].IconAnchorPosY);
                myIcon_google.infoWindowAnchor = new GPoint(result.Points[i].InfoWindowAnchorPosX, result.Points[i].InfoWindowAnchorPosY);

            }
            //Existing marker, but changed.
            if (result.Points[i].PointStatus == 'C') {
                var marker = markers.getValueById(result.Points[i].ID);
                if (marker != null) {
                    markers.removeValueById(result.Points[i].ID);
                    map.removeOverlay(marker);
                }
                var marker = CreateMarker(myPoint, myIcon_google, result.Points[i].InfoHTML, result.Points[i].Draggable, result.Points[i].ToolTip);
                marker.value = result.Points[i].ID;
                markers.pushValue(marker);
                map.addOverlay(markers.getLastValue());
            }
            //New Marker
            if (result.Points[i].PointStatus == 'N') {
                var marker = CreateMarker(myPoint, myIcon_google, result.Points[i].InfoHTML, result.Points[i].Draggable, result.Points[i].ToolTip);
                marker.value = result.Points[i].ID;
                markers.pushValue(marker);
                map.addOverlay(markers.getLastValue());
            }
            //Existing marker, but deleted.
            if (result.Points[i].PointStatus == 'D') {
                var marker = markers.getValueById(result.Points[i].ID);
                if (marker != null) {
                    markers.removeValueById(result.Points[i].ID);
                    map.removeOverlay(marker);
                }
            }
        }

        if (result.AutomaticBoundaryAndZoom) {
            RecenterAndZoom(true, result);
        } else {
            Recenter(result);
        }
    }
}

// This function causes recentering of map. 
// It finds all visible markers on map and decides center point 
// and zoom level based on these markers.
function Recenter(result) {

    //Check if there is any visible pushpin on map.
    var cnt = 0;
    bounds = new GLatLngBounds();
    var objIgnore = document.getElementById('chkIgnoreZero');
    var bIgnore = false;
    if (objIgnore != null) {
        bIgnore = objIgnore.checked;
    }
    bIgnore = result.IgnoreZeroLatLngs;
    for (var i = 0; i < markers.getLength(); i++) {
        var ignoremarker = false;
        if (bIgnore) {
            var point1 = markers.markers[i].getPoint();
            if ((point1.x == 0) && (point1.y == 0)) {
                ignoremarker = true;
            }
        }
        if (!ignoremarker) {
            if (!markers.markers[i].isHidden()) {
                bounds.extend(markers.markers[i].getPoint());
                //Increment visible pushpin count
                cnt++;
            }
        }

    }
    var iZoomLevel = map.getBoundsZoomLevel(bounds);
    var point = bounds.getCenter();

    if (iZoomLevel > 14) {
        iZoomLevel = 14;
    }

    if (cnt <= 0) {
        point = new GLatLng(result.CenterPoint.Latitude, result.CenterPoint.Longitude);
        iZoomLevel = result.ZoomLevel;
    }
    map.setZoom(result.ZoomLevel);
    map.setCenter(point);
}

// This function causes Recentering of map. 
// It finds all visible markers on map and decides center point 
// and zoom level based on these markers.
function RecenterAndZoom(bRecenter, result) {
    if (bRecenter) {
        //Check if there is any visible pushpin on map.
        var cnt = 0;
        bounds = new GLatLngBounds();
        var objIgnore = document.getElementById('chkIgnoreZero');
        var bIgnore = false;
        if (objIgnore != null) {
            bIgnore = objIgnore.checked;
        }
        bIgnore = result.IgnoreZeroLatLngs;
        for (var i = 0; i < markers.getLength(); i++) {
            var ignoremarker = false;
            if (bIgnore) {
                var point1 = markers.markers[i].getPoint();
                if ((point1.x == 0) && (point1.y == 0)) {
                    ignoremarker = true;
                }
            }
            if (!ignoremarker) {
                if (!markers.markers[i].isHidden()) {
                    bounds.extend(markers.markers[i].getPoint());
                    //Increment visible pushpin count
                    cnt++;
                }
            }

        }
        var iZoomLevel = map.getBoundsZoomLevel(bounds);
        var point = bounds.getCenter();

        if (iZoomLevel > 14) {
            iZoomLevel = 14;
        }

        if (cnt <= 0) {
            point = new GLatLng(result.CenterPoint.Latitude, result.CenterPoint.Longitude);
            iZoomLevel = result.ZoomLevel;
        }
        map.setZoom(iZoomLevel);
        map.setCenter(point);
    }
}
function endRequestHandler(sender, args) {
    GService.GetOptimizedGoogleObject(map.ObjectKey, fGetGoogleObjectOptimized);
}

function pageLoad() {
    if (!Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack())
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);
}
