﻿//Comportamiento para calcular distancia
function WVDistance()
{
    var oThis           = this;
    var viewer          = null;
    var behaviorType    = WVBehaviorType.DISTANCE; //tipo de comportamiento
    var enabled        = false;//indica si este comportamiento está operativo para afectar al visor
    var partialDistance = 0;
    var totalDistance   = 0;
    var lastPointPX     = null;
    var lastPointUTM    = null;
    var ltnwindow       = null;
    var layer           = null;
    var painter         = null;
    var imgSrc          = null;
    var lstCustomFinalize   = new Array();
    var lstCustomInit       = new Array();
    var color           = "#ff0000";
    
    this.getEnabled=function()
    {
        return enabled;
    }
    
    this.setEnabled=function(enable)
    {
        enabled = enable;
    }
    
    //retorna si el comportamiento debe estar siempre activo
    this.alwaysKeepEnabled=function()
    {
        return false;
    }
    
    //retorna si el comportamiento debe estar siempre activo
    this.alwaysKeepEnabled=function()
    {
        return false;
    }
    
    this.init = function (visor)
    {
        viewer          = visor;
        ltnwindow       = new LtnWinMsg(viewer);
        var id          = viewer.getBaseID() + viewer.getGUID()+"_distanceL";
        
        layer           = viewer.createCustomLayer(id);
        layer.setAutoClean(false);
        layer.getContainer().style.filter ="alpha(opacity=50)";
        layer.getContainer().style.opacity=".5";
        painter = viewer.createLayerPainter(layer);
        painter.setColor(color);
        painter.setStroke(2);
        
        partialDistance = 0;
        totalDistance   = 0;
        lastPointPX     = null;
        ltnwindow.create2FieldsWindow("Partial", "m", "Total", "m", "Distance", imgSrc);
        callCustomInit();
    }
   
    this.getBehaviorType = function()
    {
        return behaviorType;
    }
    
    this.onUpdate = function ()
    {
        if(lastPointPX == null)return;
        s = (parseInt(partialDistance*100)/100).toString();
        document.getElementById(viewer.getBaseID() + viewer.getGUID()+"divCab1").innerHTML = '<nobr>'+s.substring (0, s.indexOf('.') + 4)+" m</nobr>";
        s = (parseInt(totalDistance*100)/100).toString();
        document.getElementById(viewer.getBaseID() + viewer.getGUID()+"divCab2").innerHTML ='<nobr>'+ s.substring (0, s.indexOf('.') + 4)+" m</nobr>";
    }
    
    this.finalize = function()
    {
        lastPointPX = null;
        if(ltnwindow.enabled)
            ltnwindow.finalize();
        viewer.removeCustomLayer(layer);
        callCustomFinalize(); 
        lstCustomFinalize = new Array();
        lstCustomInit     = new Array();
    }
    this.addParameters = function(p)
    {
        if(p.length == 0)return;
        imgSrc = p[0];
        color = p[1];
    }
    this.addPoint = function(pointPX)
    {
        var oldPointPX  = lastPointPX;
        var oldPointUTM = lastPointUTM;
        lastPointPX     = pointPX;
        lastPointUTM    = viewer.transformLocalToWorld(pointPX.x,pointPX.y);
        lastPointUTM.x /= 1000; 
        lastPointUTM.y /= 1000; 
        if(oldPointPX == null) return;
        partialDistance = Math.sqrt( Math.pow( lastPointUTM.x - oldPointUTM.x, 2 ) + Math.pow( lastPointUTM.y - oldPointUTM.y, 2 ) );
        totalDistance += partialDistance;
        painter.drawLine(oldPointPX.x, oldPointPX.y, lastPointPX.x, lastPointPX.y); 
        painter.paint();
    }
    //Suscribe los metodos a invocar cuando se finalice el comportamiento
    this.registerCustomFinalize = function(cfinalize)
    {
        lstCustomFinalize.push(cfinalize);
    }
    
    //Elimina los metodos a invocar cuando se finalice el comportamiento
    this.unregisterCustomFinalize = function (cfinalize)
    {
        for (var i=0;i<lstCustomFinalize.length;i++)
            if (lstCustomFinalize[i]==cfinalize)
            {
                lstCustomFinalize.splice(i,1);
                break;
            }
    }
    //Invocacion cuando se finalice el comportamiento de los metodos agregados  
    function callCustomFinalize()
	{
        var n   = lstCustomFinalize.length;
        if ( n == 0 )return;
        for( var i = 0; i < n; i++ )
            lstCustomFinalize[i]();
	}
	
	//Suscribe los metodos a invocar cuando inicie el comportamiento
    this.registerCustomInit = function(cinit)
    {
        lstCustomInit.push(cinit);
    }
    
    //Elimina los metodos a invocar cuando inicie el comportamiento
    this.unregisterCustomInit = function (cinit)
    {
        for (var i=0;i<lstCustomInit.length;i++)
            if (lstCustomInit[i]==cinit)
            {
                lstCustomInit.splice(i,1);
                break;
            }
    }
    //Invocacion cuando inicie el comportamiento de los metodos agregados  
    function callCustomInit()
	{
        var n   = lstCustomInit.length;
        if ( n == 0 )return;
        for( var i = 0; i < n; i++ )
            lstCustomInit[i]();
	}
}

