﻿function initializeGrid ()
{
/// <summary>
/// Create the Xaml Image elements to hold the tiles, based on the size of the Silverlight control
/// </summary>

    var plugin = document.getElementById("SilverlightControl");
    //alert ('Silverlight size:' + plugin.width + ',' + plugin.height);
    //alert ('Silverlight size:' + plugin.content.actualWidth + ',' + plugin.content.actualHeight);
    grid = new Array(Math.ceil(plugin.clientWidth / TILE_SIZE));
    
    for (i = 0; i < grid.length; i++)
    {
        grid[i]=new Array(Math.ceil(plugin.clientHeight / TILE_SIZE));
    }
    //alert ('Tile cols, rows:' + grid.length + ',' + grid[0].length);
    VIEW.set_Width(plugin.clientWidth);
    VIEW.set_Height(plugin.clientHeight);
    
    var canvas = plugin.content.findName("canvas");
    if (canvas.children.count > 0)
        canvas.children.clear();
    var xaml = '';
    var xamlC;
    for (var x = 0; x < grid.length; x++)
    {
        for (var y = 0; y < grid[x].length; y++)
        {
            xaml  = tileImageXaml (x, y, TILE_SIZE);
            xamlC = plugin.content.createFromXaml(xaml);
            //try {
            canvas.children.add(xamlC);
            //} catch (e) {}
        }
    }
}
function drawGridCentered(column, row, wzoom)
{
/// <summary>
/// Draw each tile in the grid, AFTER centering the supplied 
/// column/row position (at zoom)
/// </summary>
    var tempHeightInRows = grid.length;
    var tempWidthInCols = grid[0].length;
    
    var tempColumn = column - Math.floor(tempHeightInRows/2) ;
    var tempRow    = row - Math.floor(tempWidthInCols/2) ;
    
    var newTopLeftTile = new TileFromRowColumn(tempColumn, tempRow, wzoom); 
    for (var x = 0; x < grid.length; x++)
    {
        for (var y = 0; y < grid[x].length; y++)
        {
            var newQuadkey = newTopLeftTile.GetRelativeQuadkey (x,y, WORLD.Columns);
            setTile (x, y, newQuadkey);
        }
    }
    VIEW.Top = CENTER.X - Math.floor(tempHeightInRows/2) * TILE_SIZE;
    VIEW.Left = CENTER.Y - Math.floor(tempWidthInCols/2) * TILE_SIZE;
}
function drawGrid(column, row, wzoom)
{
/// <summary>
/// Draw each tile in the grid, based on top left column/row position (at zoom)
/// </summary>
    alert ('obsolete');
    var newTopLeftTile = new TileFromRowColumn(column, row, wzoom); 
    for (var x = 0; x < grid.length; x++)
    {
        for (var y = 0; y < grid[x].length; y++)
        {
            var newQuadkey = newTopLeftTile.GetRelativeQuadkey (x,y, WORLD.Columns);
            setTile (x, y, newQuadkey);
        }
    }
}












function InitializeMap ()
{
/// <summary>Only called for page onload</summary>
    CENTER = new Point(256,256);
    VIEW = new Viewport (512, 512, TILE_SIZE);
    WORLD.set_Zoom (1);
    
    initializeGrid ();
    drawGridCentered (0, 0, WORLD.Zoom);
    setCanvasOffset(0, 0);
    return false;
}
function ResetMap ()
{
/// <summary>Reset to zoom level 1</summary>
    CENTER = new Point(256,256);
    WORLD.set_Zoom (1);
    drawGridCentered (0, 0, WORLD.Zoom);
    setCanvasOffset(0, 0);
    //ClearMarkers();
    return false;
}
function setCanvasOffset(left, top)
{
/// <summary></summary>
    //alert ('setCanvasOffset('+left+','+ top+')');
    var sl8 = document.getElementById("SilverlightControl");
    var canvasT = sl8.content.findName("canvasTranslate");    var canvas = sl8.content.findName("canvas");
    canvasT.X = left;     canvasT.Y = top;
}