$(function() {
	
	
	// begin MAIL FIELD STUFF
	
	// Default Text to appear in form fields
	var defaultText = $('#mail').val();
	
	$('#mail').css("color", "#CCC").focus(function() {
				if($(this).val()==defaultText) {
					$(this).attr("value","").css("color", "#424242");
				}
			});
			$('input, textarea').blur(function() {
				if($(this).val()=="") {
					$(this).val(defaultText).css("color", "#CCC");
				}
			});
	
	// end MAIL FIELD STUFF
	
	// begin BACKGROUND CONTINUOUS SCROLL
	
	// Define the height of your two background images.
           //The image to scroll
	   var backgroundheight = 1600;
           //The image to overlay
	   var backgroundheight_two = 1600;

	// Create a random offset for both images' starting positions
        offset = Math.round(Math.floor(Math.random()* 2001));
        offset2 = Math.round(Math.floor(Math.random()* 1001));
 
	function scrollbackground() {
        //Ensure all bases are covered when defining the offset.
   		offset = (offset < 1) ? offset + (backgroundheight - 1) : offset - 1;
		// Put the background onto the required div and animate vertically
   		$('#container').css("background-position", "50% " + offset + "px");
   		// Enable the function to loop when it reaches the end of the image
   		setTimeout(function() {
			scrollbackground();
			}, 80
		);
   	}

	function scrolloverlay() {
        //Ensure all bases are covered when defining the offset.
		offset2 = (offset2 < 1) ? offset2 + (backgroundheight_two - 1) : offset2 - 1;
		// Put the background onto the required div and animate vertically
		$('#overlay').css("background-position", "50% " + offset2 + "px");
   		// Enable the function to loop when it reaches the end of the image
   		setTimeout(function() {
			scrolloverlay();
			}, 40
		);
   	}

	// Initiate the scroll
		scrollbackground();
		scrolloverlay();
		
	// end BACKGROUND CONTINUOUS SCROLL	
	
	// jQuery Ajax Mail Script courtesy of Antonio Fullone - LastWebDesigner - http://lastwebdesigner.com (modified by WEBBOgrafico)
	$('#contact').removeAttr('action');
	$('#sendmail').click(function(){
		
		// starting from initial situation every click
		$('#response').removeClass("error sending sendOk");
		
		var valid = '';
		// getting the values of the fields
		var mail = $('#mail').val();
		
		// validating them. If not, change the class of the empty field to .error and add it to the message div html
		
		if (!mail.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
			valid += 'Inserire un\'indirizzo email valido';
		}
		
		// if there's at list one field empty let the user know
		if (valid!='') {
			$("#response").addClass("error");
			$("#response").fadeIn(1000);
			$("#response").html(valid);
			$("#response").delay(3000).fadeOut(1000);
		}
		// if everything is ok send the data to mail.php
		else {
			var datastr ='mail=' + mail;
			$("#response").addClass("sending");
			$('#response').html('Invio messaggio in corso... ');
			$('#response').fadeIn('slow');
			setTimeout("send('"+datastr+"')",2000);
		}
		return false;
	});

});

function send(datastr){
	$.ajax({	
		type: "POST",
		url: "mail.php",
		data: datastr,
		cache: false,
		success: function(html){
		$("#response").addClass("sendOk");
		$("#response").fadeIn("slow");
		$("#response").html(html);
		setTimeout('$("#response").fadeOut("3000")',2000);
		// clear out the fields after succesfully sending a message
		$('#mail').val("");
	}
	});
}


