/* condividi su facebook */
function fbs_click() {
	u=location.href;
	t=document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=600,height=600');
	return false;
}

var send = 0;

$(document).ready(function() {
	
	/* credits */
	$('#credits_link').click(function() {
      $('#credits_box').fadeIn('slow');
	  return false;
    });
	$('#credits_close_link').click(function() {
      $('#credits_box').fadeOut('slow');
	  return false;
    });
	
	/* form */
	$(".required").focus(function (){
		$(this).removeClass("insert_error");						   
		if(this.value == this.defaultValue) this.value = '';												
	});
	$(".required").blur(function (){								   
		if(this.value == '') this.value = this.defaultValue;												
	});
	
	$("#send").click(function(){

		if(send == 0){
				
				send = 1;					
				var mesg = '';	
				
				//validazione campi obbligatori
				$.each($(".required"), function() {								
					
				  if(this.id == 'email'){	
				  	if( !this.value.match(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i)){
							mesg = mesg+'Inserire il campo obbligatorio '+this.title+' o controllarne la sintassi.\n';
							$(this).addClass("insert_error");
						}
						else
							$(this).removeClass("insert_error");
					}				
					else {
						if( (jQuery.trim(this.value) == '') || (this.value == this.defaultValue)){
							mesg = mesg+'Inserire il campo obbligatorio '+this.title+'\n';	
							$(this).addClass("insert_error");
						}
						else
							$(this).removeClass("insert_error");
					}				  					  
				});				
				
				if(mesg != ''){				
					alert(mesg);					
					send = 0;
					return false;
				}
				else{					
					$("#send").hide();
					$("#loader").show();
					$.post("mailer/send_mail.php", $("#contatti").serialize(),
							function(data){
								if (data.status == true)
								{
									alert("Grazie per averci contattato.");	
									document.contatti.reset();	
									$("#loader").hide();
									$("#send").show();			
								}
								else
								{
									alert("Si e' verificato un errore. Riprova");
									$("#loader").hide();
									$("#send").show();	
								}
							}, 
							"json");						
					send = 0;										
					return false;				   
				}
				
			}							 
	});

});
