﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />



Type.registerNamespace("SGis.MapToolkit");

SGis.MapToolkit.MapControl = function(element) {
    SGis.MapToolkit.MapControl.initializeBase(this, [element]);
    // Propiedades
    this._Zoom = 1;                         // Zoom 
    this._CenterX = 0;                      // Coordenada X del Centro
    this._CenterY = 0;                      // Coordenada Y del Centro
    this._MarginTop = 0;                    // Margen superior para contenedor de mapa
    this._MarginBottom = 0;                 // Margen inferior para contenedor de mapa
    this._MarginRight = 0;                  // Margen derecho para contenedor de mapa
    this._MarginLeft = 0;                   // Margen izquierdo para contenedor de mapa
    this._ZoomLevels = [];                  // Niveles de zoom permitidos (si está vacío, permite todos)
    // Controles
    this._mapContainer = null;              // Contenedor de mapas
    this._mapFence = null;                  // representa a una layer utilizada como caja para propositos generales (zoom por ejemplo)
    this._mapFenceBackground = null;
    this._mapFenceBorder = null;
    // Fields
    this._MapMode = SGis.MapToolkit.MapMode.Pan;    // Por defecto modo paneo
    this._CustomModeContext = null;         // utilizado cuando el modo es custom, para identificar distintas herramientas
    this._MapStatus = SGis.MapToolkit.MapStatus.Ready;
    this._isLoaded = false;                 // Bandera que indica si el control fue cargado
    this._startMapDrag = null;              // Se define cuando se está haciendo drag
    this._endMapDrag = null;                // Se define mientras se hace el drag
    this._wheelEnabled = true;              // Deshabilita rueda por un instante
    this._ctrSize = null;                   // Tamaño interior del control contenedor de mapa
    this._zoomHistoryBak = new Array();     // Cola para mostrar los zooms anteriores
    this._zoomHistoryFor = new Array();     // Cola con los zooms fordward
    this._zoomHistoryEnabled = true;        // Bandera para no habilitar el zoom en caso de que este provenga de un undo o redo
    // Eventos
    this._mapContainer$delegates = {
        mousedown: Function.createDelegate(this, this._mapContainer_onMouseDownHandler),
        mouseup: Function.createDelegate(this, this._mapContainer_onMouseUpHandler),
        mousemove: Function.createDelegate(this, this._mapContainer_onMouseMoveHandler),
        // For IE and Opera
        mousewheel: Function.createDelegate(this, this._mapContainer_onMouseWheelHandler),
        // For Firefox
        DOMMouseScroll: Function.createDelegate(this, this._mapContainer_onMouseWheelHandler)
    }
}

