﻿$(document).ready(function () {
    $("a[class='lightbox']").fancybox({ 'titleShow': false, 'hideOnContentClick': false });
    $("#hrefmap").click(
        function () {
            loadBingAPI();
        }
    );
});
var loaded = false;
var map = null;

function onmapload() {
    
    map = new VEMap('largemap');
    var cent = new VELatLong($("#lat")[0].value, $("#lon")[0].value);
    map.LoadMap(cent, $("#zoom")[0].value, VEMapStyle.Road);
    var shp = new VEShape(VEShapeType.Pushpin, cent);
    shp.SetCustomIcon('/content/images/mapcontrols/pushpin.png');
    map.AddShape(shp);

    var tileSourceSpec =
		new VETileSourceSpecification(
			"mini", "", 1, null, 7, 10, MiniMapGetMainTileLayerPath
            // tileSourceId, tileSource, numServers, bounds, minZoom, maxZoom, getTilePath, opacity, zindex
		);

    map.AddTileLayer(tileSourceSpec, true);
}
function MiniMapGetMainTileLayerPath(tileContext) {
    // Bing numbers quad keys from the top left of the map, GeoServer/GeoWebCache starts bottom left
    // so we need to reverse the y co-ordinates
    var numTiles = Math.pow(4, tileContext.ZoomLevel);
    var maxXYOnTile = Math.sqrt(numTiles);
    var magicNumber = Math.floor((tileContext.ZoomLevel / 2) - 0.5)
    var maxXYOnFolder = Math.pow(2, magicNumber);
    var digitsOnFolder = Math.floor(tileContext.ZoomLevel / 6) + 1;
    var digitsOnTile = digitsOnFolder * 2;

    var x = tileContext.XPos;
    var y = MiniMapReverseY(tileContext.YPos, tileContext.ZoomLevel);

    var folderX = Math.floor(x / Math.pow(2, tileContext.ZoomLevel - magicNumber));
    var folderY = Math.floor(y / Math.pow(2, tileContext.ZoomLevel - magicNumber));

    folderX = MiniMapPadValue(folderX, digitsOnFolder);
    folderY = MiniMapPadValue(folderY, digitsOnFolder);

    x = MiniMapPadValue(x, digitsOnTile);
    y = MiniMapPadValue(y, digitsOnTile);

    var url = "/tiles/airports/EPSG_900913_" + MiniMapPadValue(tileContext.ZoomLevel, 2) + "/" + folderX + "_" + folderY + "/" + x + "_" + y + ".png";

    return url;
}
function MiniMapPadValue(value, totalDigits) {
    value = value.toString();
    var pd = '';
    if (totalDigits > value.length) {
        for (i = 0; i < (totalDigits - value.length); i++) {
            pd += '0';
        }
    }
    return pd + value.toString();
}
function MiniMapReverseY(value, zoomLevel) {
    var max = Math.pow(2, zoomLevel) - 1;
    return Math.abs((value - max));
}
function loadBingAPI() {


    if (!loaded) {
        loaded = true;
        //set a nice animated gif to show the map is loading
        if (!(window.attachEvent)) {
            appendJS("http://ecn.dev.virtualearth.net/mapcontrol/v6.2/js/atlascompat.js");
        }
        appendJS("http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2&onScriptLoad=onmapload");
    }
}
function appendJS(filename) {
    var fileref = document.createElement('script');
    fileref.setAttribute("type", "text/javascript");
    fileref.setAttribute("src", filename);
    document.getElementsByTagName("head")[0].appendChild(fileref);
}   
