$(function(){
	$.jFastMenu("#nav");
	$.jFastMenu("#nav_evidenza");
	
	$("img.lazy").each(function(){
		$(this).hide();
		$(this).one("load", function(){
			$(this).fadeIn("slow");
		}).each(function(){
			if(this.complete) $(this).trigger("load");
		});
	});
	
	$("img.img_attuale").each(function(){
		$(this).hide();
		$(this).one("load", function(){
			$(this).fadeIn("slow");
		}).each(function(){
			if(this.complete) $(this).trigger("load");
		});
	});
	
	var img_attuale = 0;
	var num_imgs = 3;
	
	$("#avanti").click(function(event){
		event.preventDefault();
		if(img_attuale == num_imgs - 1){
			$("#img_" + img_attuale).hide();
			img_attuale = 0;
			$("#img_" + img_attuale).one("load", function(){
				$(this).fadeIn();
			}).each(function(){
				if(this.complete) $(this).trigger("load");
			});
		}
		else{
			$("#img_" + img_attuale).hide();
			img_attuale++;
			$("#img_" + img_attuale).one("load", function(){
				$(this).fadeIn();
			}).each(function(){
				if(this.complete) $(this).trigger("load");
			});
		}
	});
	
	var validEmail = new RegExp("^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$", "gi");
	$("#registrazione_newsletter_btn").click(function(){
		var email = $("#email_newsletter").val();
		var email_conferma = $("#conferma_email_newsletter").val();
		if(email.length > 0 && validEmail.test(email)){
			if(email != email_conferma){
				$("#esito_iscrizione").html('<div class="error">' + i18n.newsletter['EMAIL_NON_CORRISPONDENTI'] + '</div>');
			}
			else{
				$.ajax({
					url: Config.getBaseURL() + "expo/iscrizione_newsletter/" + email,
					cache: false,
					type: "GET",
					dataType: "json",
					success: function(res){
						var esito = res.esito;
						if(esito == 'ISCRITTO'){
							$("#esito_iscrizione").html('<div class="success">' + i18n.newsletter[esito] + '</div>');
						}
						else{
							$("#esito_iscrizione").html('<div class="error">' + i18n.newsletter[esito] + '</div>');
						}
						
					},
					error: function(xhr, text, error){ 
						$("#esito_iscrizione").html('<div class="error">' + i18n.newsletter['ERRORE_RICHIESTA'] + '</div>');
					}
				});
			}
		}
		else{
			$("#esito_iscrizione").html('<div class="error">' + i18n.newsletter['NON_VALIDATO'] + '</div>');
		}
	});
	
	$("#voto_sondaggio_btn").click(function(){
		var risposte = $("input[name='voto[]']:checked").map(function(){ return $(this).val(); }).get().join(",");
		$.ajax({
				url: Config.getBaseURL() + "expo/vota_sondaggio/1/" + risposte,
				cache: false,
				type: "GET",
				dataType: "json",
				success: function(res){
					var esito = res.esito;
					if(esito == 'VOTO_SALVATO'){
						$("#esito_sondaggio").html('<div class="success">' + i18n.newsletter[esito] + '</div>');
					}
					else{
						$("#esito_sondaggio").html('<div class="error">' + i18n.newsletter[esito] + '</div>');
					}
					
				},
				error: function(xhr, text, error){ 
					$("#esito_sondaggio").html('<div class="error">' + i18n.newsletter['ERRORE_RICHIESTA'] + '</div>');
				}
		});
	});
	
	$("#accedi-area-riservata").click(function(event){
		event.preventDefault();
		$("#login-area").show();
		$(this).hide();
	});
	
	if($("#player-gallery").length){
		flowplayer(
			"player-gallery", 
			{ src : Config.getBaseURLmedia() + "media/flowplayer/flowplayer-3.2.7.swf", wmode : 'transparent'},
			{
				clip:  {
					autoPlay: true,
					autoBuffering: true,
					onBeforeFinish: function(){ return false; }
				}
			}
			);
	}
	
});
