window.addEvent('domready', function(){
    setNL();
});


function setNL(){
	var el = $('tabNewsletter');
	el.closed = true;
	el.addEvent('click', function(){
		if(this.closed){
			openNL(this);
		}else{
			closeNL(this);
		}
		return false;
	});
	$('mailNl').firstClick = 0;
	$('mailNl').addEvent('click', function(){
		if(this.firstClick <= 0){
			this.set('value', '');
			this.firstClick++;
		}
	});
	
	
	var req = new Request({
		url: 'newsletter/check.php',
		onSuccess: function(fb){
			showFeedback(fb);
		},
		onFailure: function(){
			$('feedback').set('html', '<p class="fb_error">Richiesta fallita<br>Prova pi&ugrave; tardi</p>');
		}

	});
	
	$('submintNl').addEvent('click', function(){
		var mail = $('mailNl').get('value');
		req.send({data: 'email=' + mail + '&action=sub'});
		return false;
	});
}

function openNL(el){
	$('tabNewsletter').setStyle('background-position', '0 0');
	$('entryBox').set('tween', {duration: 500});
	$('entryBox').tween('height', 384);
	el.closed = false;	
}

function closeNL(el){
	$('tabNewsletter').setStyle('background-position', '0 -27px');
	$('entryBox').set('tween', {duration: 500, onComplete:function(){
		$('mailNl').set('value', 'Your e-mail here');
		$('feedback').set('html', '');														   
		}
	});
	$('entryBox').tween('height', 461);
	el.closed = true;
	$('mailNl').firstClick = 0;
}

function showFeedback(fb){
	var feedback = "";
	switch(Number(fb))
		{
		case 0:
		  feedback = '<p class="fb_error">L&rsquo;indirizzo inserito non &egrave; un indirizzo valido. Controlla e riprova.</p>'
		  break;
		case 1:
		  feedback = '<p class="fb_ok">Il tuo indirizzo &egrave; stato aggiunto con successo, a breve riverai una email di conferma registrazione.</p>'
		  break;
		case 2:
		  feedback = '<p class="fb_exist">L&rsquo;indirizzo inserito &egrave; gia presente nel nostro elenco.</p>'
		  break;
		
		}
	$('feedback').set('html', feedback);
}


