// myGamesController.js


function myGamesInitialize()
{
    if (timerId > 0)
        window.clearInterval(timerId);

    timeId = 0;

    // check if we have currentCalendar stored or not, and if not, store it now before deleting the table.
    if (currentCalendar == null)
        currentCalendar = document.getElementById("myGamesCalendar").cloneNode(true);

    var scoreTable = document.getElementById("scoreTable");

    // blow away the entire table content
    scoreTable.innerHTML = "";

    var padd = document.createElement("div");
    padd.className = "cnn6pxTpad";
    padd.setAttribute("width","822");
    scoreTable.appendChild(padd);
    scoreTable.appendChild(addSaveGamesButtonRow(true));
    
    var myGamesTable = document.createElement("table");

    myGamesTable.className = "cnngmtable";
    myGamesTable.setAttribute("cellPadding","0");
    myGamesTable.setAttribute("cellSpacing","0");
    myGamesTable.setAttribute("border","0");
    myGamesTable.setAttribute("width","822");

    var myGamesTbody = document.createElement("tbody");
    var tr = document.createElement("tr");
    var td = document.createElement("td");

    var div = document.createElement("div");
    div.id = "marginfix";
   
    var myGamesContainer = document.createElement("div");
    myGamesContainer.className = "cnn2pxlpad";
    myGamesContainer.id = "myGamesTable";

    for (var i = 0; i < masterGamesArrayFinal.length; i++)
    {
        myGamesContainer.appendChild(drawMyGameBox(masterGamesArrayFinal[i]));
    }

    var br = document.createElement("br");

    br.setAttribute("clear","all");
    myGamesContainer.appendChild(br);

    div.appendChild(myGamesContainer);
    td.appendChild(div);
    tr.appendChild(td);
    myGamesTbody.appendChild(tr);
    myGamesTable.appendChild(myGamesTbody);
    scoreTable.appendChild(myGamesTable);
    scoreTable.appendChild(addSaveGamesButtonRow(false))

    // =====================================================================
    // This is the one important Scriptaculous function call.
    // This function creates a Sortable object, where all children that
    // are div's will be managed with Scriptaculous functionality.
    // =================================================================
    Sortable.create("myGamesTable", {tag:'div',
                                     overlap:'horizontal',
                                     constraint:false
                                     });
}

function addSaveGamesButtonRow(isTopRow)
{
    var div = document.createElement("div");
    var buttonImage, map;

    if (isTopRow)
    {
        div.className = "cnngmsavetop";
        div.appendChild(createImageElement(IMG_SERVER + IMG_PATH + "/scoreboards/mygames/grey/reorder_your_games_325x44.jpg","44","325","0","",null, null));
        buttonImage = createImageElement(IMG_SERVER + IMG_PATH + "/scoreboards/mygames/save_mygames_175x44.gif","44","175","0","",null,null);
        buttonImage = createLinkElement(buttonImage,"javascript:saveMyGames();",null,true);
        div.appendChild(buttonImage);

        div.appendChild(createLinkElement(createImageElement(IMG_SERVER + IMG_PATH + "/scoreboards/mygames/grey/back_to_scoreboard_320x44.gif","44","320","0","",null, null), "javascript:backToScoreboard();",null, true));
    }
    else
    {
        div.className = "cnngmsavebot";
        iDiv = document.createElement("div");
        var table,tbody,tr, td;

        table = document.createElement("table");
        tbody = document.createElement("tbody");
        table.setAttribute("cellPadding","0");
        table.setAttribute("cellSpacing","0");
        table.setAttribute("border","0");
        table.setAttribute("width","820");

        tr = document.createElement("tr");

        td = document.createElement("td");
        td.appendChild(createImageElement(IMG_SERVER + IMG_PATH + "/scoreboards/mygames/grey/bottom_left.jpg","44","342","0","",null,null));
        tr.appendChild(td);

        td = document.createElement("td");
        buttonImage = createImageElement(IMG_SERVER + IMG_PATH + "/scoreboards/mygames/grey/bottom_button.jpg","44","139","0","",null,null);
        buttonImage = createLinkElement(buttonImage,"javascript:saveMyGames();",null,true);
        td.appendChild(buttonImage);
        tr.appendChild(td);

        td = document.createElement("td");
        td.appendChild(createImageElement(IMG_SERVER + IMG_PATH + "/scoreboards/mygames/grey/bottom_right.jpg","44","339","0","",null,null));
        tr.appendChild(td);
        
        tbody.appendChild(tr);
        table.appendChild(tbody);

        iDiv.appendChild(table);
        div.appendChild(iDiv);
    }
    
    return div;
}

function saveMyGames()
{
    writeToCookie();
    
    newScoreboardAfterMyGamesSelected();
}

function getGamesArrayRow(contestID)
{
    //log.write("..... Inside getGamesArrayRow() function.... looking up contestID " + contestID + " in the master Array");
}


function drawMyGameBox(contestO)
{
    var gameDiv = document.createElement("div");
    gameDiv.className = "games";
    gameDiv.id = contestO.contestID;

    var borderDiv = document.createElement("div");
    borderDiv.className = "gameborder";

    borderDiv.appendChild(createGameHeader(contestO));
    borderDiv.appendChild(createTeamRow(contestO.visitingTeam));
    borderDiv.appendChild(createTeamRow(contestO.homeTeam));

    gameDiv.appendChild(borderDiv);

    return gameDiv;
}


function createGameHeader(contestO)
{
    var pElem = document.createElement("div");
    pElem.className = "gamedate";

    var iDiv = document.createElement("div");
    var b = document.createElement("b");

    var printStatus = null;

    if (contestO.isInprogress)
        printStatus = "In progress";
    else if (contestO.isFinal)
        printStatus = "FINAL";
    else
    {
        printStatus = contestO.gameDateStr;
        if (contestO.gameDateStr.indexOf("TBD") == -1)
        printStatus = printStatus + " ET";
    }

    b.appendChild(document.createTextNode(printStatus));
    iDiv.appendChild(b);

    pElem.appendChild(iDiv);

    return pElem;
}

function createTeamRow(teamO)
{
    var div = null;

    if (teamO != null)
    {
        div = document.createElement("div");

        div.className = "leftalign";
        if (teamO.linkable)
            div.appendChild(createImageElement(IMG_SERVER + "/images/football/ncaa/logos/" + teamO.urlName + "_30.gif", "30","30","0",(String)(teamO.name).toUpperCase(),"left","cnnhlmt"));
        else
            div.appendChild(createImageElement(BLANK_IMG, "30","30","0",teamO.name,"left","cnnhlmt"));
        var iDiv = document.createElement("div");
        iDiv.className = "cnnteamftr";
        var b = document.createElement("b");
        b.appendChild(document.createTextNode((String)(teamO.name).toUpperCase()));
        iDiv.appendChild(b);

        iDiv.appendChild(document.createElement("br"));
        
        //var iSpan = document.createElement("span");
        //iSpan.appendChild(document.createTextNode(createTeamRecord(teamO.wins, teamO.losses, true)));
        //iDiv.appendChild(iSpan);
        
        div.appendChild(iDiv);
        var iBr = document.createElement("br");
        iBr.setAttribute("clear","all");
        div.appendChild(iBr);
    } 
    return div;
}
