﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("AirQuestSite");

AirQuestSite.RouteViewScriptController = function(element) {
    AirQuestSite.RouteViewScriptController.initializeBase(this, [element]);
    this._cmps = {};

}

AirQuestSite.RouteViewScriptController.prototype = {
    _cmps: null,
    _ctls: null,
    _route_key: '',

    onCmdAeroChanged$handler: null,
    onClickMarker$handler: null,
    onPreCreateMarker$handler: null,
    markerService: null,

    initialize: function () {

        AirQuestSite.RouteViewScriptController.callBaseMethod(this, 'initialize');

        this._ctls = {
            markerManager: $find(this._cmps.markerManager),
            map: $find(this._cmps.mapCtl),
            scbPoints: $find(this._cmps.scbPoints)
        };

        this.onClickMarker$handler = Function.createDelegate(this, this.onClickMarker);
        this._ctls.markerManager.add_clickMarker(this.onClickMarker$handler);

        this.onPreCreateMarker$handler = Function.createDelegate(this, this.onPreCreateMarker);
        this._ctls.markerManager.add_preCreateMarker(this.onPreCreateMarker$handler);

        this.markerService = this._ctls.markerManager.get_markerService();

    },


    onClickMarker: function (sender, args) {
        var marker = args._marker;

        if (marker.cls == "FOTO" || marker.cls == "QP") {
            args._cancel = true;

            this._ctls.scbPoints.doPostBack("click|" + marker.cls + "|" + marker.id);
        }
    },

    onPreCreateMarker: function (sender, args) {
        var markerData = args._marker;
    },

    externalClickPoint: function (id, lat, lon) {
        var theMap = this._ctls.map.get_map();
        theMap.panTo(new GLatLng(lat, lon));
    },

    drawRoute: function (pts) {

        var action = Function.createDelegate(this, function (theMap) {
            if (this._route_line) {
                theMap.removeOverlay(this._route_line);
                this._route_line = null;
            }

            if (pts) {
                this._route_line = GPolyline.fromEncoded({ points: pts[0], numLevels: 4, zoomFactor: 32, levels: pts[1], color: '#ff00ff', weight: 5, opacity: 0.8 });
                Sys.Debug.trace(pts[0]);
                Sys.Debug.trace(pts[1]);
                theMap.addOverlay(this._route_line);
            }
        });

        this._ctls.map.postMapAction(action);


    },

    drawTrack: function (pts, mode) {
        var action = Function.createDelegate(this, function (theMap) {
            if (pts) {
                var trackLine = null;

                switch (mode) {
                    case 0:
                        trackLine = GPolyline.fromEncoded({ points: pts[0], numLevels: 4, zoomFactor: 32, levels: pts[1], color: '#660000', weight: 3, opacity: 0.7 });
                        break;
                    case 1:
                        trackLine = GPolyline.fromEncoded({ points: pts[0], numLevels: 4, zoomFactor: 32, levels: pts[1], color: '#ff0000', weight: 3, opacity: 0.5 });
                        break;
                }
                theMap.addOverlay(trackLine);
            }
        });

        this._ctls.map.postMapAction(action);
    },


    zoomRoute: function () {
        var action = Function.createDelegate(this, function (theMap) {
            if (this._route_line) {
                var bnds = this._route_line.getBounds();
                var zoom = theMap.getBoundsZoomLevel(bnds);
                theMap.setCenter(bnds.getCenter(), zoom);
            }
        });
        this._ctls.map.postMapAction(action);

    },


    zoomFeature: function (pt) {
        var action = Function.createDelegate(this, function (theMap) {
            theMap.setCenter(new GLatLng(pt[0], pt[1]), 12);
        });
        this._ctls.map.postMapAction(action);
    },

    dispose: function () {
        this._ctls.markerManager.remove_preCreateMarker(this.onPreCreateMarker$handler);
        this._ctls.markerManager.remove_clickMarker(this.onClickMarker$handler);

        AirQuestSite.RouteViewScriptController.callBaseMethod(this, 'dispose');
    }
}
AirQuestSite.RouteViewScriptController.registerClass('AirQuestSite.RouteViewScriptController', Sys.UI.Control);

if(typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

