// Javascript functions used by MapEnhancer web control.

/// Tile load balancing functions ///

// Pads a string
function pad(toPad, padding, totalLength, padLeft) {
    if (toPad.length < totalLength) {
        if (padLeft)
            toPad = padding + toPad;
        else
            toPad = toPad + padding;
    }
    if (toPad.length >= totalLength)
        return toPad;
    return pad(toPad, padding, totalLength, padLeft);
}

// Custom function for generating tile url for jpg images
function arcgisVirtualDirectoryTileUrlGenerator_jpg_LoadBalanced(level, column, row, vdir) {
    return arcgisVirtualDirectoryTileUrlGenerator_LoadBalanced(level, column, row, vdir, '.jpg');
}

// Custom function for generating tile url for png images
function arcgisVirtualDirectoryTileUrlGenerator_png_LoadBalanced(level, column, row, vdir) {
    return arcgisVirtualDirectoryTileUrlGenerator_LoadBalanced(level, column, row, vdir, '.png');
}

// Custom function for generating tile url for jpg and png images. Called every time the ADF needs to
// request a tile.
function arcgisVirtualDirectoryTileUrlGenerator_LoadBalanced(level, column, row, vdir, filesuffix) {
    var sLevel = 'L' + pad(level.toString(), '0', 2, true);
    var sRow = 'R' + pad(row.toString(16), '0', 8, true);
    var sColumn = 'C' + pad(column.toString(16), '0', 8, true) + filesuffix;
    var normalUrl = vdir + '/' + sLevel + '/' + sRow + '/' + sColumn;

    var vdirs = vdir.split('|'); // first part is original url, next parts are the hosts to be used
    if(vdirs.length < 2) return normalUrl;
    // Parse original url:
    var i = vdirs[0].indexOf('http://');
    if(i < 0) return normalUrl;
    var j = vdirs[0].indexOf('/', i+7);
    if(j < i) return normalUrl;
    var path = vdirs[0].substring(j);
    // Select a host based on the row and column. The same row and column will always give the same host,
    // thus client caching will still work.
    var hostNr = (column+row)%(vdirs.length - 1);
    var url = 'http://' + vdirs[hostNr + 1] + path + '/' + sLevel + '/' + sRow + '/' + sColumn;
    return url;
}

// Modify a map resource to make it load balanced. Modifies the tileServerUrlGenerator property, which
// is the name of the custom function that generates the tile url. Also modifies the tileServerUrl property,
// which is the url to the root of the map cache, to remember which hosts to load balance between.
function makeTiledResourceLoadBalanced(mapId, resourceName, loadBalancingHosts) {
    var map = Maps[mapId];
    if(!(map)) return;
    var rc = map.ResourceCollection;
    if(!(rc)) return;
    for(var i = 0; i < rc.length; i++)
    {
        if(rc[i].id == resourceName)
        {
            makeTiledResourceLoadBalanced2(rc[i], loadBalancingHosts);
        }
    }
}
function makeTiledResourceLoadBalanced2(resource, loadBalancingHosts)
{
    resource.tileServerUrlGenerator += '_LoadBalanced';
    resource.tileServerUrl += '|' + loadBalancingHosts;
}
/// End of tile load balancing functions ///

// Script evaluated on map.RefreshResource, that sabotages us:
//addScriptTag('arcgisVirtualDirectoryTileUrlGenerator_jpg', null, 'javascript', null, null, null, 'function pad(toPad, padding, totalLength, padLeft){if (toPad.length < totalLength){if (padLeft) toPad = padding + toPad;else toPad = toPad + padding;}if (toPad.length >= totalLength) return toPad;return pad(toPad, padding, totalLength, padLeft);}function arcgisVirtualDirectoryTileUrlGenerator_jpg(level, column, row, vdir){var sLevel = \'L\' + pad(level.toString(), \'0\', 2, true);var sRow = \'R\' + pad(row.toString(16), \'0\', 8, true);var sColumn = \'C\' + pad(column.toString(16), \'0\', 8, true) + \'.jpg\';return vdir + \'/\' + sLevel + \'/\' + sRow + \'/\' + sColumn;}');
//var rc=Maps["Map1"].ResourceCollection; var index=rc.getPosition("MapResourceItem0");if (index>-1){ rc[index].tileServerUrl='http://cache.geodataonline.no/mapcache/Geocache_GeoCacheVektor/Layers/_alllayers';rc[index].tileServerUrlGenerator='arcgisVirtualDirectoryTileUrlGenerator_jpg'; rc[index].visible=true; rc[index].setLimits(0,0,16,16); rc[index].redraw(); }