﻿// JScript File


function removeDynamicService(name)
{
    if(map.GetTileLayerByID(name)!=null)
    {
        map.HideTileLayer(name);
    }
}


var layersLandBase= '';

function getTilePathLandBase(tileContext)
{
    return getTilePath(tileContext,dynamicUrl,layersLandBase);
}

function addDynamicService(dynMap, name,urlPath,getTilePathFunction,ZIndex,visibleOnLoad)
{
    if(dynMap.GetTileLayerByID(name)==null)
    {
        var view = dynMap.GetMapView();
        var top = view.TopLeftLatLong.Latitude;
        var left = view.TopLeftLatLong.Longitude;
        var bottom = view.BottomRightLatLong.Latitude;
        var right = view.BottomRightLatLong.Longitude;
        

        var bounds = [new VELatLongRectangle(new VELatLong(top, left),new VELatLong(bottom, right))];
        var opacity = 0.75;

        
        var tileSourceSpec = new VETileSourceSpecification(name, 
        "");
        tileSourceSpec.NumServers = 1;
        tileSourceSpec.Bounds = bounds;
        tileSourceSpec.MinZoomLevel = 0;
        tileSourceSpec.MaxZoomLevel = 18;
        tileSourceSpec.Opacity = opacity;
        tileSourceSpec.ZIndex = ZIndex;
        tileSourceSpec.GetTilePath = getTilePathFunction;

        
        dynMap.AddTileLayer(tileSourceSpec, visibleOnLoad);
    }
    else
    {
        dynMap.ShowTileLayer(name);
    }

}




function getTilePath(tileContext,URL,layers)
{
    if(tileContext != null && tileContext != "undefined")
    {
        var minx = getMinX(tileContext.XPos,tileContext.ZoomLevel);
        var maxx = getMaxX(tileContext.XPos,tileContext.ZoomLevel);
        var miny = getMinY(tileContext.YPos, tileContext.ZoomLevel);
        var maxy = getMaxY(tileContext.YPos, tileContext.ZoomLevel);
        
        var path = URL + "/export?bbox=" + minx + "%2C" + miny + "%2C" + maxx + "%2C" + maxy + "&bboxSR=4326&format=png&f=image&size=256%2C256&transparent=true&imageSR=102113";
        if (layers != null && layers != "undefined")
        {
            path = path + "&layers=show:" + layers;
        }
        return path;
    }
}

function getMinX(XPos,ZoomLevel)
{
    var minx = 0.0;

    var PixelXMin = XPos * 256;
    minx = (((PixelXMin * 360) / (256 * Math.pow(2, ZoomLevel))) - 180);
    
    return minx;
}

function getMaxX(XPos,ZoomLevel)
{
    var maxx = 0.0;

    var PixelXMax = ((XPos + 1) * 256) - 1;
    maxx = (((PixelXMax * 360) / (256 * Math.pow(2, ZoomLevel))) - 180);
    
    return maxx;
}

function getMinY(YPos,ZoomLevel)
{
    var miny = 0.0;

    var PixelYMax = ((YPos + 1) * 256) - 1;

    var denom = (256 * Math.pow(2, ZoomLevel));
    var eMaxNum = (PixelYMax / denom);
    var eMaxFactor = (Math.pow(Math.E, (0.5 - eMaxNum) * 4 * Math.PI));
    miny = (Math.asin((eMaxFactor - 1) / (eMaxFactor + 1)) * (180 / Math.PI));   
    return miny;
}

function getMaxY(YPos,ZoomLevel)
{
    var maxy = 0.0;
    var PixelYMin = YPos * 256;

    var denom = (256 * Math.pow(2, ZoomLevel));

    var eMinNum = (PixelYMin / denom);
    var eMinFactor = (Math.pow(Math.E, (0.5 - eMinNum) * 4 * Math.PI));
    maxy = (Math.asin((eMinFactor - 1) / (eMinFactor + 1)) * (180 / Math.PI));
    
    return maxy;
}