
window.attachEvent("onload", function()
{
    // Init Tray Table
    var inx;
    for (inx = 0; inx < 7; inx++)
    {
        var trayTD = trayTable.rows[0].insertCell();
        trayTD.id = "trayCell"+(inx+1);
        trayTD.style.width = "41px";
        trayTD.style.height = "41px";
        trayTD.innerHTML = "&nbsp;";

        var td = trayTable.rows[1].insertCell();
        td.innerHTML = "#"+(inx+1);
    }
});


function Bag(){}
Bag.TilesRemaining = function()
{
    var used = Board.laidCount + (Player.ActivePlayers() * 7);
    return 100 - used;
}

function trayCount()
{
    var tiles = TrayTiles();

    var inx;
    for (inx = 0; inx < 7; inx++)
    {
        var trayTD = trayTable.rows[0].cells[inx];
        if ( inx < tiles.length )
        {
            var tile = tiles[inx];
            
            trayTD.tileInx = tile.scrabbleSetInx;
            tile.SetPosition(trayTD);
        }
        else
            trayTD.tileInx = -1;
    }
}


function replenishTray()
{
    var needTile = new Array();

    for (var inx = 0; inx < 7; inx++)
    {
        var trayTD = trayTable.rows[0].cells[inx];
        trayTD.id = "trayCell"+(inx+1);
        if ( !TrayFilled(trayTD) )
            needTile.push( trayTD );
    }

    if (needTile.length > 0)
    {
        var newTiles = new Array();

        var respTxt = CallbackBusSyncEx("replenishTray", needTile.length);
        if ( respTxt.length == 0 )
            return;

        var setInxs = respTxt.split(/\n/);

        for (var inx = 0; inx < setInxs.length; inx++)
        {
            var setInx = setInxs[inx];
            var trayTD = needTile[inx];

            var tile = scrabbleSet[ parseInt(setInx) ];

            while (trayTD.childNodes.length > 0)
                trayTD.childNodes[0].removeNode();
            trayTD.tileInx = tile.scrabbleSetInx;
            tile.SetPosition(trayTD);
            tile.ShowImg();
        }
    }
}


function TrayFilled( trayTD )
{
    return ( typeof(trayTD.tileInx) != "undefined" && trayTD.tileInx != -1 );
}
function TrayTile( trayTD )
{
    if ( TrayFilled( trayTD ) )
        return scrabbleSet[ trayTD.tileInx ];
    else
        return null;
}

function TrayTiles()
{
    var tiles = new Array();
    var inx;
    for (inx = 0; inx < 7; inx++)
    {
        var trayTD = trayTable.rows[0].cells[inx];
        var tile = TrayTile( trayTD );
        if (tile == null)
            continue;
        list_push( tiles, tile );
    }
    return tiles;
}


function jumbleTray()
{
    var tiles = TrayTiles();

    tiles.sort( function( a, b )
                {
                    return (Math.random() < 0.5) ? 1 : -1;
                }
              );

    var inx;
    for (inx = 0; inx < 7; inx++)
    {
        var trayTD = trayTable.rows[0].cells[inx];
        if ( inx < tiles.length )
        {
            var tile = tiles[inx];
            
            trayTD.tileInx = tile.scrabbleSetInx;
            tile.SetPosition(trayTD);
        }
        else
            trayTD.tileInx = -1;
    }
}


function Tile( scrabbleSetInx, letter )
{
    this.scrabbleSetInx = scrabbleSetInx;
    this.letter = letter;

    this.fname = this.letter;
    if (this.fname == " ")
        this.fname = "blank";
    this.fname += ".png";

    this.imgNode = document.createElement("IMG");
    this.imgNode.style.position = "absolute";
    this.imgNode.src = this.fname;
    this.HideImg();
    document.body.appendChild( this.imgNode );
}
Tile.prototype.ShowImg = function ()
{
    this.imgNode.style.display = "block";
}
Tile.prototype.HideImg = function ()
{
    this.imgNode.style.display = "none";
}
Tile.prototype.SetPosition = function ( e )
{
    this.imgNode.style.top = findPosY(e)+6;
    this.imgNode.style.left = findPosX(e)+6;
}
Tile.prototype.Raise = function ()
{
    this.imgNode.style.top = addPixels(this.imgNode.style.top, -20);
}
Tile.prototype.Lower = function ()
{
    this.imgNode.style.top = addPixels(this.imgNode.style.top, 20);
}
Tile.prototype.GetPosition = function ()
{
    return new Point( 
                        /*x*/this.GetVaryingDim(Orientation.Horizontal), 
                        /*y*/this.GetVaryingDim(Orientation.Vertical)
                    );
}
Tile.prototype.GetVaryingDim = function (orient)  // If this tile belongs to a word (in either a Horizontal or Vertical orientation)
{                                                 // then return the tile's cooridinate along the varying axis (e.g. Horizontal word, x-axis varies)
    return orient == Orientation.Horizontal ?
            (getPixels(this.imgNode.style.left) - 6 - Board.startX) / 40 + 1
                :
            (getPixels(this.imgNode.style.top) - 6 - Board.startY) / 40 + 1
                ;
}
Tile.prototype.GetFixedDim = function (orient)  // If this tile belongs to a word (in either a Horizontal or Vertical orientation)
{                                               // then return the tile's cooridinate along the fixed axis (e.g. Horizontal word, y-axis is fixed)
    return this.GetVaryingDim( !orient );
}
Tile.prototype.OnBoard = function ()
{
    var pt = this.GetPosition();
    return (1 <= pt.col && pt.col <= 15 && 1 <= pt.row && ptrow <= 15);
}
Tile.prototype.Score = function ()
{
    if ( this.letterDIV )
        return 0;

    switch (this.letter)
    {
        case 'a':
        case 'e':
        case 'i':
        case 'l':
        case 'n':
        case 'o':
        case 't':
        case 'r':
        case 's':
        case 'u':
            return 1;
        case 'd':
        case 'g':
            return 2;
        case 'b':
        case 'c':
        case 'p':
        case 'm':
            return 3;
        case 'f':
        case 'h':
        case 'w':
        case 'v':
        case 'y':
            return 4;
        case 'k':
            return 5;
        case 'x':
        case 'j':
            return 8;
        case 'q':
        case 'z':
            return 10;
    }
}


function InitScrabbleSet( letters )
{
    var inx;
    for (inx = 0; inx < letters.length; inx++)
    {
        var letter = letters[inx];
        list_push( scrabbleSet, new Tile( inx, letter ) );
    }
}
var scrabbleSet = new Array();






