
window.attachEvent("onload", 
    function ()
    {
        CheckAllGames();
        setInterval(CheckAllGames, 5 * 1000);

        GetTopScores();
        setInterval(GetTopScores, 60 * 1000);
    }
);

function CheckAllGames()
{
    CallbackBus("allGames", function( txt )
    {
        while (gameList.rows.length > 0)
            gameList.deleteRow(0);
            
        if ( txt == "" )
        {
            var tr = gameList.insertRow();
            var td = tr.insertCell();
            td.innerHTML = "<span style='color:gray'>...No Games Playing Currently.</span>";
            return;
        }

        // Header
        var tr = gameList.insertRow();
        var td = tr.insertCell();
        td.innerHTML = "<B>Game Name&nbsp;&nbsp;&nbsp;</B>";
        var td = tr.insertCell();
        td.innerHTML = "<B>Creator&nbsp;&nbsp;&nbsp;</B>";
        var td = tr.insertCell();
        td.innerHTML = "<B>Number of Players&nbsp;&nbsp;&nbsp;</B>";
        var td = tr.insertCell();
        td.innerHTML = "<B>Started?&nbsp;&nbsp;</B>";

        // Games
        var games = txt.split(/\n/);
        for (var gnx = 0; gnx < games.length; gnx++)
        {
            var gameArray = games[gnx].split(/\t/);
            if (gameArray.length == 1)
                continue;

            var tr = gameList.insertRow();
            tr.insertCell().innerHTML = gameArray[0];
            tr.insertCell().innerHTML = gameArray[1];
            tr.insertCell().innerHTML = gameArray[2];
            tr.insertCell().innerHTML = gameArray[3]=="Y" ? "<span style='color:red'>Yes</span>" : "No";
            
            tr.onclick = function(gameName)
            {
                return function()
                {
                    $("gameRoomTxtBox").value = gameName;
                    $('usernameTxtBox').focus();
                    CheckUsernameAndGameRoom();
                };
            }(gameArray[0]);
            tr.onmouseover = MouseOver( tr );
            tr.onmouseout = MouseOut( tr );
        }
    });
}

function GetTopScores()
{
    CallbackBus("getTopScores", function( txt )
    {
        var doc = NewXmlDoc(txt);
        
        _GetTopScore( doc, topGameScores, "game", ["Player", "Score", "Versus", "Date"] );
        _GetTopScore( doc, topPlays, "play", ["Player", "Words", "Score", "Versus", "Date"] );
        _GetTopScore( doc, topWordScores, "legalWord", ["Player", "Word", "Score", "Date"] );
        _GetTopScore( doc, topBluffScores, "bluffWord", ["Player", "Word", "Score", "Versus", "Date"] );
    });
}
function _GetTopScore( doc, t, elemName, attributes )
{
    while (t.rows.length > 1)
        t.deleteRow(1);
    var nodeList = doc.documentElement.selectNodes(elemName);
    var sortedArray = [];
    for (var gnx = 0; gnx < nodeList.length; gnx++)
        sortedArray.push(nodeList[gnx]);
    sortedArray.sort(function(a,b)
    {
        return parseInt(b.getAttribute("Score")) - parseInt(a.getAttribute("Score"));
    });
    for (var gnx = 0; gnx < sortedArray.length; gnx++)
    {
        var e = sortedArray[gnx];
        var tr = t.insertRow();
        
        tr.insertCell().innerHTML = (gnx+1)+".";
        
        for (var anx = 0; anx < attributes.length; anx++)
            tr.insertCell().innerHTML = e.getAttribute( attributes[anx] );
    }
}