$(function(){
	
	$("#lancia-privacy,#lancia-privacy-2,#lancia-legal,#lancia-credits").fancybox({
		onStart: function(){
			$('#fancybox-close').css({backgroundImage: 'url("../fancybox/chiudi-x.gif")', top: 40, right: 40});
			$('#fancybox-outer').css({background:'#fff'});
			$('#fancybox-content').css({border: '10px solid #FFFFFF'});
		},
		onClosed: function(){
			$('#fancybox-close').css({backgroundImage: 'inherit', top: 'inherit', right: 'inherit'});
			$('#fancybox-outer').css({background:'inherit'});
			$('#fancybox-content').css({border: 'inherit'});
		}
	});
	
	$('#email-value').click(function(){
		$(this).val('');
	});
	
	$(".wrapper-newsletter form").validate({
		rules: {
			email: {
				required: true,
				email: true
				}
			},
			messages: {
				email: ""
			},
			errorPlacement:function(error,element){
			
				var id = element.attr('name');
						if (id === 'privacymailchimp') {
							element = element.parent().addClass('errore');
						}
			},
			submitHandler: function(form) {
				
				$('#lancia-privacy').trigger('click');
				
				$('#newsletter form').validate({
					rules: { checkprivacy:"required" },
					messages: { checkprivacy:"" },
					errorPlacement:function(error,element){
						var id = element.attr('name');
						if (id === 'checkprivacy') {
							element = element.parent().addClass('wrapper-consenso-errore');
						}
					},
					submitHandler: function(form) {
						$("#newsletter-sender").hide();
						$('#newsletter-response').show();
						$.ajax({
							url: 'http://www.ultom.com/php/store-address.php', 
							data: 'email=' + escape($('#email-value').val())+'&lang='+$('#sigla-lingua').val(),
							dataType: 'text',
							success: function(data, textStatus) {
								$('#newsletter-response').html('<img src="http://www.ultom.com/images/ajax-loader.gif"/>');
								$('#newsletter-response').delay(1000).html(data);
							},
							error: function(data, textStatus){
								$('#newsletter-response').html('<img src="http://www.ultom.com/images/ajax-loader.gif"/>');
								$('#newsletter-response').delay(1000).html(data);
							}
						});
						
					}
				});
			}
	});

});
