// myGamesController.js


function myGamesInitialize()
{
    if (timerId > 0)
    {
        window.clearInterval(timerId);
        //log.write("TIMER: " + timerId);
        timerId = 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("calendar").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 = "cnnMyGms";
   
    var myGamesContainer = document.createElement("div");
    myGamesContainer.className = "cnn3pxlpad";
    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));

    var clrDiv = document.createElement("div");
    clrDiv.className = "cnnClr";
    borderDiv.appendChild(clrDiv);
    gameDiv.appendChild(borderDiv);

    return gameDiv;
}


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

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

    var printStatus = null;

    if (contestO.isInprogress)
        printStatus = "IN PROGRESS";
    else if (contestO.isFinal)
        printStatus = "FINAL";
    else
        printStatus = contestO.gameDateStr;

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

    pElem.appendChild(b);

    return pElem;
}

function createTeamRow(teamO)
{
    var div = null;

    if (teamO != null)
    {
        div = document.createElement("div");
        div.appendChild(createImageElement(IMG_SERVER + LOGO_PATH + teamO.urlName + ".jpg", "40","60","0",(String)(teamO.nickname).toUpperCase(),"left","cnnteamlog"));
        var iDiv = document.createElement("div");
        iDiv.className = "cnnteamftr";
        iDiv.appendChild(createLinkElement(teamO.name.toUpperCase() + " " + teamO.nickname.toUpperCase(),"#",null, false));

        div.appendChild(iDiv);
        var iBr = document.createElement("br");
        iBr.setAttribute("clear","all");
        div.appendChild(iBr);
    } 
    return div;
}

