var crazyform = {
	elements	:	null,
	init		:	function(){
		if( jQuery('.node-type-page .contact-form form').length )
		{
			crazyform.elements = jQuery('.node-type-page .contact-form .box');
			jQuery('.node-type-page .contact-form .end-box button').focus(function(){
				jQuery(this).blur();
			});

			var tmp = 0;
			crazyform.elements.each(function(){

				if( tmp != 0 )
				{
					jQuery(this).hide();
				}
				else{
					jQuery(this).find('label').hide();
				}

				if ( jQuery(this).find('input').val() != '' )	jQuery(this).find('label').addClass('focused');

				tmp = tmp+1;
			});
			jQuery(crazyform.elements[0]).css({
				marginTop : 400
			}).animate({
				marginTop : 0
			}, 600, 'easeInOutQuint', function(){
				jQuery('.node-type-page .contact-form .end-box button').attr('data-oldtext', jQuery('.node-type-page .contact-form .end-box button').text() );
				jQuery('.node-type-page .contact-form .end-box button').html('+ &nbsp; &nbsp; &nbsp;' + jQuery('.node-type-page .contact-form .end-box button').attr('data-newtext') );

				if( jQuery(this).find('input').val() == '' ) jQuery(this).find('label').show().jTypeWriter({sequential:false});
				else jQuery(this).find('label').addClass('focused').show();

				jQuery(this).find('input').addClass('passed').addClass('highlighted');
				

				system.bodyAdapt();
			});
			crazyform.elements.each(function(){
				var myel = null;
				var myelval = null;
				if( jQuery(this).find('.text').length )
				{
					myel = jQuery(this).find('.text input');
					myelval = jQuery(this).find('.text input').val();
				}
				else if( jQuery(this).find('.textarea').length )
				{
					myel = jQuery(this).find('.textarea textarea');
					myelval = jQuery(this).find('.textarea textarea').val();
				}
				jQuery.data(myel, 'oldtext', myelval);



				jQuery(this).find('input').bind(
					{
						'focus'		:	function(){
							jQuery(this).parent().children('label').addClass('focused');
							jQuery(this).parent().parent().show();
						},
						'blur'		:	function(){
							if( jQuery(this).val() == '' )
							{
								if( jQuery(this).hasClass('highlighted') )
								jQuery(this).parent().children('label').removeClass('focused');
							}
							else
							{
								//console.log('esegui altra azione');
							}
						}
					}
				);
			});

			jQuery('.contact-form form').submit(function(){

				var isExecutable = false;
				if ( jQuery('.node-type-page .contact-form .box .input .highlighted').length )
				{
					if( crazyform.validateField( jQuery('.node-type-page .contact-form .box .input .highlighted') ) )
							crazyform.blurElement( jQuery('.node-type-page .contact-form .box .input .highlighted').parent().parent() );
					else	return false;
				}
				var tmp = 1;

				crazyform.elements.each(function(){
					isExecutable = true;
					var lastEl = ( crazyform.elements.length == tmp ) ? true : false;
					if( jQuery(this).find('input').length )
					{
						if ( !jQuery(this).find('input').hasClass('passed') )
						{
							isExecutable = false;
							crazyform.focusElement(jQuery(this),lastEl);
							return false;
						}
						else isExecutable = true;
					}
					else if( jQuery(this).find('textarea').length )
					{
						if ( !jQuery(this).find('textarea').hasClass('passed') )
						{
							isExecutable = false;
							crazyform.focusElement(jQuery(this),lastEl);
							return false;
						}
						else isExecutable = true;
					}

					tmp++;
				});


				if( !isExecutable ) return false;
				else{

					jQuery.ajax({
						type: 'POST',
						url: jQuery(this).attr('action'),
						data: jQuery(this).formSerialize(),
						async : false,
						success: function(datas){
							jQuery('.node-type-page .node-inner .node-subtitle').hide();
							jQuery('.contact-form').css('background','none');
							system.bodyAdapt();
							jQuery('.contact-form').html( datas );
							system.bodyAdapt();
						},
						dataType: 'html'
					});
					return false;
				}
			});
		}
	},
	focusElement	:	function(el,lastel)
	{
		if ( jQuery(el).length )
		{
			if( !jQuery(el).find('textarea').length )
			{
				jQuery(el)
					.show()
					.animate({height: 130}, 100, 'easeInOutQuint', function(){

					})
					.find('input')
						.animate({
							'height'		:	118,
							'font-size'	:	72,
							'line-height'	:	122
						}, 100, 'easeInOutQuint', function(){
							jQuery(this).addClass('highlighted').addClass('passed');
							system.bodyAdapt();
						});
			}
			else
			{
				jQuery(el)
					.show()
					.find('textarea')
						.addClass('highlighted passed');
			}
		}
		if( lastel )
		{
			jQuery('.node-type-page .contact-form .end-box button').html( jQuery('.node-type-page .contact-form .end-box button').attr('data-oldtext') );
		}
	},
	blurElement		:	function(el)
	{
		if ( jQuery(el).length )
		{
			if( !jQuery(el).find('textarea').length )
			{
				jQuery(el)
					.animate({height: 40}, 100, 'easeInOutQuint', function(){

					})
					.find('input')
						.animate({
							'height'		:	36,
							'font-size'	:	18,
							'line-height'	:	36
						}, 100, 'easeInOutQuint', function(){
							jQuery(this).removeClass('highlighted');
							system.bodyAdapt();
						})
					.parent().find('label')
						.addClass('focused');
			}
			else
			{
				jQuery(el)
					.animate({height: 40}, 100, 'easeInOutQuint', function(){

					})
					.find('textarea')
						.removeClass('highlighted')
					.parent().find('label')
						.addClass('focused');
			}
		}
	},
	validateField	:	function( el ){
		if( jQuery(el).length )
		{
			if( jQuery(el).parent().parent().children('.input').hasClass('required') )
			{
				if( !crazyform.checkField(jQuery(el).parent().parent()) )
				{
					jQuery(el).parent().find('label').effect("shake", {times:2}, 100);
					return false;
				}
				else return true;
			}
			else return true;
		}
	},
	checkField		:	function( el )
	{
		if( jQuery(el).length )
		{

			if( jQuery(el).find('.text').length )
			{

				if( jQuery(el).find('.text input').hasClass('email-input') )
				{
					var emailPattern = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
					if( !emailPattern.test( jQuery.trim(jQuery(el).find('.text input').val()) ) ) return false;
				}
				if ( jQuery.trim(jQuery(el).find('.text input').val()) != '' ) return true;
				else return false;
			}
			else if( jQuery(el).find('.textarea').length )
			{
				if ( jQuery.trim(jQuery(el).find('.textarea textarea').val()) != '' ) return true;
				else return false;
			}
			else return true;
		}
		return true;
	}
};

jQuery(document).ready(function(){
	var intID = window.setInterval(function(){
		if( fontloaded )
		{
			window.clearInterval(intID);
			crazyform.init();
		}
	}, 200);
});
