﻿// JavaScript Document
var _duration = 350;
var g_duration = 350;
var s_t;

function initPopup(_ind)
{
	var _shadow = $('#overlay');
	var _h = Math.max (
					   $(document).height(),
					   $(window).height(),
					   /* For opera: */
					   document.documentElement.clientHeight
					   );
	s_t = document.documentElement.scrollTop;
	
	if(_ind > 0){ _shadow.css({'height': _h, 'display': 'block'}).animate({opacity:0.93}, _duration);}
	else if(_ind < 0){ _shadow.animate({opacity:0}, _duration, function(){ $(this).css('display', 'none')});}
	else{_shadow.css({'display': 'none', 'opacity': 0});}
}

var form_error, str_h3, str_h3_ok, str_st, str_st_ok, str_h3_no, str_st_no, str_h3_ml, str_st_ml;

function initForm()
{
	var _hold  = $('#form-block').css('display', 'none');
	var f_form = $('.form', _hold).css('display', 'none');
	var t_form = $('.thank', _hold).css('display', 'none');
	initPopup();

	// OPEN
	$('.btn2-send1').click(function(){
		initPopup(1);
		_hold.css('display', 'block');
		_hold.css('top', s_t + "10%");
		f_form.fadeIn(_duration);
		return false;
	});

	// SEND
	$('#btn-send', f_form).hover ( function(){ $(this).addClass('hover');}, function(){ $(this).removeClass('hover'); }).click ( function()
	 {
		 var name1 = $('#name1').val();
		 var mail1 = $('#mail1').val();
		 var name2 = $('#name2').val();
		 var mail2 = $('#mail2').val();

		 form_error = 0;
		 str_h3 = str_h3_ok = "Obrigado pela sua recomendação!";
		 str_st = str_st_ok = "A sua mensagem foi enviada com sucesso.";
		 str_h3_no = str_h3_ml = "Atenção!";
		 str_st_no = "Todos os campos são de preenchimento obrigatório. Devem conter no mínimo três caracteres.";
		 str_st_ml = "Verifique se inseriu correctamente os endereços de E-mail.";

		function validateEmail ( str )
		{
			var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
			return regex.test(str);
		}

		// VALIDATE ALL FIELDS VALUE LENGTH
		if ( name1.length<3 || mail1.length<3 || name2.length<3 || mail2.length<3 ) { form_error=1; str_h3=str_h3_no; str_st=str_st_no; }

		// VALIDATE EMAILS
		if (form_error==0) if ( !validateEmail(mail1) || !validateEmail(mail2) ) { form_error=1; str_h3=str_h3_ml; str_st=str_st_ml; }

		$('div.thank h3').text(str_h3);
		$('div.thank strong').text(str_st);

		if (form_error==0)
		{
			var values = { name1:name1, mail1:mail1, name2:name2, mail2:mail2, title:$('#send-friend-title').val(), excerpt:$('#send-friend-excerpt').val(), url:window.location.href, type:$('#send-friend-type').val() };

			// SUBMIT FORM
			$.ajax (
			{
				type:    "post",
				url:     $('#sendFriend').attr('action'),
				data:    values,
				success: function ( result )
				{
					// alert (result);
					switch (result)
					{
						case '1':
							str_h3 = str_h3_ok;
							str_st = str_st_ok;
							form_error = 0;
							//_gaq._trackPageView( "livros-recomendados", "Recomendação de ", $('#send-friend-title').val() );
						break;
						case '-4':
						case '-5':
							str_h3 = str_h3_ml;
							str_st = str_st_ml;
							form_error = 1;
						break;
						case '0':
						default:
							str_h3 = "Pedimos desculpa.";
							str_st = "Ocorreu um erro com o envio da sua mensagem. Por favor tente mais tarde.";
							form_error = 1;
						break;
					}
					$('div.thank h3').text(str_h3);
					$('div.thank strong').text(str_st);
					f_form.fadeOut ( _duration, function() { t_form.fadeIn(_duration); });
				}
			});
		} else { f_form.fadeOut ( _duration, function() { t_form.fadeIn(_duration); }); }

		return false;
	});

	// WARN
	$('a.btn-back', _hold).click ( function()
	{
		t_form.fadeOut ( _duration, function() { if (form_error==0) resetFormSendFriend (); f_form.fadeIn(_duration); });
	});

	// CLOSE
	$('a.btn-close', _hold).click( function()
	{
		if(t_form.is(':visible')) t_form.fadeOut(_duration, function(){ _hold.css('display', 'none');});
		if(f_form.is(':visible')) f_form.fadeOut(_duration, function(){ _hold.css('display', 'none');});
		if (form_error==0) resetFormSendFriend ();
		initPopup(-1);
		return false;
	});
}

function resetFormSendFriend () {
	$('#sendFriend input[type="text"]').each(function (index,domEle){ $(this).val('');});
}

if (window.addEventListener){
	window.addEventListener("load", initForm, false);
}
else if (window.attachEvent && !window.opera){
	window.attachEvent("onload", initForm);
}
