
/**
* author: whizzer
* filename: shout.js
* This file is part of the "AJAX Shoutbox" project written and sold ONLY on 
* codecanyon.net.
* This file reloads the shoutbox and handles the sending of new shouts.
*/


//minimum length of the username field
var uname_minlength = 3;

//maximum length of the username field
var uname_maxlength = 20;

//minimum length of the message field
var message_minlength = 3;

//maximum length of the message filed (twitter-style)
var message_maxlength = 140;

/* IMPORTANT: If you want to change the values above you should also change them in the chat.php file. There are checks
in the php file in case one has javascript disabled. Changing the values here only will have NO effect. */

// the chat will be updated every *refreshSeconds* milliseconds
var refreshSeconds = 10000;

//execute javascript when the document is loaded
$(document).ready(function() {

	//global variables
	var username_div = $("#username");
	var message_div = $("#message");
	
	//remove the default value when clicked
	username_div.click(function() {
		if(username_div.val() == 'Username')
		{
			username_div.val('');
		}
	});
	
	//set the default value again if no username entered
	username_div.blur(function() {
		if(username_div.val() == '')
		{
			username_div.val('Username');
		}
	});
	
	//remove the default value when clicked
	message_div.click(function() {
		if(message_div.val() == 'Message')
		{
			message_div.val('');
		}
	});
	
	//set the default value again if no message entered
	message_div.blur(function() {
		if(message_div.val() == '')
		{
			message_div.val('Message');
		}
	});	
	
	//the submit button is clicked
	$("#send").click(function(eve) {
		//prevent the form from submitting
		eve.preventDefault();
		
		//internal variables
		var username = username_div.val();
		var message = message_div.val();
		var errors = new Array();
		var errors_div = $("#errors");
		var username_length = username.length;
		var message_length = message.length;
		
		//make the message input blank
		$("#message").val('');
		
		//hide the errors div in case it was previously shown
		if(errors_div.css("display") != "none")
		{
			errors_div.hide();
		}
		
		//username and message length checks
		if(username_length < uname_minlength)
		{
			errors[0] = "Username is too goddamn short.";
		}
		if(username_length > uname_maxlength)
		{
			errors[errors.length] = "Username is way too fucking long.";
		}
		if(message_length < message_minlength)
		{
			//allow messages less than 3 characters only if they are smilies
			if((message_length == 0) || ((message_length != 0) && (message != message.toUpperCase())))
			{
				errors[errors.length] = "Message is too short.";
			}
		}
		if(message_length > message_maxlength)
		{
			errors[errors.length] = "Message is too long.";
		}
		
		if(username == 'Username' && message == 'Message')
		{
			//don't submit the form if nothing is changed (the default username and message)
			return;
		}

		//are there errors
		if(errors.length > 0)
		{
			//display the errors
			errors_div.html(errors.join(" ")).fadeIn("slow");
		}
		else
		{
			$.post("index.php", {username : username, message : message},
			function(data)
			{
				//if an error message is returned
				if(data.substr(0, 5) == 'error')
				{
					errors_div.html(data.substr(5)).fadeIn("slow");
				}
				refreshChat();			
			}
			);
			
		}		
	});
	
	
	function refreshChat()
	{
		//speed up by selecting the div only once
		var shoutbox = $("#shout_messages");
		
		//get the height of the scroll (if any)
		var oldScrollH = shoutbox.attr("scrollHeight") - 20;
		
		//the ajax request
		$.ajax({
		url: 'update.php',
		//disable cache
		cache: false,
		success: function(html) {
			//update the shoutbox
			shoutbox.html(html);
			//get the heigth of the scroll after the update
			var newScrollH = shoutbox.attr("scrollHeight") - 20;
			if(newScrollH > oldScrollH)
			{
				//*move* the scroll down using an animation :)
				shoutbox.animate({scrollTop: newScrollH}, 1000);
			}
		}		
		});
	}
	
	//set the refreshChat function to run every *refreshSeconds*
	refreshChat();
	setInterval(refreshChat, refreshSeconds);
	
});

//insert smileys into the shoutbox message input
function shout_insert(smily)
{
	var shout_input = $('#message');
	
	if(shout_input.val() == 'Message')
	{
		//remove the default value of the message field
		shout_input.val('');
	}
	
	//set a new value which is the old value + the smily
	shout_input.val(shout_input.val() + ' ' + smily + ' ');
}
