/*
 * Scores
 */
function Scores(){
	
	Box.call(this);
}

Scores.prototype = new Box;
Scores.prototype.constructor = Scores;
Scores.prototype.type = "top";
Scores.prototype.country = "";
Scores.prototype.countryName = "";
Scores.prototype.icon = "boxicons/scores.png";
Scores.prototype.gameImage = false;
Scores.prototype.lastData = null;
Scores.prototype.singleChild = false;

Scores.prototype.setType = function(type){
	
	this.type = type;
	return this;
};

Scores.prototype.setCountry = function(country){
	
	this.country = country;
	return this;
};

Scores.prototype.setCountryName = function(countryName){
	
	this.countryName = countryName;
	return this;
};

Scores.prototype.setGameImage = function(){
	
	this.gameImage = true;
	return this;
};

Scores.prototype.addContent = function(){
	
	if (this.type == "top")
		this.getScores();
	else if (this.type == "mine")
		this.getRank();
	else if (this.type == "myCountry")
		this.getCountry();
};

Scores.prototype.getRank = function(){
	
	$.getJSON("interface/json.php",{action: "userGameStats", gameId: this.gameId},this.jqueryRankCallbackFunc(this));	
};

Scores.prototype.getCountry = function(){
	
	$.getJSON("interface/json.php",{action: "getUserDetails"},this.jqueryCountryCallbackFunc(this));
};

Scores.prototype.jqueryCountryCallbackFunc = function(obj){
	
	return function(data){
		
		obj.country = data.country;
		obj.countryName = data.countryName;
		obj.getScores(obj);
	};
};

Scores.prototype.jqueryRankCallbackFunc = function(obj){
	
	return function(data){
			 
		obj.starting = data.highestRank - 4;
		if (obj.starting < 1)
			obj.starting = 1;
		obj.getScores(obj);
	};
};

Scores.prototype.getScores = function(obj){

	 if (obj == null)
		 obj = this;
	 $.getJSON("interface/json.php",{action: "getGameDetails", gameId: obj.gameId}, obj.jqueryGameDetailsCallbackFunc(obj));	 	 
	 $.getJSON("interface/json.php",{action: "getScores", gameId: obj.gameId, country: obj.country, starting: obj.starting, number: obj.number}, obj.jqueryScoresCallbackFunc(obj));	 
};

Scores.prototype.jqueryGameDetailsCallbackFunc = function(obj){
	
	return function(data){
		
		var customTitle = false;
		if (obj.title != ""){
			customTitle = true;
			1;
		} else if (obj.country != "")
			obj.title = obj.countryName + " Scoreboard";		
		else if (obj.type == "top")
			obj.title = "Top Scores";
		else if (obj.type == "mine")
			obj.title = "My Position";

		gameName = typeof(data.game) == "undefined" ? "Secret Game" : data.game[0].gameName;
		if (!customTitle) 
			obj.title = obj.title + ":<br/>" + gameName;
		$(obj.showId + " .box_title_text").html(obj.title);
	};
};

Scores.prototype.addScore = function (scoreData){
		
	var score = $("#template_player_scores_score").clone().attr("id","")
	.addClass("scoreboard_position_" + scoreData.scoreboardPosition);
	score.find(".score_rank").html(scoreData.position);
	score.find(".score_image").attr("src","images/avatars/playerpics" + scoreData.picture + ".png");
	score.find(".score_name").html(scoreData.userName);
	score.find(".score_link").attr("href","?action=profile&userId=" + scoreData.userId);			   
	score.find(".score_countryName").html(scoreData.countryName);
	score.find(".score_score").html(scoreData.score);
	score.find(".score_countryFlag").attr("src", "images/flags/" + scoreData.country + ".gif");
	if (scoreData.userId == userId){
		
		score.find(".highlightable").addClass("highlighted");
	}
	
	oldScore = $(this.containerId + " .scoreboard_position_" + scoreData.scoreboardPosition);
	if (oldScore.length != 0){
		
		score.insertAfter(this.containerId + " .scoreboard_position_" + + scoreData.scoreboardPosition);
		score.css("display","");
		score.find(".user_row_rank").effect("pulsate",{},500,function(){$(this).effect("pulsate",{},500,null);});
		score.find(".score_name").css("color","#FFF").animate({"color": "#FC0"},10000);
		
		oldScore.remove();		
	} else {

		score.appendTo(this.containerId);
		score.css("display","");
	}				
};

Scores.prototype.jqueryScoresCallbackFunc = function(obj){
	
	return function (data){
		
		//console.log("Scores: " + obj.title + " - running full update");		
		
		//$(obj.containerId).empty();
				
		for (var i = 0; i < data.score.length; i++){
			
			data.score[i].scoreboardPosition = i;
			obj.addScore(data.score[i]);
		}

		obj.lastData = data;

		//Add click functionality
		$(obj.containerId).find(".score_link").click(function(event){
			
			event.preventDefault();
			var newProfile = new Profile;
			newProfile.setOwner(obj).setUserId(piece($(this).attr("href"),"userId"));
			obj.addChild(newProfile);
		});
		
		if (obj.country == "" || obj.gameImage)
			obj.icon = "boxicons/resize/games/game" + obj.gameId + ".png";
		else
			obj.icon = "boxicons/resize/flags/" + obj.country + ".gif";		 
		obj.loaded();	
		
		if ($(obj.containerId).size() > 0)
			window.setTimeout(obj.timerCallbackFunc(obj),10000);
		else
			console.log("Scores: " + obj.title + " - closed, ending full update timer");			
	};
};

Scores.prototype.timerCallbackFunc = function(obj){
	
	return function(){
		
	 	$.getJSON("interface/json.php",{action: "getScores", gameId: obj.gameId, country: obj.country, starting: obj.starting, number: obj.number}, obj.jqueryScoresUpdateCallbackFunc(obj));	 		
	};
};

Scores.prototype.jqueryScoresUpdateCallbackFunc = function(obj){
	
	return function(data){
		
		//console.log("Scores: " + obj.title + " - running update");
		
		//Update scores where the new data doesn't match
		for (var i = 0; i < data.score.length; i++) {
			
			data.score[i].scoreboardPosition = i;
			if (typeof(obj.lastData.score[i].scoreId) == 'undefined' || data.score[i].scoreId != obj.lastData.score[i].scoreId)
				obj.addScore(data.score[i]);			
		}

		obj.lastData = data;

		//Add / re-add click functionality
		$(obj.containerId).find(".score_link").unbind('click').click(function(event){
			
			event.preventDefault();
			var newProfile = new Profile;
			newProfile.setOwner(obj).setUserId(piece($(this).attr("href"),"userId"));
			obj.addChild(newProfile);
		});	
		
		if ($(obj.containerId).size() > 0)
			window.setTimeout(obj.timerCallbackFunc(obj),10000);
		else
			console.log("Scores: " + obj.title + " - closed, ending update timer");			
	};
};

