var valDefault = '';
var is_ready = true;

function resize()
{
	var _maxHeight = $(document).height();
	var _globalHeight = $('#global').height();
	
	var _top = (_maxHeight-_globalHeight)/2;
	$('#global').css({top:_top+'px'});
}

function manage_input()
{
	valDefault = $("input.text").val();
	$("input.text").click(function(){
		var currentValue = $(this).val();
		if(currentValue == valDefault) $(this).val("");
		
		$(this).blur(function(){
			if($(this).val() == "")
			{
				$(this).val(valDefault);
			}
		});
	});
}

function animation()
{
	$('#global').fadeIn(400);
}

function send_email()
{
	$('.send').click(function(){
		return ajax_request();
	});
}

function ajax_request()
{
	var _currentValue = $('#email').val();
	
	if(is_ready == true)
	{
		is_ready = false;
		
		$.ajax({
			type:'POST',
			url:'ajax/ajax.send_mail.php',
			data:{email:_currentValue},
			success:function(_html)
			{
				callback(_html);
			}
		});
		return false;
	}}

function callback(_html)
{
	if(_html == 'ok')
	{
		$('#email').css({opacity:0});
		$('#email').val("C'est enregistré !");
		$('#email').animate({opacity:1},400,function(){
			//
			setTimeout("init_input()",2000);
		});
	}
	else
	{
		$('#email').css({opacity:0});
		$('#email').val("Erreur sur le courriel !");
		$('#email').animate({opacity:1},400,function(){
			//
			setTimeout("reset_input()",1500);
		});
	}
}
function init_input()
{
	$('#global').css({display:'none'});
	$('#email').val(valDefault);
	animation();
	is_ready = true;
}
function reset_input()
{
	$('#email').animate({opacity:0},400,function(){
		$('#email').val(valDefault);
		//$('#email').css({opacity:1});
		$('#email').animate({opacity:1},400);
		is_ready = true;
	});
}

$(document).ready(function(){
	resize();
	animation();
	manage_input();
	send_email();
});

$(window).resize(function(){
	resize();
});