REAL.Form = (function () {

	// Private variables
	var _form, _submit, _msg;


	// Public variables
	var obj					= {};


	// Private methods
	function init (element, submit, msg) {

		_form	= $(element);
		_submit	= $(submit);
		_msg	= $(msg).css({opacity:0});

		addPlaceholder();
		addSubmitForm();
	}

	function addPlaceholder (element) {
		// placeholder >> H5F by Ryan Seddon > http://github.com/ryanseddon/H5F
		H5F.setup(_form.eq(0),{
			validClass: "valid",
			invalidClass: "invalid",
			requiredClass: "required"
		});
	}

	function addSubmitForm (element) {
		// submit del formulario

		$('#form-newsletter').submit(function() {


			if (onFormValidate()){
				// cambiamos el action y cargamos
				var pageURL = "http://dxmail.dxmail.be/dxmail/service.php?email=" + $("#email").val() + "&maillist=d933ec1e89a6fed27987a5d3de2cd3da&gr=db2710f";

				// escribir un iframe con la llamada a la página
				$('<iframe />', {
					name: 'mailIframe',
					id: 'mailIframe',
					src: pageURL
				}).appendTo('body');


				onFormSent("Suscription sent");
			}

			return false;
		});
	}

	function onFormSent (data, status, xhr, $form) {
		setTimeout(function(){
			_msg.css({opacity:0}).html(data).stop().animate({opacity:1}, "fast").delay(2000).animate({opacity:0}, "slow", function(){
				_msg.html("");
				_submit.fadeIn("slow");
			});
		}, 1000);
	}

	function onFormValidate (arr, $form, options) {
		var valid = _form[0].checkValidity();
		if (!valid) {
			_submit.fadeOut("fast");
			_msg.html("Incorrect E-mail").stop().animate({opacity:1}, "fast").delay(2000).animate({opacity:0}, "slow", function(){
				_msg.html("");
				_submit.fadeIn("slow");
			});
			return false;
		} else {
			_msg.html("Sending").stop().animate({opacity:1}, "fast");
			_submit.fadeOut("slow");
			return true;
		}
	}


	// Public methods
	obj.init	= init;

	return obj;

})();

