﻿function RadiansToDegrees (rad)
{
/// <remarks>VeHttpHandler</remarks>
    return (rad / Math.PI * 180.0);
}
function DegreesToRadians (deg)
{
/// <remarks>VeHttpHandler</remarks>
    return (deg * Math.PI / 180.0);
}

function LongLatFromCoordinate (coordinate, worldRect)
{
/// <summary>
/// Extent maps to +-180 for Longitude and +-85 for Latitude
/// </summary>
/// <remarks>SLVirtualEarthViewer</remarks>
    var x = ((coordinate.X - worldRect.Left) * 360.0 / worldRect.Width) - 180.0;
    var y = RadiansToDegrees (2 * Math.atan(Math.exp(Math.PI * (1.0 - 2.0 * (coordinate.Y - worldRect.Top) / worldRect.Height))) - Math.PI * 0.5);
    var point = eval('({"X":'+x+', "Y":'+y+'})');
    return point;
}

function CoordinateFromLongLat (longLat, worldRect)
{
/// <remarks>SLVirtualEarthViewer</remarks>
    var coordinate = eval('({"X":0, "Y":0})');
    coordinate.X = (worldRect.Left * 1) + (longLat.X + 180.0) * worldRect.Width / 360.0;
    coordinate.Y = (worldRect.Top * 1) + (worldRect.Width * 0.5) * (1 - (Math.log(Math.tan(DegreesToRadians(longLat.Y) * 0.5 + Math.PI * 0.25))) / Math.PI);
    return coordinate;
}


/* grid represents the 'viewport' - "t_0_0".."t_2_1" are the names of the Xaml Image elements */
var grid = new Array(3);
for (i = 0; i < 3; i++)
{
    grid[i]=new Array(2);
}

function QuadkeyToBase4 (quadKey)
{
/// <summary>
/// Convert Google alpha representation [qrst] to [0123] Base4 number
/// If input is already a number, the 'replace' in this function has no effect
/// </summary>
    s = new String(quadKey);
    s = s.replace(/q/g, '0');
    s = s.replace(/r/g, '1');
    s = s.replace(/t/g, '2');
    s = s.replace(/s/g, '3');
    return s;
}
function Base4ToQuadkey (base4)
{
/// <summary>
/// Convert Base4 number to Google alpha representation [0123] to [qrst]
/// </summary>
    s = new String(base4);
    if ((GLOBE == 'moon') || (GLOBE == 'mars') || (GLOBE == 'earthg')) 
    {
        s = s.replace(/0/g, 'q');
        s = s.replace(/1/g, 'r');
        s = s.replace(/2/g, 't');
        s = s.replace(/3/g, 's');
    }
    return s;
}
function Base2ToPoint (binary)
{ 
/// <summary>Convert binary number to coordinate</summary>
/// <remarks>
/// 033 = 001111 = 011,011 = 3,3  (Y, X)
/// 123 = 011011 = 011,101 = 3,4  (Y, X)
///
/// need charAt because treating string like array point.X[p] didn't work in IE7
/// </remarks>
    var x='', y='';eval('var binaryString="'+binary+'"');
    for (var p = 0; p < binaryString.length; p++)
    {
        if (p % 2 == 0)
            y = y + '' + binaryString.charAt(p);
        else
            x = x + '' + binaryString.charAt(p);
    }
    return new Point(x,y);
}

function PointToBase2 (point)
{
    var binary='';
    for (var p = 0; p < point.X.length; p++)
    { // need charAt because treating string like array point.X[p] didn't work in IE7
        binary = binary + point.X.charAt(p) + point.Y.charAt(p);
    }
    return binary;
}

