
function Leaderboard(name, tmpl)
{
	this.name = name;
	this.tmpl = tmpl;
	this.panel = document.getElementById(name);
	this.panel.leaderboard = this;

	this.nTop = 10;

	this.type = "treats";
	this.userID = 0;
	this.gameID = 0;
	this.group = "all";
	this.period = "week";

	this.userID = 0;

    this.gameOptions = "";

    this.current = {
        screenName: ""
        ,screenNameShort : ""
        ,userID: 0
        ,treats : 0
        ,score : 0
        ,rank_treats : 0
        ,rank_score : 0
        
    };

	this.hideGames ='';
	
	this.baseUrl = '/';

	return this;
}
Leaderboard.prototype._updateGameOptions = function () {
    this.gameOptions = "<option value='0'>All Games</option>\n";
    for (var i = 0; i < LB_games.length; ++i)
    {
		this.gameOptions += "<option value='" + LB_games[i].id + "'" + (LB_games[i].id == this.gameID ? " selected='selected'" : "") + ">" + LB_games[i].name + "</option>\n";
	}
}

Leaderboard.prototype._short = function(src) {
    if (!src)
        return src;
    if (src.length <= 10)
        return src;
    return src.substring(0, 10) + "...";
};

Leaderboard.prototype._render = function () {
	this.panel.innerHTML = TrimPath.processDOMTemplate(this.tmpl, this);
	if (this.hideGames)
	    document.getElementById(this.hideGames).style.display = 'none';
};
Leaderboard.prototype._getData = function () {
    Arkadium.LifeTime.GameArena.ClientSite.Common.Controls.Leaderboard.GetData(
		this.group	    	// all, friends
		,this.period		// all, week, today
		,this.userID
		,this.type  		// score, treats
		,this.gameID		// specified gameID or 0 for all games mode
		,this.nTop
        ,this._getData_CallBack, this);
};
Leaderboard.prototype._getData_CallBack = function(response) {

    if (response.value.current == null) {
        response.context.current.userID = 0;
    } else {
        response.context.current = response.value.current;
        var rcc = response.context.current;
        rcc.screenNameShort = response.context._short(rcc.screenName);
        rcc.screenNameShort = rcc.screenNameShort.split("'").join('&rsquo;');
        rcc.screenName = rcc.screenName.split("'").join('&rsquo;');
    }

    response.context.data = [];
    var dt = response.value.table;
    if (dt != null && typeof (dt) == "object" && dt.Rows != null) {
        for (var i = 0; i < dt.Rows.length; i++) {
            var game = typeof (dt.Rows[i].game) == "undefined" ? 0 : dt.Rows[i].game.split("'").join('&rsquo;');
            var gameShort = typeof (dt.Rows[i].game) == "undefined" ? 0 : response.context._short(dt.Rows[i].game).split("'").join('&rsquo;');
            var gameName = typeof (dt.Rows[i].gameName) == "undefined" ? 0 : dt.Rows[i].gameName;
            var gameID = typeof (dt.Rows[i].gameID) == "undefined" ? 0 : dt.Rows[i].gameID;
            var score = typeof (dt.Rows[i].score) == "undefined" ? 0 : dt.Rows[i].score;
            var treats = typeof (dt.Rows[i].treats) == "undefined" ? 0 : dt.Rows[i].treats;
            var rank = typeof (dt.Rows[i].rank) == "undefined" ? 0 : dt.Rows[i].rank;
            var userUrl = typeof (dt.Rows[i].userUrl) == "undefined" ? "#" : dt.Rows[i].userUrl;
            var userAvatar = typeof (dt.Rows[i].userAvatar) == "undefined" ? "#" : dt.Rows[i].userAvatar;
            var gameUrl = typeof (dt.Rows[i].gameUrl) == "undefined" ? "#" : dt.Rows[i].gameUrl;
            var trophy = typeof (dt.Rows[i].trophy) == "undefined" ? "0" : dt.Rows[i].trophy;
            var star = typeof (dt.Rows[i].star) == "undefined" ? "0" : dt.Rows[i].star;
            response.context.data[response.context.data.length] = {
                screenName: dt.Rows[i].screenName.split("'").join('&rsquo;')
                , screenNameShort: response.context._short(dt.Rows[i].screenName).split("'").join('&rsquo;')
                , userID: dt.Rows[i].userID
                , treats: treats
                , score: score
                , rank: rank
                , star: dt.Rows[i].star
                , userUrl: userUrl
                , userAvatar: userAvatar
                , gameID: gameID
                , gameName: gameName
                , game: game
                , gameShort: gameShort
                , gameUrl: gameUrl
                , trophy: trophy
                , star: star
            };
        }
    }
    response.context._render();
};
Leaderboard.prototype.update = function () {
    this._updateGameOptions();
    this._getData();
};

Leaderboard.prototype.typeTreats = function () {
    this.type = "treats";
    this.update();
};
Leaderboard.prototype.typeScore = function () {
    this.type = "score";
    this.update();
};

Leaderboard.prototype.groupFriends = function () {
    this.group = "friends";
    this.update();
};
Leaderboard.prototype.groupAll = function () {
    this.group = "all";
    this.update();
};
Leaderboard.prototype.periodToday = function () {
    this.period = "today";
    this.update();
};
Leaderboard.prototype.periodWeek = function () {
    this.period = "week";
    this.update();
};
Leaderboard.prototype.periodAll = function () {
    this.period = "all";
    this.update();
};
Leaderboard.prototype.game = function (element) {
    this.gameID = element.value;
    this.update();
};

