﻿function ScoresTop(typeClassement, id, salle, control, timeToRefresh)
{
    this.ScoresTopType = typeClassement;
    this.ScoresTopId = id;
    this.ScoresTopRoom = salle;
    this.ScoresTopControl = control;
    this.ScoresTopTimeToRefresh = timeToRefresh;
    this.ScoresTopRetries = 0;
    this.ScoresTopTimer = null;
    
    this.StartTopScores = function()
    {
        this.ScoresTopTimerRefresh();
    }

    this.StopTopScores = function()
    {
        if (this.ScoresTopTimer!=null)
        {
            clearInterval(this.ScoresTopTimer);
        }
    }

    this.RestartTopScores = function()
    {
        this.StopTopScores();
        if (typeof(VdcInfosDiv) != "undefined")
        {
            this.ScoresTopTimerRefresh();
            this.ScoresTopTimer = setInterval("this.ScoresTopTimerRefresh()", this.ScoresTopTimeToRefresh);
        }
    }

    this.ScoresTopTimerRefresh = function()
    {
        if (typeof(FrontUtils) != "undefined")
        {
            this.ScoresTopRetries = 0;
            eval('FrontUtils.GetScoresTop(this.ScoresTopType, this.ScoresTopId, this.ScoresTopRoom, this.ScoresTopControl, this.ScoresTopTimerRefreshSuccess' + this.ScoresTopType + ');');
            eval('FrontUtils.GetScoreForGame(this.ScoresTopType, this.ScoresTopId, this.ScoresTopRoom, this.ScoresTopControl, this.YourScoreSuccess' + this.ScoresTopType + ');');
        }
        else
        {
            if (this.ScoresTopRetries < 10)
            {
                this.ScoresTopRetries++;
                this.ScoresTopDiv.innerHTML = "Chargement des scores...";
            }
            else
            {
                //trop de tentatives
                this.ScoresTopDiv.innerHTML = "Merci d'actualiser la page.";
            }
        }
        
    }

    this.YourScoreSuccessMONTH = function(result) { YourScoreSuccessDiv(result, 'divClmntYourMONTH'); }
    this.YourScoreSuccessOLD = function(result) { YourScoreSuccessDiv(result, 'divClmntYourOLD'); }
    this.YourScoreSuccessNEW = function(result) { YourScoreSuccessDiv(result, 'divClmntYourNEW'); }

    this.ScoresTopTimerRefreshSuccessMONTH = function(result) { ScoresTopTimerRefreshSuccessDiv(result, 'divClmntTopMONTH'); }
    this.ScoresTopTimerRefreshSuccessOLD = function(result) { ScoresTopTimerRefreshSuccessDiv(result, 'divClmntTopOLD'); }
    this.ScoresTopTimerRefreshSuccessNEW = function(result) { ScoresTopTimerRefreshSuccessDiv(result, 'divClmntTopNEW'); }
}

function YourScoreSuccessDiv(result, div)
{
    var obj = document.getElementById(div);
    if (typeof(obj) == "undefined") return;
    
    switch(result)
    {
        case -3 : obj.innerHTML = "<a href=\"../frictiffs/gestioncompte.aspx\">activez l'option JeuxPlus</a> !";break;
        case -2 : obj.innerHTML = "indisponible pour ce jeu";break;
        case -1 : obj.innerHTML = "jouez une partie class&eacute;e !";break;
        default : obj.innerHTML = "" + result;break;
    }
}

function ScoresTopTimerRefreshSuccessDiv(result, div)
{
    var obj = document.getElementById(div);
    if (typeof(obj) == "undefined") return;
    
    var str = "";
    if (!result)
    {
        str = "Le classement est indisponible pour ce jeu";
    }
    else
    {
        if (result.length == 0)
        {
            str = "Aucun classement";
        }
        else
        {
            str += "<table style=\"font-size:9px;\">\n";
            str += "<tr>\n";
            str += "<th class=\"TopRang\">Rg</th>\n";
            str += "<th class=\"TopPseudo\">Pseudo</th>\n";
            str += "<th class=\"TopScore\">Classement</th>\n";
            str += "<th class=\"TopAttitude\">Att.</td>\n";
            str += "</tr>\n";
            for(i = 0; i < result.length; i++)
            {
                str += AddScoresTopNewUser(result[i]);
            }
            str += "</table>\n";
        }
    }
    obj.innerHTML = str;
}

function AddScoresTopNewUser(ligne)
{
    var strTmp = "<tr>\n";
    strTmp += "<td class=\"TopRang\">" + ligne[0] + "</td>\n";
    strTmp += "<td class=\"TopPseudo\">" + ligne[1] + "</td>\n";
    strTmp += "<td class=\"TopScore\">" + ligne[2] + "</td>\n";
    strTmp += "<td class=\"TopAttitude\">" + ligne[3] + "</td>\n";
    strTmp += "</tr>\n";
    
    return strTmp;
}

