/**

File: 		uxci-newsletter.js
Version: 	0.1
Date: 		June 3, 2009

*/

/**

Function: 		checkEmail
Description: 	Checks the validity of the submitted email
Parameters: 	(String) email
Return: 		(Boolean)

**/
function checkEmail( email )
{
	if( email != "" ) {
		var filter = /^([a-zA-Z0-9_\+\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		return ( filter.test(email) );
	}
	return false;
}


/**

Function: 		backToTheInput
Description: 	Sets the focus on the email text input and select the text inside
Parameters: 	(Element) obj
Return: 		-

**/
function backToTheInput( obj )
{
	obj.focus();
	obj.select();
}


/**

Function: 		makeActive
Description: 	Sets the the email text input status to 'active'
Parameters: 	(Element) email
Return: 		-

**/
function makeActive( email )
{
	if( email.hasClass("unactive") ) {
		email.removeClass("unactive");
		email.addClass("active");
		email.val("");
	}
}


/**

Function: 		makeUnactive
Description: 	Sets the the email text input status to 'unactive'
Parameters: 	(Element) email, (String) default_text, (Boolean) submitted
Return: 		-

**/
function makeUnactive( email, default_text, submitted )
{
	if( email.val() == "" ) {
		email.addClass("unactive");
		email.removeClass("active");
		if( !submitted )
			email.val(default_text);
	}
}


/**

Function: 		onDocumentReady
Description: 	Binds the events to the elements at startup
Parameters: 	-
Return: 		-

**/
$(document).ready( function() {
	
	// Constants
	var email 			= $("#subscribeform #email");
	var default_text 	= "Inserisci la tua mail";
	var form			= $("#subscribeform");
	var submit			= $("#submit");
	var body			= $("body");
	var clicked			= "";
	
		// Effects
		var speed		= 100;
	
	// Setting the default text for the email field
	email.val(default_text);
	
	// Hover status for clickable elements
	$(".clickMe").mouseover( function() {
		$(this).addClass("cmHover");
	});
	$(".clickMe").mouseout( function() {
		$(this).removeClass("cmHover");
		$(this).removeClass("cmFocus");
	});
	
	// Active status for clickable elements
	$(".clickMe").mousedown( function() {
		$(this).addClass("cmFocus");
	});
	$(".clickMe").mouseup( function() {
		$(this).removeClass("cmFocus");
	});
	
	// Capturing the clicks on the page
	body.mousedown( function(e) {
		clicked = e.target.id;
	});
	
	// Email.onFocus event
	email.focus( function() {
		makeActive( $(this) );
	});
	
	// Email.onBlur event
	email.blur( function() {
		makeUnactive( $(this), default_text, (clicked == submit.attr("id")) );
	});
	
	// Form.onSubmit event
	form.submit( function() {
		$(".nl.msg").slideUp(speed, function() {
			$(this).remove();
		});
		if( checkEmail(email.val()) )
			return true;
		else {
			//submit.after("<p class=\"nl msg error hide\">L’email inserita non è corretta.</p>");
			form.append("<p class=\"nl msg error hide\" style='display: none !important'>L’email inserita non è corretta.</p>");
			$(".nl.msg").slideDown(speed, function() {
				if( email.hasClass("unactive") ) {
					email.removeClass("unactive");
					email.addClass("active");
				}
				backToTheInput( email );
				email.keydown( function() {
					$(".nl.msg").slideUp(speed, function() {
						$(this).remove();
						email.unbind("keydown");
					});
				});
			});
		}
		return false;
	});
	
} );