$(document).ready(function () {
	//Fondo en flash
/*
	$('#bg').flash({
		swf: path+'flash/if/_bg.swf'	,width: '100%'	,height: '100%'	,hasVersion: 9,
		params: { menu: 'false', wmode: 'transparent', scale: 'exactfit' }
	});
*/
	$('#logo').html('').flash({
		swf: path+'flash/if/logo3d.swf',	width: '300',	height: '190',	hasVersion: 9, wmode: 'transparent'
		,params: { menu: 'false', wmode: 'transparent', scale: 'noscale' }
	});
	
	//$.nyroModalSettings();
	$('a[rel=lightbox]').nyroModal();
	
//	$.sifr({
//		path: 'fonts/'		,save: true
//	});
//	$('h2').sifr({ font: 'mgopenmodatabold' });
//	$('h2').sifr({ font: 'cocktail' });
//	$('h2').sifr({ font: 'Gunmetal', sifrVersion: 3, zoom: 1 });



	//Hover en idiomas *************************
	$("#idiomas img[class!=sel]").hover(function() {
		$(this).animate({opacity: "1"}, "fast");
	}, function() {
		$(this).animate({opacity: "0.5"}, "fast");
	});
	$("#idiomas img[class!=sel]").css({opacity: '0.5'}, 1);
	


	//MAILING *********************************


	
	var newsFormReset = function(){
		$('#f_email').val('E-mail');
		$('#f_sub').attr('disabled', false);
		$('#f_lista').attr('checked',false);
		$('#f_listagay').attr('checked',false);
		$('#f_condiciones').attr('checked',false);
	}

	$('#mailingMsg').fadeOut(0);
	newsFormReset();
	
	
	$('#f_sub').click(function(e){
		$('#f_sub').attr('disabled','disabled');
		e.stopPropagation();
		e.preventDefault();

		//var listaVal = $("#newsletter #f_listas").val();
		var textos_es = {
			"OK": 			"OK! Tu e-mail ha sido añadido correctamente!",
			"ERR_CONFIRM":	"<b>Error</b>. No se pudo enviar confirmacion",
			"ERR_SYSTEM":	"<b>Error</b>. Fallo conectando con el servidor de listas",
			"ERR_ALREADY":	"<b>Error</b>. El e-mail ya está en la lista",
			"ERR_ALREADY1":	"<b>Error</b>. El e-mail ya está en la lista Matinee News",
			"ERR_ALREADY2":	"<b>Error</b>. El e-mail ya está en la lista Matinee Gay News",
			"ERR_ALREADY12":"<b>Error</b>. El e-mail ya está en ambas listas",
			"ERR_BADEMAIL":	"<b>Error</b>. El e-mail tiene un formato erroneo o invalido",
			"ERR_BADLIST":	"<b>Error</b>. Lista erronea",
			
			"ERR_NOLIST":	"<b>Error</b>. No has indicado ninguna lista a la que suscribirte",
			"ERR_COND":		"<b>Error</b>. Debes aceptar y entender las condiciones de uso",
			"ERR_NOEMAIL":	"<b>Error</b>. Debes rellenar tu e-mail"
		};
		var textos_asig = {
			'es': textos_es,
			'en': textos_es
		}
		var _ = textos_asig[ locale ];
		

		var hasError = false;
		var emailReg = /^[a-zA-Z0-9._-]+([+][a-zA-Z0-9._-]+){0,1}[@][a-zA-Z0-9._-]+[.][a-zA-Z]{2,6}$/;

		var emailVal = $("#f_email").val();
		var condicionesVal = $('#f_condiciones').attr('checked');
		var listaVal = $('#f_lista').attr('checked');
		var listagayVal = $('#f_listagay').attr('checked');
		if(condicionesVal != true) {
			$("#mailingMsg").html(_['ERR_COND']).fadeIn('slow').animate({opacity: '+=0'}, 2000).fadeOut('slow', function(){
				$('#f_sub').attr('disabled', false);
			});
			hasError = true;
		} else if(emailVal == 'E-Mail' || emailVal == '') {
			$("#mailingMsg").html(_['ERR_NOEMAIL']).fadeIn('slow').animate({opacity: '+=0'}, 2000).fadeOut('slow', function(){
				$('#f_sub').attr('disabled', false);
			});
			hasError = true;
		} else if(!emailReg.test(emailVal)) {	
			$("#mailingMsg").html(_['ERR_BADEMAIL']).fadeIn('slow').animate({opacity: '+=0'}, 2000).fadeOut('slow', function(){
				$('#f_sub').attr('disabled', false);
			});
			hasError = true;
		} else if ( !listagayVal && !listaVal ){
			$("#mailingMsg").html(_['ERR_NOLIST']).fadeIn('slow').animate({opacity: '+=0'}, 2000).fadeOut('slow', function(){
				$('#f_sub').attr('disabled', false);
			});
			hasError = true;
		}
		

		if(hasError == false) {
			//$("#maillist").append('<img src="loading.gif" alt="Loading" id="loading" />');
			$.post(
				path+locale+"/newsletter/process",
				{ email: emailVal, lista: listaVal, listagay: listagayVal },
				function(data,status){
					switch(status){
						case 'success':
						case 'notmodified':
							texto = _[data];
							if(data=='OK'){ newsFormReset(); }
							break;
						case 'error':
						case 'timeout':
						case 'parsererror': 
						default:
							texto = 'Error de comunicación con el gestor de listas.'
							break;
					}
					$("#mailingMsg").html(texto);
					$("#mailingMsg").slideDown('slow').animate({opacity: '+=0'}, 3000).slideUp('slow', function(){
						$('#f_sub').attr('disabled', false);
					});
				}
			);
		}
	});


	
});