/***************************/
//@Author: Adrian "yEnS" Mato Gondelle & Ivan Guardado Castro
//@website: www.yensdesign.com
// Modifs: webmaster@teamboo.net
/***************************/

$(document).ready(function(){
	//global vars
	var inputUser = $("#nick");
	var inputMessage = $("#message");
	var loading = $("#loading");
	var messageList = $(".content > ul");
	var smiley = $("#smiley");
	var tribune = $("#tribcontainer");

	function updateShoutbox(){
	
		smiley.hide();
		messageList.hide();
		loading.fadeIn();		
		//tribune.hide("clip", { direction: "vertical" }, 2000);

		
		//send the post to shoutbox.php
		$.ajax({
			type: "POST", url: "http://www.teamboo.net/site/shoutbox.php", data: "action=update",
			complete: function(data){
				//tribune.show("clip", { direction: "vertical" }, "3000);
				loading.hide();
				messageList.html(data.responseText);
				messageList.fadeIn(1500);
				smiley.fadeIn(1500);
				
			}
		});
	}
	
	
	//check if all fields are filled:
	function checkForm(){
		if(inputUser.attr("value") && inputMessage.attr("value"))
			return true;
		else
			return false;
	}
	
	
	//Load for the first time the shoutbox data:
	updateShoutbox();
	
	
	//on submit event:
	$("#form").submit(function(){
		if(checkForm()){
			var nick = inputUser.attr("value");
			var message = inputMessage.attr("value");
			
			//we deactivate submit button while sending
			$("#send").attr({ disabled:true, value:"Envoi..." });
			$("#send").blur();
			//send the post to shoutbox.php
			$.ajax({
				type: "POST", url: "http://www.teamboo.net/site/shoutbox.php", data: "action=insert&nick=" + nick + "&message=" + message,
				complete: function(data){
					messageList.html(data.responseText);
					updateShoutbox();
					//reactivate the send button
					$("#send").attr({ disabled:false, value:"Poster" });
					$("#message").attr({ disabled:false, value:"" });
				}
			 });
		}
		else alert("Remplissez tous les champs");
		//we prevent the refresh of the page after submitting the form
		return false;
	});

});