function ScoresAll(classementType, salle)
{
    this.ScoresAllType = classementType;
    this.ScoresAllDiv = document.getElementById("divScoresAll");
    this.ScoresAllRoom = salle;
    this.ScoresAllPage = 0;
    this.ScoresAllRetries = 0;

    this.GetScoresAll = function(page)
    {
        if (typeof(FrontUtils) != "undefined")
        {
            this.ScoresAllRetries = 0;
            this.ScoresAllPage = page;
            this.ScoresAllDiv.innerHTML = "<br/>Chargement, merci de patienter quelques secondes... <img src='http://data.jeuxclassiques.com/designs/images/spinner.gif' border='0'><br/>";
            FrontUtils.GetScoresAll(this.ScoresAllType, this.ScoresAllRoom, this.ScoresAllPage, GetScoresAllSuccess);
            FrontUtils.GetScoresNbPages(this.ScoresAllType, this.ScoresAllRoom, this.ScoresAllPage, GetScoresAllNbPagesSuccess);
        }
        else
        {
            if (this.ScoresAllRetries<10)
            {
                this.ScoresAllRetries++;
                this.ScoresAllDiv.innerHTML = "<br/>Chargement, merci de patienter quelques secondes... <img src='http://data.jeuxclassiques.com/designs/images/spinner.gif' border='0'><br/>";
                setTimeout("this.GetScoresAll()", 1000 * this.ScoresAllRetries);
            }
            else
            {
                ScoresAllDiv.innerHTML = "Erreur lors du chargement du classement.";
            }
        }
    }
}

function GetScoresAllSuccess(result)
{
    var ScoresAllDiv = document.getElementById("divScoresAll");
    if (typeof(ScoresAllDiv) == "undefined") return;
    
    if (!result)
    {
        ScoresAllDiv.innerHTML = "Le classement n'est pas disponible pour ce jeu.";
        return;
    }
    
    if (result.length == 0)
    {
        ScoresAllDiv.innerHTML = "Aucun classement pour ce jeu.";
        return;    
    }
    
    var str = "";
    str += "<table style=\"font-size:11px;\">\n";
    str += "<tr>\n";
    str += "<th class=\"rang\">Rg</th>\n";
    str += "<th class=\"pseudo\">Pseudo</th>\n";
    str += "<th class=\"score\">Classement</th>\n";
    str += "<th class=\"attitude\">Att.</td>\n";
    str += "<th class=\"nbparties\">Jou&eacute;es</td>\n";
    str += "<th class=\"nbparties\">Termin&eacute;es</td>\n";
    str += "<th class=\"nbparties\">Gagn&eacute;es</td>\n";
    str += "</tr>\n";
    for(i = 0; i < result.length; i++)
    {
        str += AddScoresAll(result[i]);
    }
    str += "</table>\n";

    ScoresAllDiv.innerHTML = str;
}

function AddScoresAll(ligne)
{
    var strTmp = "<tr>\n";
    strTmp += "<td class=\"rang\">" + ligne[0] + "</td>\n";
    strTmp += "<td class=\"pseudo\">" + ligne[1] + "</td>\n";
    strTmp += "<td class=\"score\">" + ligne[2] + "</td>\n";
    strTmp += "<td class=\"attitude\">" + ligne[3] + "</td>\n";
    strTmp += "<td class=\"nbparties\">" + ligne[4] + "</td>\n";
    strTmp += "<td class=\"nbparties\">" + ligne[5] + "</td>\n";
    strTmp += "<td class=\"nbparties\">" + ligne[6] + "</td>\n";
    strTmp += "</tr>\n";
    return strTmp;
}

function GetScoresAllNbPagesSuccess(tabNbpages)
{
    var ScoresAllPagesDiv = document.getElementById("divScoresAllPages");
    if (typeof(ScoresAllPagesDiv) == "undefined") return;
    
    if (tabNbpages == null || tabNbpages.length != 2) return;
    
    var iPage = tabNbpages[0];
    var nbpages = tabNbpages[1];
    
    var str = "";
    str += "<table style=\"font-size:11px;\">\n";
    str += "<tr>\n";
    str += "<th class=\"pagenumber\">Pages :&nbsp;";
    
    //firstMin firstMin+1 firstMax ... secondMin SecondMin+1 ScoresAllPage secondMax-1 secondMax  ... thirdMin thirdMin+1 thirdMax
    
    var firstMin = 0;
    var firstMax = firstMin + 3; if (firstMax > nbpages) firstMax = nbpages;
    
    var secondMin = iPage - 3; if (secondMin < firstMax) secondMin = firstMax;
    var secondMax = iPage + 3; if (secondMax > nbpages) secondMax = nbpages;
    
    var thirdMax = nbpages;
    var thirdMin = thirdMax - 3; if (thirdMin < secondMax) thirdMin = secondMax;
    
    var i = 0;
    
    for (i=firstMin; i<firstMax; i++)
    {
        if (i==iPage)
            str += "" + (i+1) + "</a>&nbsp;";
        else
            str += "<a href=\"javascript:scall.GetScoresAll(" + i + ");\">" + (i+1) + "</a>&nbsp;";
    }
    
    if (secondMin > firstMax) str += "...";
    
    for (i=secondMin; i<secondMax; i++)
    {
        if (i==iPage)
            str += "" + (i+1) + "</a>&nbsp;";
        else
            str += "<a href=\"javascript:scall.GetScoresAll(" + i + ");\">" + (i+1) + "</a>&nbsp;";
    }
    
    if (thirdMin > secondMax) str += "...";

    for (i=thirdMin; i<thirdMax; i++)
    {
        if (i==iPage)
            str += "" + (i+1) + "</a>&nbsp;";
        else
            str += "<a href=\"javascript:scall.GetScoresAll(" + i + ");\">" + (i+1) + "</a>&nbsp;";
    }    

    str += "</th>\n";
    str += "</tr>\n";
    
    ScoresAllPagesDiv.innerHTML = str;
}