﻿/***************************/
//@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!					
/***************************/


	//global vars
	var inputUser = $("#nick");
	var inputMessage = $("#shoutmessage");
	var loading = $("#shoutloading");
	var messageList = $(".shoutcontent > ul");
	var timeout = $("#shouttimeout");
	//functions
	function updateShoutbox(){
		//just for the fade effect
		//messageList.hide();
		loading.fadeIn();
		//send the post to shoutbox.php
		$.ajax({
			type: "POST", url: "/inc/forum/shoutbox.php", 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;
	}
	//Hide the timout message
	timeout.fadeOut();
	
	//Load for the first time the shoutbox data
	updateShoutbox();
	//Set auto refresh rate
	reload = setInterval('updateShoutbox()', 10000);
	
	//Stop the refresh after 5 minutes
	function stopShoutRefresh(){
		clearInterval( reload );
		messageList.hide();
		timeout.fadeIn();
	}
	stop = setInterval('stopShoutRefresh()', 360000);
	//restart the shoutbox on click
	function restartShoutbox(){
		timeout.fadeOut();
		messageList.fadeIn();
		reload = setInterval('updateShoutbox()', 10000);
		clearInterval( stop );
		stop = setInterval('stopShoutRefresh()', 360000);
	}
	//on submit event
	$("#form").submit(function(){
	clearInterval( stop );
	stop = setInterval('stopShoutRefresh()', 360000);
		if(checkForm()){
			var nick = inputUser.attr("value");
			var shoutmessage = inputMessage.attr("value");
			//we deactivate submit button while sending
			$("#send").attr({ disabled:true});
			$("#send").blur();
			//send the post to shoutbox.php
			$.ajax({
				type: "POST", url: "/inc/forum/shoutbox.php", data: "action=insert&nick=" + nick + "&shoutmessage=" + shoutmessage,
				complete: function(data){
					messageList.html(data.responseText);
					updateShoutbox();
					
					
					//reactivate the send button
					$("#send").attr({ disabled:false, value:"Shout it!" });
					$('#shoutmessage').val('');
				}
			 });
			 //setfocus
			 inputMessage.focus();
			 
		}
		else alert("Please fill all fields!");
		//we prevent the refresh of the page after submitting the form
		return false;
	});
	



