

$(document).ready(function(){
	//global vars
	var inputMessage = $("#message");
	var loading = $("#loading");
	var messageList = $(".content > ul");
	
	//functions
	function updateShoutbox(){
		//just for the fade effect
		messageList.hide();
		loading.fadeIn();
		//send the post to shoutbox.php
		var date = (new Date()).getTime();
		$.ajax({
			type: "POST", url: "js/tribune.php?p="+date, data: "action=update",
			complete: function(data){
				loading.fadeOut();
				messageList.html(data.responseText);
				messageList.fadeIn(2000);
			}
		});
	}
	//check if all fields are filled
	function checkForm(){
		if(inputMessage.attr("value"))
			return true;
		else
			return false;
	}
	
	//Load for the first time the shoutbox data
	updateShoutbox();
		
	$("#form").submit(function(){
		if(checkForm()){
			var message = inputMessage.attr("value");
			$("#message").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: "js/tribune.php", data: "action=insert&message=" + message,
				complete: function(data){
					messageList.html(data.responseText);
					updateShoutbox();
					//reactivate the send button
					$("#send").attr({ disabled:false, value:"Envoyer" });
				}
			 });
		}
		else alert("Saisissez un message !");
		//we prevent the refresh of the page after submitting the form
		return false;
	});
});
$(document).ready(function(){
	$(".modification_date_match").click(function() {$("#form_modification_date_match").slideToggle(0);});
});