SGis.MapToolkit.MapControl.prototype = {
    initialize: function() {
        SGis.MapToolkit.MapControl.callBaseMethod(this, 'initialize');
        // Crea Controles
        this.buildAllControls();
    },
    dispose: function() {
        //Add custom dispose actions here
        SGis.MapToolkit.MapControl.callBaseMethod(this, 'dispose');
    },
    load: function() {
        if (this._isLoaded) return;
        this._isLoaded = true;
        // Build menu based on extenders
        var elt = this.get_element();
        for (var i = 0; i < elt._behaviors.length; i++) {
            var ext = elt._behaviors[i];
            if (Object.getType(ext).implementsInterface(SGis.MapToolkit.IPlugIn)) {
                Sys.Debug.trace(String.format("Loading PlugIn '{0}'", ext.get_PlugInId()));
                ext.load(this);
                Sys.Debug.trace(String.format("PlugIn '{0}' Loaded", ext.get_PlugInId()));
            }
        }
        this.stackBakZoom();
    },
    // Construye todos los controles hijo
    buildAllControls: function() {
        this.buildMapContainer();
        this.buildMapFence();
    },
    // Construye Contenedor de mapas
    buildMapContainer: function() {
        this.get_element().style.position = "relative";
        this._mapContainer = $common.createElementFromTemplate(
            {
                nodeName: "div",
                cssClasses: ["sgmap_mapcontrol_container"],
                properties: {
                    id: "MapContainer"
                },
                events: this._mapContainer$delegates
            }, this.get_element());
        this.resizeMapContainer();
    },
    // Construye una reja para propositos generales
    buildMapFence: function() {
        this._mapFence = $common.createElementFromTemplate({
            nodeName: "div",
            properties: {
                id: "MapFence"
            },
            cssClasses: ["sgmap_mapcontrol_mapfence_container"],
            visible: false
        }, this._mapContainer);
        this._mapFenceBackground = $common.createElementFromTemplate({
            nodeName: "div",
            cssClasses: ["sgmap_mapcontrol_mapfence_background"],
            opacity: .2
        }, this._mapFence);
        this._mapFenceBorder = $common.createElementFromTemplate({
            nodeName: "div",
            cssClasses: ["sgmap_mapcontrol_mapfence_border"]
        }, this._mapFence);
    },
    resizeMapContainer: function() {
        // Reestablece el tamaño del control contenedor de mapa
        var s = $common.getSize(this.get_element());
        $common.setSize(this._mapContainer, { width: s.width - this._MarginLeft - this._MarginRight, height: s.height - this._MarginTop - this._MarginBottom });
        this._ctrSize = $common.getContentSize(this._mapContainer);
    },
    // Construye una reja para propositos generales
    buildMapFence: function() {
        this._mapFence = $common.createElementFromTemplate({
            nodeName: "div",
            properties: {
                id: "MapFence"
            },
            cssClasses: ["sgmap_mapcontrol_mapfence_container"],
            visible: false
        }, this._mapContainer);
        this._mapFenceBackground = $common.createElementFromTemplate({
            nodeName: "div",
            cssClasses: ["sgmap_mapcontrol_mapfence_background"],
            opacity: .2
        }, this._mapFence);
        this._mapFenceBorder = $common.createElementFromTemplate({
            nodeName: "div",
            cssClasses: ["sgmap_mapcontrol_mapfence_border"]
        }, this._mapFence);
    },
    // Obtiene posicion relativa al objeto
    getRelativePosition: function(x, y, obj) {
        var position = $common.getLocation(obj);
        return new Sys.UI.Point(x - position.x, y - position.y);
    },
    // Obtiene la ubicacion de un evento del mouse en coordenadas de cliente
    getClientLocation: function(evt) {
        var pos = $common.getLocation(evt.target);
        return new Sys.UI.Point(pos.x + evt.offsetX, pos.y + evt.offsetY);

    },
    // Obtiene la ubicacion relativa de un evento de mouse con respecto a un elemento
    getRelativeLocation: function(evt, obj) {
        var pos = $common.getLocation(obj);
        var loc = this.getClientLocation(evt);
        return new Sys.UI.Point(loc.x - pos.x, loc.y - pos.y);
    },
    // Transforma coordenadas de mundo a coordenadas de mapa (pantalla
    worldToMap: function(worldPoint) {
        return $sgutils.worldToMap(worldPoint, this._CenterX, this._CenterY, this._Zoom, this._ctrSize.width, this._ctrSize.height);
    },
    offsetCenter: function(deltaX, deltaY) {
        this._CenterX = this._CenterX + deltaX;
        this._CenterY = this._CenterY + deltaY;
        this.raiseMapExtentChanged();
    },
    // Cambia la extensión dado el centro y el zoom
    changeExtentToCenter: function(newCenterX, newCenterY, newZoom) {
        this._CenterX = newCenterX;
        this._CenterY = newCenterY;
        this._Zoom = this.filterZoomLevel(newZoom);
        this.raiseMapExtentChanged();
    },
    // Cambia el zoom 
    changeZoom: function(newZoom) {
        this._Zoom = this.filterZoomLevel(newZoom);
        this.raiseMapExtentChanged();
    },
    // Cambia la extensión dado un box
    changeExtentToBox: function(minX, maxX, minY, maxY) {
        var centerX = (maxX + minX) / 2;
        var centerY = (maxY + minY) / 2;
        var mapSize = $common.getContentSize(this._mapContainer);
        var zoom = Math.max(Math.abs((maxX - minX)) / mapSize.width, Math.abs(maxY - minY) / mapSize.height);
        this._CenterX = centerX;
        this._CenterY = centerY;
        this._Zoom = this.filterZoomLevel(zoom);
        this.raiseMapExtentChanged();
    },
    // Establece el modo en Pan
    setModePan: function() {
        this._MapMode = SGis.MapToolkit.MapMode.Pan;
        return true;
    },
    // Establece el modo en zoom In
    setModeZoomIn: function() {
        this._MapMode = SGis.MapToolkit.MapMode.ZoomIn;
        return true;
    },
    // Establece el modo en zoom Out
    setModeZoomOut: function() {
        this._MapMode = SGis.MapToolkit.MapMode.ZoomOut;
        return true;
    },
    // Establece el modo en Custom
    setModeCustom: function(context) {
        this._MapMode = SGis.MapToolkit.MapMode.Custom;
        if (context) {
            this._CustomModeContext = context;
        } else {
            this._CustomModeContext = '';
        }
        return true;
    },
    // Acondiciona el nivel de zoom, si es que existe una lista que lo restringe
    filterZoomLevel: function(zoom) {

        if (this._ZoomLevels && this._ZoomLevels.length > 0) {
            int = 0;
            if (zoom > this._ZoomLevels[0]) {
                return this._ZoomLevels[0];
            }
            for (i = 0; i < this._ZoomLevels.length - 1; i++) {
                var a = this._ZoomLevels[i];
                var b = this._ZoomLevels[i + 1];
                if (zoom <= a && zoom >= b) {
                    if (zoom < (a + b) / 2) {
                        return b; // Menor zoom
                    } else {
                        return a; // Zoom mayor
                    }
                }
            }
            return this._ZoomLevels[this._ZoomLevels.length - 1]; // si es menor al mínimo, retorno el mínimo
        } else {
            return zoom;
        }

    },

    // Vuelve al zoom anterior
    undoZoom: function() {
        //debugger;
        if (this._zoomHistoryBak.length > 1) {
            this.stackForZoom();
            this._zoomHistoryEnabled = false;
            var o = Array.dequeue(this._zoomHistoryBak);
            var z = this._zoomHistoryBak[0];
            this.changeExtentToCenter(z.CenterX, z.CenterY, z.Zoom);
            this._zoomHistoryEnabled = true;
        }
    },

    // Rehace el zoom 
    redoZoom: function() {
        if (this._zoomHistoryFor.length > 0) {
            this._zoomHistoryEnabled = false;
            var z = Array.dequeue(this._zoomHistoryFor);
            this.changeExtentToCenter(z.CenterX, z.CenterY, z.Zoom);
            this._zoomHistoryEnabled = true;
            Array.insert(this._zoomHistoryBak, 0, z);
        }
    },

    // incorpora el zoom en la pila para backup
    stackBakZoom: function() {
        if (this._zoomHistoryEnabled) {
            var obj = new Object();
            obj.CenterX = this._CenterX;
            obj.CenterY = this._CenterY;
            obj.Zoom = this._Zoom;
            //
            Array.insert(this._zoomHistoryBak, 0, obj);
            if (this._zoomHistoryBak.length > 20) {
                Array.removeAt(this._zoomHistoryBak, this._zoomHistoryBak - 1);
            }
            // Borra el zoom Siguiente
            Array.clear(this._zoomHistoryFor);
        }
    },
    // incorpora el zoom en la pila redo
    stackForZoom: function() {
        if (this._zoomHistoryEnabled) {
            var obj = new Object();
            obj.CenterX = this._CenterX;
            obj.CenterY = this._CenterY;
            obj.Zoom = this._Zoom;
            //
            Array.insert(this._zoomHistoryFor, 0, obj);
            if (this._zoomHistoryFor.length > 20) {
                Array.removeAt(this._zoomHistoryFor, this._zoomHistoryFor - 1);
            }
        }
    },


    //--------------------------------------------------------------------------
    // Acceso a Propiedades
    //--------------------------------------------------------------------------
    get_Zoom: function() {
        return this._Zoom;
    },
    set_Zoom: function(value) {
        if (this._Zoom != value) {
            this._Zoom = this.filterZoomLevel(value);
            this.raisePropertyChanged('Zoom');
        }
    },
    get_CenterX: function() {
        return this._CenterX;
    },
    set_CenterX: function(value) {
        if (this._CenterX != value) {
            this._CenterX = value;
            this.raisePropertyChanged('CenterX');
        }
    },
    get_CenterY: function() {
        return this._CenterY;
    },
    set_CenterY: function(value) {
        if (this._CenterY != value) {
            this._CenterY = value;
            this.raisePropertyChanged('CenterY');
        }
    },
    get_MarginTop: function() {
        return this._MarginTop;
    },
    set_MarginTop: function(value) {
        if (this._MarginTop != value) {
            this._MarginTop = value;
            this.raisePropertyChanged('MarginTop');
        }
    },
    get_MarginBottom: function() {
        return this._MarginBottom;
    },
    set_MarginBottom: function(value) {
        if (this._MarginBottom != value) {
            this._MarginBottom = value;
            this.raisePropertyChanged('MarginBottom');
        }
    },
    get_MarginLeft: function() {
        return this._MarginLeft;
    },
    set_MarginLeft: function(value) {
        if (this._MarginLeft != value) {
            this._MarginLeft = value;
            this.raisePropertyChanged('MarginLeft');
        }
    },
    get_MarginRight: function() {
        return this._MarginRight;
    },
    set_MarginRight: function(value) {
        if (this._MarginRight != value) {
            this._MarginRight = value;
            this.raisePropertyChanged('MarginRight');
        }
    },

    get_ZoomLevels: function() {
        return this._ZoomLevels;
    },
    set_ZoomLevels: function(value) {
        if (this._ZoomLevels != value) {
            this._ZoomLevels = value;
            this._Zoom = this.filterZoomLevel(this._Zoom);
            this.raisePropertyChanged('ZoomLevels');
        }
    },
    //--------------------------------------------------------------------------
    // EVENTS
    //--------------------------------------------------------------------------
    // PanningStart
    add_panningStart: function(handler) {
        this.get_events().addHandler("panningStart", handler);
    },
    remove_panningStart: function(handler) {
        this.get_events().removeHandler("panningStart", handler);
    },
    raisePanningStart: function(event) {
        var eh = this.get_events().getHandler("panningStart");
        if (eh) {
            eh(this, event);
        }
    },
    // PanningMove
    add_panningMove: function(handler) {
        this.get_events().addHandler("panningMove", handler);
    },
    remove_panningMove: function(handler) {
        this.get_events().removeHandler("panningMove", handler);
    },
    raisePanningMove: function(event) {
        var eh = this.get_events().getHandler("panningMove");
        if (eh) {
            eh(this, event);
        }
    },
    // PanningEnd
    add_panningEnd: function(handler) {
        this.get_events().addHandler("panningEnd", handler);
    },
    remove_panningEnd: function(handler) {
        this.get_events().removeHandler("panningEnd", handler);
    },
    raisePanningEnd: function(event) {
        var eh = this.get_events().getHandler("panningEnd");
        if (eh) {
            eh(this, event);
        }
    },
    // ZoomInStart
    add_zoomInStart: function(handler) {
        this.get_events().addHandler("zoomInStart", handler);
    },
    remove_zoomInStart: function(handler) {
        this.get_events().removeHandler("zoomInStart", handler);
    },
    raiseZoomInStart: function(event) {
        var eh = this.get_events().getHandler("zoomInStart");
        if (eh) {
            eh(this, event);
        }
    },
    // ZoomInMove
    add_zoomInMove: function(handler) {
        this.get_events().addHandler("zoomInMove", handler);
    },
    remove_zoomInMove: function(handler) {
        this.get_events().removeHandler("zoomInMove", handler);
    },
    raiseZoomInMove: function(event) {
        var eh = this.get_events().getHandler("zoomInMove");
        if (eh) {
            eh(this, event);
        }
    },
    // ZoomInEnd
    add_zoomInEnd: function(handler) {
        this.get_events().addHandler("zoomInEnd", handler);
    },
    remove_zoomInEnd: function(handler) {
        this.get_events().removeHandler("zoomInEnd", handler);
    },
    raiseZoomInEnd: function(event) {
        var eh = this.get_events().getHandler("zoomInEnd");
        if (eh) {
            eh(this, event);
        }
    },
    // MapExtentChanged
    add_mapExtentChanged: function(handler) {
        this.get_events().addHandler("mapExtentChanged", handler);
    },
    remove_mapExtentChanged: function(handler) {
        this.get_events().removeHandler("mapExtentChanged", handler);
    },
    raiseMapExtentChanged: function(event) {
        this.stackBakZoom();
        var eh = this.get_events().getHandler("mapExtentChanged");
        if (eh) {
            eh(this, event);
        }
    },

    // CustomToolMoseDonwn
    add_customToolMouseDown: function(handler) {
        this.get_events().addHandler("customToolMouseDown", handler);
    },
    remove_customToolMouseDown: function(handler) {
        this.get_events().removeHandler("customToolMouseDown", handler);
    },
    raiseCustomToolMouseDown: function(event) {
        var eh = this.get_events().getHandler("customToolMouseDown");
        if (eh) {
            event.Context = this._CustomModeContext;
            eh(this, event);
        }
    },

    //--------------------------------------------------------------------------
    // EVENT HANDLERS
    //--------------------------------------------------------------------------
    // Click sobre el mapa, para selección, drag, etc 
    _mapContainer_onMouseDownHandler: function(evt) {
        if (this._MapStatus == SGis.MapToolkit.MapStatus.Ready) {
            // Cambia el comportamiento en función de la tecla shift o ctrl presionada
            var mode = this._MapMode;
            if (mode == SGis.MapToolkit.MapMode.Pan) {
                // Herramienta paneo seleccionada
                if (evt.shiftKey) {
                    // Shift + Pan = ZoomIn
                    mode = SGis.MapToolkit.MapMode.ZoomIn;
                } else if (evt.ctrlKey) {
                    // Ctrl + Pan = ZoomOut
                    mode = SGis.MapToolkit.MapMode.ZoomOut;
                }
            } else if (mode == SGis.MapToolkit.MapMode.ZoomIn) {
                // herramienta ZoomIn seleccionada
                if (evt.shiftKey) {
                    // Shift+ ZoomIn = paneo
                    mode = SGis.MapToolkit.MapMode.Pan;
                } else if (evt.ctrlKey) {
                    // Ctrl + ZoomIn = ZoomOut
                    mode = SGis.MapToolkit.MapMode.ZoomOut;
                }
            } else if (mode == SGis.MapToolkit.MapMode.ZoomOut) {
                // herramienta ZoomOut seleccionada
                if (evt.shiftKey) {
                    // Shift+ ZoomOut = Paneo
                    mode = SGis.MapToolkit.MapMode.Pan;
                } else if (evt.ctrlKey) {
                    // Ctrl + ZoomOut = ZoomIn
                    mode = SGis.MapToolkit.MapMode.ZoomIn;
                }
            }


            switch (mode) {
                case SGis.MapToolkit.MapMode.Pan:
                    this._MapStatus = SGis.MapToolkit.MapStatus.MapPanning;
                    this._startMapDrag = this.getRelativePosition(evt.clientX, evt.clientY, this._mapContainer);
                    this._endMapDrag = null;
                    evt.startPos = this._startMapDrag;
                    this.raisePanningStart(evt);
                    break;
                case SGis.MapToolkit.MapMode.ZoomIn:
                    this._MapStatus = SGis.MapToolkit.MapStatus.MapZoomingIn;
                    this._startMapDrag = this.getRelativeLocation(evt, this._mapContainer);
                    var f = this._mapFence;
                    $common.setSize(f, { width: 0, height: 0 });
                    $common.setLocation(f, { x: this._startMapDrag.x, y: this._startMapDrag.y });
                    this._mapFenceBorder.style.width = "0px";
                    this._mapFenceBorder.style.height = "0px";
                    this._mapFenceBackground.style.width = "0px";
                    this._mapFenceBackground.style.height = "0px";
                    $common.setVisible(f, true);
                    evt.startPos = this._startMapDrag;
                    this.raiseZoomInStart(evt);
                    break;
                case SGis.MapToolkit.MapMode.ZoomOut:
                    var pos = this.getRelativeLocation(evt, this._mapContainer);
                    var mapSize = $common.getContentSize(this._mapContainer);
                    var relZoom = 2;
                    var deltaX = mapSize.width / 2 - pos.x;
                    var deltaY = mapSize.height / 2 - pos.y;
                    this.changeExtentToCenter(
                        this._CenterX - (this._Zoom * deltaX),
                        this._CenterY + (this._Zoom * deltaY),
                        this._Zoom * relZoom);
                    //
                    break;
                case SGis.MapToolkit.MapMode.Custom:
                    var pos = this.getRelativeLocation(evt, this._mapContainer);
                    var mapPos = $sgutils.mapToWorld(
                        { x: pos.x, y: pos.y },
                        this._CenterX,
                        this._CenterY,
                        this._Zoom,
                        this._ctrSize.width,
                        this._ctrSize.height);
                    evt.MapPosition = mapPos; // Carga posicion en coordenadas de mapa
                    this.raiseCustomToolMouseDown(evt);
                    break;
                default:
                    return;
            }
            evt.preventDefault();
        }
    },


    // Suelta el mouse sobre el mapa, fin de drag, zoom, etc 
    _mapContainer_onMouseUpHandler: function(evt) {

        switch (this._MapStatus) {
            case (SGis.MapToolkit.MapStatus.MapPanning):
                this._MapStatus = SGis.MapToolkit.MapStatus.Ready;
                this._endMapDrag = this.getRelativePosition(evt.clientX, evt.clientY, this._mapContainer);
                evt.startPos = this._startMapDrag;
                evt.endPos = this._endMapDrag;
                evt.dx = this._endMapDrag.x - this._startMapDrag.x;
                evt.dy = this._endMapDrag.y - this._startMapDrag.y;
                // Si no hay desplazamiento, no se disparan los eventos
                if (evt.dy != 0 || evt.dx != 0) {
                    this._CenterX = this._CenterX - (this._Zoom * evt.dx);
                    this._CenterY = this._CenterY + (this._Zoom * evt.dy);
                    this.raisePanningEnd(evt);
                    this.raiseMapExtentChanged();
                }
                break;
            case SGis.MapToolkit.MapStatus.MapZoomingIn:
                this._MapStatus = SGis.MapToolkit.MapStatus.Ready;
                // Determina Desplazamiento y Zoom
                var rec = $common.getBounds(this._mapFence);
                var pos = this.getRelativePosition(rec.x, rec.y, this._mapContainer);
                var mapSize = $common.getContentSize(this._mapContainer);
                if (rec.width < 3 || rec.height < 3) {
                    evt.relZoom = .5;
                } else {
                    evt.relZoom = Math.max(rec.width / mapSize.width, rec.height / mapSize.height);
                }
                if (evt.relZoom < 0.05) {
                    evt.relZoom = 0.05;
                }
                evt.dx = (mapSize.width / 2 - (pos.x + rec.width / 2));
                evt.dy = (mapSize.height / 2 - (pos.y + rec.height / 2));
                $common.setVisible(this._mapFence, false);
                // Establece nuevas propiedades
                this._CenterX = this._CenterX - (this._Zoom * evt.dx);
                this._CenterY = this._CenterY + (this._Zoom * evt.dy);
                this._Zoom = this.filterZoomLevel(this._Zoom * evt.relZoom);
                //
                this.raiseMapExtentChanged();
                this.raiseZoomInEnd(evt);
                break;
            default:
                return;
        }
        evt.preventDefault();
    },
    // Mueve el mose sobre el Mapa
    _mapContainer_onMouseMoveHandler: function(evt) {
        switch (this._MapStatus) {
            case (SGis.MapToolkit.MapStatus.MapPanning):
                this._endMapDrag = this.getRelativePosition(evt.clientX, evt.clientY, this._mapContainer);
                evt.startPos = this._startMapDrag;
                evt.endPos = this._endMapDrag;
                evt.dx = this._endMapDrag.x - this._startMapDrag.x;
                evt.dy = this._endMapDrag.y - this._startMapDrag.y;
                this.raisePanningMove(evt);
                break;
            case SGis.MapToolkit.MapStatus.MapZoomingIn:
                this._endMapDrag = this.getRelativePosition(evt.clientX, evt.clientY, this._mapContainer);
                var w = this._endMapDrag.x - this._startMapDrag.x;
                var h = this._endMapDrag.y - this._startMapDrag.y;
                if (w < 2) w = 2;
                if (h < 2) h = 2;
                this._mapFence.style.width = w + "px";
                this._mapFence.style.height = h + "px";
                this._mapFenceBorder.style.width = (w - 2) + "px";
                this._mapFenceBorder.style.height = (h - 2) + "px";
                this._mapFenceBackground.style.width = (w - 1) + "px";
                this._mapFenceBackground.style.height = (h - 1) + "px";
                //$common.setSize(this._mapFence, { width: w, height: h });
                //$common.setSize(this._mapFenceBorder, { width: w, height: h });
                //$common.setSize(this._mapFenceBorder, { width: (Math.max(this._endMapDrag.x - this._startMapDrag.x, 0)), height: (Math.max(this._endMapDrag.y - this._startMapDrag.y, 0)) });
                evt.startPos = this._startMapDrag;
                evt.endPos = this._endMapDrag;
                this.raiseZoomInMove(evt);
                break;
            default:
                return;
        }
        evt.preventDefault();
    },
    habilitaWheel: function() {
        this._wheelEnabled = true;
    },
    // al accionar la rueda del mouse
    _mapContainer_onMouseWheelHandler: function(evt) {
        if (this._MapStatus == SGis.MapToolkit.MapStatus.Ready && this._wheelEnabled) {
            window.setTimeout(Function.createDelegate(this, this.habilitaWheel), 500);
            this._wheelEnabled = false;
            var delta; // variable temporal delta generada dependiendo del navegador
            var rawEvent = evt.rawEvent;
            var mapSize = $common.getContentSize(this._mapContainer);
            if (rawEvent.wheelDelta) {
                delta = rawEvent.wheelDelta / 120;
                if (Sys.Browser.agent === Sys.Browser.Opera) delta = -delta;
            }
            else if (rawEvent.detail) {
                delta = -rawEvent.detail / 3;
            }
            evt.delta = delta;
            // Zoom en la zona donde se realiza el mousewheel
            //var relZoom = (evt.delta > 0) ? .5 / evt.delta : -2 * evt.delta;
            var relZoom = (evt.delta > 0) ? .5 : 2;
            var deltaX = 0;
            var deltaY = 0;
            if (Sys.Browser.agent == Sys.Browser.InternetExplorer) {
                var pos = this.getRelativeLocation(evt, this._mapContainer);
                var deltaX = mapSize.width / 2 - pos.x;
                var deltaY = mapSize.height / 2 - pos.y;
            }
            // Establece nuevas propiedades
            this._CenterX = this._CenterX - (this._Zoom * deltaX);
            this._CenterY = this._CenterY + (this._Zoom * deltaY);
            this._Zoom = this.filterZoomLevel(this._Zoom * relZoom);
            //
            this.raiseMapExtentChanged();
            evt.preventDefault();
        }
    }
}
SGis.MapToolkit.MapControl.registerClass('SGis.MapToolkit.MapControl', AjaxControlToolkit.ControlBase);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();