﻿//2D Vector
function Vector2D()
{
    this.x  = 0;
    this.y  = 0;
}

//2D Bound
function Bound2D()
{
    this.x = 0;
    this.y = 0;
    this.width = 0;
    this.height = 0;
}

//Enumerado de capas de sistema del visor web
function WVLayerType(val) 
{
    this.valor = val;
}
WVLayerType.prototype.toString = function () 
{
    return this.valor;
};
WVLayerType.RASTER = new WVLayerType('RASTER');
WVLayerType.VECTOR = new WVLayerType('VECTOR');
WVLayerType.ICON   = new WVLayerType('ICON');
WVLayerType.CUSTOM = new WVLayerType('CUSTOM');

//Enumerado de comportamientos de sistema del visor web
function WVBehaviorType(val) 
{
    this.valor = val;
}
WVBehaviorType.prototype.toString = function () 
{
    return this.valor;
};
WVBehaviorType.CUSTOM           = new WVBehaviorType('CUSTOM');
WVBehaviorType.PANNING          = new WVBehaviorType('PANNING');
WVBehaviorType.ZOOM_POWER_TWO   = new WVBehaviorType('ZOOM_POWER_TWO');
WVBehaviorType.NAVIGATOR        = new WVBehaviorType('NAVIGATOR');
WVBehaviorType.HISTORY_BROWSER  = new WVBehaviorType('HISTORY_BROWSER');
WVBehaviorType.FENCE            = new WVBehaviorType('FENCE');
WVBehaviorType.DISTANCE         = new WVBehaviorType('DISTANCE');
WVBehaviorType.AREA             = new WVBehaviorType('AREA');
WVBehaviorType.FIT              = new WVBehaviorType('FIT');
WVBehaviorType.UTMCOORDS        = new WVBehaviorType('UTMCOORDS');
WVBehaviorType.CARTINF          = new WVBehaviorType('CARTINF');
WVBehaviorType.ALPHINF          = new WVBehaviorType('ALPHINF');
WVBehaviorType.PRINT            = new WVBehaviorType('PRINT');
WVBehaviorType.REFRESH          = new WVBehaviorType('REFRESH');
WVBehaviorType.EMPTY            = new WVBehaviorType('EMPTY');


//Enumerado de eventos del visor web
function WVEventType(val) 
{
    this.valor = val;
}
WVEventType.prototype.toString = function () 
{
    return this.valor;
};
WVEventType.DOUBLE_CLICK = new WVEventType('DOUBLE_CLICK');
WVEventType.LEFT_CLICK   = new WVEventType('LEFT_CLICK');
WVEventType.RIGHT_CLICK  = new WVEventType('RIGHT_CLICK');
WVEventType.MOUSE_MOVE   = new WVEventType('MOUSE_MOVE');
WVEventType.CHANGE_SCALE = new WVEventType('CHANGE_SCALE');
WVEventType.CHANGE_BEHAVIOR  = new WVEventType('CHANGE_BEHAVIOR');
WVEventType.ICON_CLICK   = new WVEventType('ICON_CLICK');
WVEventType.ICON_OVER    = new WVEventType('ICON_OVER');
WVEventType.ICON_OUT     = new WVEventType('ICON_OUT');

//Ordena lista de tiles en base a la distancia de su centro al centro del visor en coordenadas locales
function sortTiles(viewer, lstTiles)
{
    var center = {x:viewer.getWidth()>>1,y:viewer.getHeight()>>1};
    var swap;
    do //ordena tiles
    {
        var tw = viewer.getTileWidth() >> 1;
        var th = viewer.getTileHeight()>> 1;
        
        swap=false;
        var n = lstTiles.length;
        for(var i=0;i<n-1;i++)
        {
            var tile1 = lstTiles[i];
            var tile2 = lstTiles[i+1];
            var p1= {x:parseInt(tile1.style.left) + tw,y:parseInt(tile1.style.top) + th};
            var p2= {x:parseInt(tile2.style.left) + tw,y:parseInt(tile2.style.top) + th};
            
            var dx = p1.x - center.x;
            var dy = p1.y - center.y;
            var d1 = (dx*dx) + (dy*dy);
            dx = p2.x - center.x;
            dy = p2.y - center.y;
            var d2 = (dx*dx) + (dy*dy);
            
            if (d1>d2)
            {
                var tmpTile  = lstTiles[i];
                lstTiles[i]  = lstTiles[i+1];
                lstTiles[i+1]= tmpTile;
                swap=true;
            }
        }
    }
    while (swap);
    
    return lstTiles;
}

//Activa/Desactiva bordes en imagenes de Botones del visor
function IMGButtonManager(id)
{
    var border  = null;
    var img     = document.getElementById(id);
    this.setMarked = function()
    {
        if (img!=null)
        {
            border = img.border;
            img.border = "2px";
        }
    }
    this.setUnmarked = function()
    {
        if (img!=null)
        {
            img.border = border;
            border = null;
        }
    }
}

//********************* Cross-Domain AJAX JSON **************************
function JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl; 
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'YJscriptId' + JSONscriptRequest.scriptCounter++;
}

//datos del usuario
JSONscriptRequest.tag = null;

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
JSONscriptRequest.prototype.buildScriptTag = function () {

    // Create the script tag
    this.scriptObj = document.createElement("script");
    
    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
// removeScriptTag method
JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);  
}

// addScriptTag method
JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
}
//************************************************************************