﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />

Type.registerNamespace("SGis.MapToolkit");

SGis.MapToolkit.DistanceReferenceBehavior = function(element) {
SGis.MapToolkit.DistanceReferenceBehavior.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._centralBar = null;
    this._leftBar = null;
    this._rightBar = null;
    this._txtDiv = null;
    // Propiedades
    this._MaxWidth = 150;        // Tamaño máximo del control
    // 
    // Eventos
    this._mapControl$onMapExtentChanged$delegate = Function.createDelegate(this, this._mapControl_onMapExtentChanged);

}

SGis.MapToolkit.DistanceReferenceBehavior.prototype = {
    initialize: function() {
        SGis.MapToolkit.DistanceReferenceBehavior.callBaseMethod(this, 'initialize');
        //
        this._mapContainer = this.get_element().control._mapContainer;
        this._mapControl = this.get_element();
        this._parentControl = this.get_element().control;
        // Attach event handlers
        this._parentControl.add_mapExtentChanged(this._mapControl$onMapExtentChanged$delegate);

        // Construye elementos
        this.buildControls();
    },
    dispose: function() {
        this._parentControl.remove_mapExtentChanged(this._mapControl$onMapExtentChanged$delegate);
        //Add custom dispose actions here
        SGis.MapToolkit.DistanceReferenceBehavior.callBaseMethod(this, 'dispose');
    },
    get_PlugInId: function() {
        /// <value type="string" mayBeNull="false">
        /// Gets de plugIn unique id
        /// </value>
        return 'sgis.distref';
    },
    /// <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) {
        this.buildReference();
    },
    unload: function() {
        Sys.Debug.trace('Unloading distref');
    },
    buildControls: function() {
        //
        this._thisContainer = $common.createElementFromTemplate(
        {
            nodeName: "div",
            cssClasses: ["sgmap_ref_container"]
        }, this._mapControl);
        this._centralBar = $common.createElementFromTemplate(
        {
            nodeName: "div",
            cssClasses: ["sgmap_ref_centralbar"]
        }, this._thisContainer);
        //
        this._leftBar = $common.createElementFromTemplate(
        {
            nodeName: "div",
            cssClasses: ["sgmap_ref_leftbar"]
        }, this._thisContainer);
        //
        this._rightBar = $common.createElementFromTemplate(
        {
            nodeName: "div",
            cssClasses: ["sgmap_ref_rightbar"]
        }, this._thisContainer);
        //
        this._txtDiv = $common.createElementFromTemplate(
        {
            nodeName: "div",
            cssClasses: ["sgmap_ref_txtdiv"]
        }, this._thisContainer);
    },
    buildReference: function() {
        // Determina la distancia para el maximo tamaño de referencia
        //debugger;
        var max = $sgutils.calculateDistance(this._parentControl._CenterY * 60, (this._parentControl._CenterX - this._MaxWidth / 2 * this._parentControl._Zoom) * 60, this._parentControl._CenterY * 60, (this._parentControl._CenterX + this._MaxWidth / 2 * this._parentControl._Zoom) * 60);
        //
        // Determina el numero a mostrar
        if (max > 1) {
            var l = Math.round(max).toString().length;
            var ref = Math.round(max / Math.pow(10, l - 1)) * Math.pow(10, l - 1)
            if (ref < 1) ref = 1;
            var width = this._MaxWidth * (ref / max);
            // Determina texto
            var txt = "";
            if (ref > 1000) {
                txt = String.format("{0} Km", Math.round(ref / 1000));
            } else {
                txt = String.format("{0} m", Math.round(ref));
            }
            // Cambiar tamaño y posicion de controles de controles
            this._centralBar.style.width = width + "px";
            this._rightBar.style.left = (width - 3) + "px";
            this._txtDiv.style.width = (width - 15) + "PX";
            this._txtDiv.innerHTML = txt;
            //
            $common.setVisible(this._thisContainer, true);
        } else {
            // Si es muy chico, no se muestra
            $common.setVisible(this._thisContainer, false);
        }
    },
    //--------------------------------------------------------------------------
    // Manejadores de Eventos
    //--------------------------------------------------------------------------
    // Al cambiar la extensión del mapa
    _mapControl_onMapExtentChanged: function(sender, args) {
        this.buildReference();
    }


}
SGis.MapToolkit.DistanceReferenceBehavior.registerClass('SGis.MapToolkit.DistanceReferenceBehavior', AjaxControlToolkit.BehaviorBase, SGis.MapToolkit.IPlugIn);



if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();