
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.mood = GetCookie("mood");

    this.gameOptions = "";

    this.current = {
        rank : 0
        ,screenName : ""
        ,userID : ""
        ,treats : 0
        ,score : 0
    };

	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._render = function () {
	this.panel.innerHTML = TrimPath.processDOMTemplate(this.tmpl, this);
};
Leaderboard.prototype._getData = function () {
    Arkadium.Common.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 dt = response.value.table;
    if(dt != null && typeof(dt) == "object" && dt.Rows != null) 
    {
        response.context.data = [];
        for(var i=0;i<dt.Rows.length;i++) 
        {
            var game = typeof(dt.Rows[i].game) == "undefined" ? 0 : dt.Rows[i].game;
            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 userUrl = typeof(dt.Rows[i].userUrl) == "undefined" ? "#" : dt.Rows[i].userUrl;
            response.context.data[response.context.data.length] = {
                rank : dt.Rows[i].rank
                ,screenName : dt.Rows[i].screenName
                ,userID : dt.Rows[i].userID
                ,treats : treats
                ,score : score
                ,star : dt.Rows[i].star
                ,userUrl : userUrl
                ,game : game
                ,gameID : gameID
            };
        }
    }    
    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.typeMyScore = function () {
    this.type = "myscore";
    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();
};

