/**
 * Countries
 */
function Countries(){

	Box.call(this);
}

Countries.prototype = new Box;
Countries.prototype.constructor = Countries;
Countries.prototype.icon = "boxicons/countries.png";

Countries.prototype.addContent = function(){
	
	this.getCountries();
}

Countries.prototype.getCountries = function(obj){

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

Countries.prototype.jqueryGameDetailsCallbackFunc = function(obj){
	
	return function(data){
			
		$(obj.showId + " .box_title_text").html("Top Countries:<br/>" + data.game[0].gameName);
	}
}

Countries.prototype.jqueryCountriesCallbackFunc = function(obj){
	
	return function(data){
	
		for (i = 0; i < data.country.length; i++){
			   
			   score = $("#template_country_scores_score").clone().css("display","").appendTo(obj.containerId);
			   score.find(".score_rank").html(data.country[i].position);
			   score.find(".score_countryName").html(data.country[i].countryName).attr("href","?action=countryScores&country=" + data.country[i].country + "&countryName=" + data.country[i].countryName);
			   
			   score.find(".score_players").html(data.country[i].players);
			   score.find(".score_score").html(data.country[i].score);
			   score.find(".score_countryFlag").attr("src", "images/flags/" + data.country[i].country + ".gif");
		   }
		   
		//Add / re-add click functionality
		var gameId = obj.gameId;
		$(obj.containerId).find(".score_countryName").unbind('click').click(function(event){
			
			event.preventDefault();
			var scores = new Scores;
			scores.setOwner(obj).setType("top") .setGameId(gameId).setCountry(piece($(this).attr("href"),"country")).setCountryName(piece($(this).attr("href"),"countryName"));
			obj.addChild(scores);			
		});			  

		obj.icon = "boxicons/resize/games/game" + obj.gameId + ".png";
	
		obj.loaded();		
	}
}
