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

LatestShouts.prototype = new Box;
LatestShouts.prototype.constructor = LatestShouts;
LatestShouts.prototype.icon = "boxicons/shouts.png";
LatestShouts.prototype.title = "Latest Shouts";
LatestShouts.prototype.oldIds = null;

LatestShouts.prototype.addContent = function(){
	
	this.getLatestShouts();
}

LatestShouts.prototype.getLatestShouts = function(obj){

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

LatestShouts.prototype.timerCallbackFunc = function(obj){
	
	return function(){
		
		console.log("LatestShouts: " + obj.title + " - getting latest shouts");		 
		$.getJSON("interface/json.php",{action: "getShouts", gameId: obj.gameId, starting: obj.starting, number: obj.number},obj.jqueryLatestShoutsCallbackFunc(obj));	
	}
}

LatestShouts.prototype.jqueryLatestShoutsCallbackFunc = function(obj){
	
	return function(data){
	
		$(obj.containerId).empty();
		
		newIds = new Array;	
	
		for (i = 0; i < data.shout.length; i++){
			   
			newIds.push(data.shout[i].id);

			shout = $("#template_latestshouts_shout").clone().attr("id","").css("display","").appendTo(obj.containerId);
			shout.find(".shout_link").attr("href","profile.php?userId=" + data.shout[i].userId);
			shout.find(".shout_name").html(data.shout[i].userName);
			shout.find(".shout_name").attr("href","#arena_stats_" + data.shout[i].userId);
			shout.find(".shout_text").html(data.shout[i].msg);
			shout.find(".shout_picture").attr("src","images/avatars/playerpics" + data.shout[i].picture + ".png");
			
			msgDate = data.shout[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]);
						
			shout.find(".shout_dateTime").html(d.toString());	
			shout.find(".shout_countryFlag").attr("src", "images/flags/" + data.shout[i].country + ".gif");		
			shout.find(".shout_gameName").html(data.shout[i].gameName);
			shout.find(".shout_gameName").attr("href","#arena_game_" + data.shout[i].gameId)
	
			
			if (data.shout[i].userId == userId){
				shout.find(".highlightable").addClass("highlighted");
			}	
			
			if (obj.oldIds != null && !(data.shout[i].id in inar(obj.oldIds))){
				
				console.log("New shout!");
				shout.css("display","");															
				shout.find(".shout_text").effect("pulsate",{},500,function(){$(this).effect("pulsate",{},500,null);});
				shout.find(".shout_picture").effect("pulsate",{},500,function(){$(this).effect("pulsate",{},500,null);});				
			} else {
				shout.css("display","");				
			}					
		}
		
		obj.oldIds = newIds; //Keep a log of ids, so we can distinguish new ones			
		
		obj.loaded();
		
		if ($(obj.containerId).size() > 0) {
			window.setTimeout(obj.timerCallbackFunc(obj), 10000);
		} else {
			console.log("LatestShouts: " + obj.title + " - closed, ending update timer");
		}			
		
	}
}
