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

LatestScores.prototype = new Box;
LatestScores.prototype.constructor = LatestScores;
LatestScores.prototype.type = "top";
LatestScores.prototype.country = null;
LatestScores.prototype.icon = "boxicons/scores.png";
LatestScores.prototype.oldIds = null;

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

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

LatestScores.prototype.addContent = function(){
	
	if (this.type == "top")
		this.getLatestScores();
	else if (this.type == "mine")
		this.getRank();
}

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

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

LatestScores.prototype.getLatestScores = function(obj){

	 if (obj == null)
		 obj = this;
	console.log("LatestScores: " + obj.title + " - getting latest scores");		 
	 $.getJSON("interface/json.php",{action: "getScores", gameId: obj.gameId, orderBy: "date", starting: obj.starting, number: obj.number, country: obj.country}, obj.jqueryLatestScoresCallbackFunc(obj));	 
}

LatestScores.prototype.timerCallbackFunc = function(obj){
	
	return function(){
		
		console.log("LatestScores: " + obj.title + " - getting latest scores");		 
		$.getJSON("interface/json.php",{action: "getScores", gameId: obj.gameId, orderBy: "date", starting: obj.starting, number: obj.number, country: obj.country}, obj.jqueryLatestScoresCallbackFunc(obj));	 
	}
}

LatestScores.prototype.jqueryLatestScoresCallbackFunc = function(obj){
	
	return function (data){
				
		$(obj.containerId).empty();
		
		newIds = new Array;
				
		for (var i = 0; i < data.score.length; i++){
			   
			newIds.push(data.score[i].scoreId);
			   
			var score = $("#template_latest_score").clone().attr("id","").appendTo(obj.containerId);
			//score.find(".score_rank").html(data.score[i].position);
			score.find(".score_image").attr("src","images/avatars/playerpics" + data.score[i].picture + ".png");
			score.find(".score_name").html(data.score[i].userName);
			score.find(".score_name").attr("href","#arena_stats_" + data.score[i].userId);
			//score.find(".score_link").attr("href","?action=profile&userId=" + data.score[i].userId);			   
			//score.find(".score_countryName").html(data.score[i].countryName);
			score.find(".score_score").html(data.score[i].score);
			score.find(".score_countryFlag").attr("src", "images/flags/" + data.score[i].country + ".gif");
			score.find(".game_name").html(data.score[i].gameName);
			score.find(".game_name").attr("href","#arena_game_" + data.score[i].gameId)
			score.find(".score_message").html(data.score[i].msg);
			
			msgDate = data.score[i].dateTime.split(" ");
			msgTime = msgDate[1];
			msgDate = msgDate[0].split("-");
			msgTime = msgTime.split(":");
			d = new Date();
			d.setUTCFullYear(msgDate[0]);
			d.setUTCMonth(msgDate[1]-1);
			d.setUTCDate(msgDate[2]);
			d.setUTCHours(msgTime[0]);
			d.setUTCMinutes(msgTime[1]);
			d.setUTCSeconds(msgTime[2]);
			score.find(".score_dateTime").html(d.toString());	
			
			if (data.score[i].userId == userId){
				score.find(".highlightable").addClass("highlighted");
			}

			if (obj.oldIds != null && !(data.score[i].scoreId in inar(obj.oldIds))){
				
				console.log("New score!");
				score.css("display","");															
				score.find(".score_score").effect("pulsate",{},500,function(){$(this).effect("pulsate",{},500,null);});
				score.find(".score_image").effect("pulsate",{},500,function(){$(this).effect("pulsate",{},500,null);});				
			} else {
				score.css("display","");				
			}
		}
		
		obj.oldIds = newIds; //Keep a log of ids, so we can distinguish new ones		

		//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);
		});
		
		obj.loaded();
		
		if ($(obj.containerId).size() > 0) {
			window.setTimeout(obj.timerCallbackFunc(obj), 10000);
		} else {
			console.log("LatestScores: " + obj.title + " - closed, ending update timer");
		}			
	}
}
