/***************************/
//@Author: Adrian "yEnS" Mato Gondelle & Ivan Guardado Castro
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

$(document).ready(function(){
	//global vars
	var inputUser = jQuery("#pseudo");
	var inputMessage = jQuery('textarea[name="message"]');
	var loading = jQuery("#loading");
	var messageList = jQuery("#textbox > ul");
	
	//functions
	function updateShoutbox(){
		//just for the fade effect
		messageList.hide();
		loading.fadeIn();
		//send the post to shoutbox.php
		jQuery.ajax({
			type: "POST", url: "shoutbox.php", data: "action=update",
			complete: function(data){
				loading.fadeOut("normal",function(){
				messageList.html(data.responseText).hide();
				messageList.fadeIn(2000,function(){
				messageList.removeAttr('filter');
				});
				jQuery("#reload").attr({src:"images/reload_shoutbox.png"});
					});
			}
		});
	}


	jQuery("#up").mouseover(function(){
	arretDefil();
	transfertArriere(50);
	});;

	jQuery("#up").mouseout(function(){
	arretDefil();
	});;


	jQuery("#down").mouseover(function(){
	arretDefil();
	transfertPlus(50);
	});;

	jQuery("#down").mouseout(function(){
	arretDefil();
	});;




	
	//check if all fields are filled
	function checkForm(){
		if(inputUser.attr("value") && inputMessage.val() &&  jQuery("#secret_shoutbox").attr("value") )
			return true;
		else
			return false;
	}
	
	//Load for the first time the shoutbox data
	updateShoutbox();
	// setInterval(updateShoutbox, 60000);
	
	jQuery("#reload").click(function(){
		jQuery("#reload").attr({src:"images/reload_shoutbox_gif.gif"});
		updateShoutbox();
	});
	
	//on submit event
	jQuery("#form").submit(function(){
		if(checkForm()){
			var nick = inputUser.attr("value");
			var message = inputMessage.val();
			var secret_shoutbox = jQuery("#secret_shoutbox").attr("value");
			//we deactivate submit button while sending
			jQuery("#send").attr({ disabled:true, value:"Envoie..." });
			jQuery("#send").blur();
			//send the post to shoutbox.php
			jQuery.ajax({
				type: "POST", url: "shoutbox.php", data: "action=insert&nick=" + nick + "&message=" + message + "&secret_shoutbox=" + secret_shoutbox,
				complete: function(data){
					
					if (data.responseText == "error")
						{
							jQuery("#send").attr({ disabled:false, value:"envoyer" });
							alert("Code anti-spam incorrecte");
							return false;
						}
					else
					{
					messageList.html(data.responseText);
					updateShoutbox();
					inputUser.attr({ value:""});
					id_unique = 1 + Math.floor((Math.random()*32767));
					jQuery("#img_anti_spam_shoutbox").attr({ src: "anti_spam.php?form=shoutbox&amp;id="+id_unique});
					secret_shoutbox = jQuery("#secret_shoutbox").attr({ value:""});
					inputMessage.val("");
					//reactivate the send button
					jQuery("#send").attr({ disabled:false, value:"envoyer" });
					}
				}
			 });
		}
		else alert("Veuillez remplir tous les champs");
		//we prevent the refresh of the page after submitting the form
		return false;
	});
});
