﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />


Type.registerNamespace('SGis.MapToolkit');

SGis.MapToolkit.MapMode = function() {
    /// <summary>Enumeración de los modos en los que se encuetra el mapa</summary>
    throw Error.invalidOperation();
}
SGis.MapToolkit.MapMode.prototype = {
    Pan: 0,
    ZoomIn: 1,
    ZoomOut: 2,
    Custom:9
}
SGis.MapToolkit.MapMode.registerEnum("SGis.MapToolkit.MapMode", false);
//
// Enumeración de las acciones que se estan llevando a cabo
SGis.MapToolkit.MapStatus = function() {
    throw Error.invalidOperation();
}
SGis.MapToolkit.MapStatus.prototype = {
    Ready: 0,
    MapPanning: 2,          // Se está haciendo panning desde el mapa principal
    MapZoomingIn: 3         // Se está haciendo zooming in con reja
}
SGis.MapToolkit.MapStatus.registerEnum("SGis.MapToolkit.MapStatus", false);
//
// Enumeración de los iconos que se pueden mostrar en la barra de estados
SGis.MapToolkit.StatusBarIcon = function() {
    throw Error.invalidOperation();
}
SGis.MapToolkit.StatusBarIcon.prototype = {
    None: 0,
    Throbber: 1,
    Download: 2,
    Info: 3,
    Error: 4
}
SGis.MapToolkit.StatusBarIcon.registerEnum("SGis.MapToolkit.StatusBarIcon", false);


//
// Funciones Estáticas
SGis.MapToolkit._Utils = function() {

}
SGis.MapToolkit._Utils.prototype = {

    // transforma coordenadas de mundo en coordenadas de mapa    
    worldToMap: function(worldPoint, centerX, centerY, zoom, imgWidth, imgHeight) {
        var result = new Object();
        var wx = imgWidth * zoom;
        var wy = imgHeight * zoom;
        var minx = centerX - wx / 2;
        var maxx = centerX + wx / 2;
        var miny = centerY - wy / 2;
        var maxy = centerY + wy / 2;
        result.x = (worldPoint.x - minx) / zoom;
        result.y = (maxy - worldPoint.y) / zoom;
        return result;
    },

    // transforma coordenadas de mapa en coordenadas de mundo    
    mapToWorld: function(mapPoint, centerX, centerY, zoom, imgWidth, imgHeight) {
        var result = new Object();
        var wx = imgWidth * zoom;
        var wy = imgHeight * zoom;
        var minx = centerX - wx / 2;
        var maxx = centerX + wx / 2;
        var miny = centerY - wy / 2;
        var maxy = centerY + wy / 2;
        result.x = minx + mapPoint.x * zoom;
        result.y = maxy - mapPoint.y * zoom;
        return result;
    },
    // Obtiene posicion relativa al objeto
    getRelativeLocation: 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
    getEventClientLocation: 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
    getRelativeEventLocation: function(evt, obj) {
        var pos = $common.getLocation(obj);
        var loc = this.getEventClientLocation(evt);
        return new Sys.UI.Point(loc.x - pos.x, loc.y - pos.y);
    },
    // Calcula la distancia en metros entre dos puntos, dadas coordenadas geográficas
    calculateDistance: function(lat1, long1, lat2, long2) {
        //conversion to radian
        lat1 = (lat1 * 2.0 * Math.PI) / 60.0 / 360.0;
        long1 = (long1 * 2.0 * Math.PI) / 60.0 / 360.0;
        lat2 = (lat2 * 2.0 * Math.PI) / 60.0 / 360.0;
        long2 = (long2 * 2.0 * Math.PI) / 60.0 / 360.0;

        // use to different earth axis length
        var a = 6378137.0;        // Earth Major Axis (WGS84)
        var b = 6356752.3142;     // Minor Axis
        var f = (a - b) / a;        // "Flattening"
        var e = 2.0 * f - f * f;      // "Eccentricity"

        var beta = (a / Math.sqrt(1.0 - e * Math.sin(lat1) * Math.sin(lat1)));
        var cos = Math.cos(lat1);
        var x = beta * cos * Math.cos(long1);
        var y = beta * cos * Math.sin(long1);
        var z = beta * (1 - e) * Math.sin(lat1);

        beta = (a / Math.sqrt(1.0 - e * Math.sin(lat2) * Math.sin(lat2)));
        cos = Math.cos(lat2);
        x -= (beta * cos * Math.cos(long2));
        y -= (beta * cos * Math.sin(long2));
        z -= (beta * (1 - e) * Math.sin(lat2));

        return Math.sqrt((x * x) + (y * y) + (z * z));
    }
}

// Create the singleton instance of the CommonToolkitScripts
var SGisCommonScripts = SGis.MapToolkit.Utils = new SGis.MapToolkit._Utils();
var $sgutils = SGisCommonScripts;


if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();