﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />


Type.registerNamespace("SGis.MapToolkit");


SGis.MapToolkit.StatusBarBehavior = function(element) {
    SGis.MapToolkit.StatusBarBehavior.initializeBase(this, [element]);
    // Referencia a Controles y objetos
    this._thisContainer = null;  // contenedor de este control
    this._mapControl = null;
    this._mapContainer = null;    // Referencia al contenedor de mapas
    this._parentControl = null;   // Referencia al control de mapas
    // Referencia a Controles y objetos
    this._DivIconMessage;     // Div donde se coloca mensaje y el icono
    this._DivIcon = null;     // Div donde se coloca el icono
    this._DivMessage = null;  // Div donde se coloca el mensaje
    this._DivCoord = null;    // Div donde se colocan las coordenadas
    // Miembros Privados
    this._messages = [];      // Contiene el arreglo de la información mostrada.
    this._icon = SGis.MapToolkit.StatusBarIcon.None;
    // Eventos
    this._mapControl$onMapMouseMove$delegate = Function.createDelegate(this, this._mapControl_onMapMouseMove);
    this._mapControl$onMapMouseOut$delegate = Function.createDelegate(this, this._mapControl_onMapMouseOut);

    
}

SGis.MapToolkit.StatusBarBehavior.prototype = {
    initialize: function() {
        SGis.MapToolkit.StatusBarBehavior.callBaseMethod(this, 'initialize');
        //
        this._mapContainer = this.get_element().control._mapContainer;
        this._mapControl = this.get_element();
        this._parentControl = this.get_element().control;
        // Event
        Sys.UI.DomEvent.addHandler(this._mapContainer, 'mousemove', this._mapControl$onMapMouseMove$delegate);
        Sys.UI.DomEvent.addHandler(this._mapContainer, 'mouseout', this._mapControl$onMapMouseOut$delegate);

        // Crea Control
        this.buildStatusBar();
    },
    dispose: function() {
        //Add custom dispose actions here
        SGis.MapToolkit.StatusBarBehavior.callBaseMethod(this, 'dispose');
        //
        Sys.UI.DomEvent.removeHandler(this._mapContainer, 'mousemove', this._mapControl$onMapMouseMove$delegate);
        Sys.UI.DomEvent.removeHandler(this._mapContainer, 'mouseout', this._mapControl$onMapMouseOut$delegate);

    },
    get_PlugInId: function() {
        /// <value type="string" mayBeNull="false">
        /// Gets de plugIn unique id
        /// </value>
        return 'sgis.statusbar';
    },
    /// <summary>
    // loads the current plugin into the main map container, creates menu, toolbar, atach events, etc
    /// </summary>
    /// <param name="container" type="SGis.MapToolkit.MapControl">
    /// The map control container
    /// </param>
    load: function(container) {
        if (this._isLoaded) return;
        this._isLoaded = true;
        // Establece el control como status bar
        // TODO: Manejar a traves de eventos 
        container._stsBar = this;
    },
    unload: function() {
        Sys.Debug.trace('Unloading ovmap');
    },
    // Construye elementos div para toolbar
    buildStatusBar: function() {
        this._thisContainer = $common.createElementFromTemplate(
        {
            nodeName: "div",
            cssClasses: ["sgmap_sts_container"]
        }, this._mapControl);
        this._DivIconMessage = $common.createElementFromTemplate(
            {
                nodeName: "div",
                cssClasses: ["sgmap_sts_div_iconmessage"],
                properties: {
                    id: "stsDivIconMessage"
                }
            }, this._thisContainer);
        this._DivIcon = $common.createElementFromTemplate(
            {
                nodeName: "div",
                cssClasses: ["sgmap_sts_div_icon"],
                properties: {
                    id: "stsDivIcon"
                }
            }, this._DivIconMessage);
        this._DivMessage = $common.createElementFromTemplate(
            {
                nodeName: "div",
                cssClasses: ["sgmap_sts_div_message"],
                properties: {
                    id: "stsDivMessage"
                }
            }, this._DivIconMessage);
        this._DivCoord = $common.createElementFromTemplate(
            {
                nodeName: "div",
                cssClasses: ["sgmap_sts_div_coord"],
                properties: {
                    id: "stsDivCoord",
                    innerHTML: '<span></span>'
                }
            }, this._thisContainer);

    },
    // Agrega un mensaje al statusbar base a una clave y un objeto con los datos a informar
    addMessage: function(key, info) {
        this._messages[key] = info;
        this.showMessage(info);
    },
    removeMessage: function(key, info) {
        this._messages[key] = null;
        this.showMessage();
    },
    showMessage: function(message) {
        var showMessage = true;
        if (message === null || typeof (message) === 'undefined') {
            // Busca algun otro mensaje que este contenido en la tabla
            for (var i in this._messages) {
                if (this._messages[i] !== null) {
                    message = this._messages[i];
                    break;
                }
            }
            showMessage = false;
        }
        if (showMessage) {
            // Establece mensaje
            Sys.UI.DomElement.removeCssClass(this._DivIcon, this.getIconCss(this._icon));
            this._icon = message.Icon;
            Sys.UI.DomElement.addCssClass(this._DivIcon, this.getIconCss(this._icon));
            this._DivMessage.innerHTML = message.Text;
        } else {
            // Borra mensaje
            Sys.UI.DomElement.removeCssClass(this._DivIcon, this.getIconCss(this._icon));
            this._icon = SGis.MapToolkit.StatusBarIcon.None;
            Sys.UI.DomElement.addCssClass(this._DivIcon, this.getIconCss(this._icon));
            this._DivMessage.innerHTML = '';
        }
    },
    /// Obtiene la clase dado el tipo icono
    getIconCss: function(icon) {
        switch (icon) {
            case SGis.MapToolkit.StatusBarIcon.None:
                return "sgmap_sts_icon_none";
            case SGis.MapToolkit.StatusBarIcon.Throbber:
                return "sgmap_sts_icon_throbber";
            case SGis.MapToolkit.StatusBarIcon.Download:
                return "sgmap_sts_icon_download";
            case SGis.MapToolkit.StatusBarIcon.Info:
                return "sgmap_sts_icon_info";
            case SGis.MapToolkit.StatusBarIcon.Error:
                return "sgmap_sts_icon_error";
            default:
                return "sgmap_sts_icon_none";
        }
    },
    //--------------------------------------------------------------------------
    // Manejadores de Eventos
    //--------------------------------------------------------------------------
    // Al moverse sobre el mapa
    _mapControl_onMapMouseMove: function(args) {
        var pos = this._parentControl.getRelativeLocation(args, this._mapContainer);
        //debugger;
        var mapPos = $sgutils.mapToWorld(
                        { x: pos.x, y: pos.y },
                        this._parentControl._CenterX,
                        this._parentControl._CenterY,
                        this._parentControl._Zoom,
                        this._parentControl._ctrSize.width,
                        this._parentControl._ctrSize.height);


        this._DivCoord.innerHTML = String.format('<span>Lat:<strong>{0:n6}</strong>  Lon:<strong>{1:n6}</strong> ', mapPos.y, mapPos.x)+'</span>';
    },
    // Al salirse del mapa
    _mapControl_onMapMouseOut: function(args) {
        this._DivCoord.innerHTML = '<span></span>';
    }
}
SGis.MapToolkit.StatusBarBehavior.registerClass('SGis.MapToolkit.StatusBarBehavior', AjaxControlToolkit.BehaviorBase, SGis.MapToolkit.IPlugIn);



if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();