/**
 * Shouts
 */
function Shouts(){
	
	Box.call(this);
	eventhook.addListener(this,"loggedIn");
}

Shouts.prototype = new Box;
Shouts.prototype.constructor = Shouts;
Shouts.prototype.icon = "boxicons/shouts.png";
Shouts.prototype.template = "#template_latestshouts";

Shouts.prototype.addContent = function(){

	var obj = this;		
	
	//Shout submit button js
	$(obj.containerId).find(".shout_submit_button").click(function(event){
		
		event.preventDefault();
		$(obj.containerId).find(".shout_submit_button").attr("disabled","true");
		$(obj.containerId).find(".shout_submit_text").attr("disabled","true");
		
		$.getJSON("interface/json.php",{action: "submitShout", msg: $(obj.containerId + " .shout_submit_text").val(), gameId: obj.gameId},
				function(data){	
		   
		   $(obj.containerId).find(".shout_submit_button").removeAttr("disabled");
		   $(obj.containerId).find(".shout_submit_text").removeAttr("disabled");
		   $(obj.containerId).find(".shout_submit_text").val("");
		   $.getJSON("interface/json.php",{action: "getShouts", gameId: obj.gameId, starting: obj.starting, number: obj.number},obj.jqueryShoutsCallbackFunc(obj));
	   });
	});
	
	$(obj.containerId).find(".shout_submit_text").keypress(function(e) {
       if(e.which == 13) {
    	   e.preventDefault();
           $(this).blur();
           $(obj.containerId).find(".shout_submit_button").focus().click();
       }
   });  	
	
	//Show shout submit if user is logged in
	if (userId != 0)
		$(obj.containerId).find(".shout_submit").slideDown("slow");
	
	this.getShouts();
};

Shouts.prototype.getShouts = function(obj){

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

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

Shouts.prototype.addShout = function(newShout, mode){

	var obj = this;
	
	if (mode == null)
		mode = "appendTo";

	shout = $("#template_game_shouts_shout").clone().attr("id","").css("display","");
	if (mode == "appendTo")
		shout.appendTo(obj.containerId);
	else
		shout.prependTo(obj.containerId);
	shout.find(".shout_link").attr("href","profile.php?userId=" + newShout.userId);
	shout.find(".shout_name").html(newShout.userName);
	shout.find(".shout_text").html(newShout.msg);
	shout.find(".shout_picture").attr("src","images/avatars/playerpics" + newShout.picture + ".png");
	msgDate = newShout.dateTime.split(" ");
	msgTime = msgDate[1];
	msgDate = msgDate[0].split("-");
	d = new Date(msgDate[0],msgDate[1]-1,msgDate[2]);
	shout.find(".shout_dateTime").html(msgTime + " " + d.toDateString());	
	
	if (newShout.userId == userId){
		shout.find(".highlightable").addClass("highlighted");
	}	
};

Shouts.prototype.jqueryShoutsCallbackFunc = function(obj){
	
	return function(data){
	
		$(obj.containerId).find(".template_game_shouts_shout").remove();
		
		for (i = 0; i < data.shout.length; i++){
			   
			obj.addShout(data.shout[i]);
		}
		
		//Add click functionality
		
		$(obj.containerId).find(".shout_link").click(function(event){
			
			event.preventDefault();
			var newProfile = new Profile;
			newProfile.setOwner(obj).setUserId(piece($(this).attr("href"),"userId"));
			obj.addChild(newProfile);
		});		
		
		obj.loaded();
	};
};

Shouts.prototype.eventCallback = function(eventType){
	
	if (eventType == "loggedIn") {
		$(this.containerId).find(".shout_submit").slideDown("slow");
	}
};
