$(document).ready(function(){
	
	// jQuery SmoothScroll - thanks to Bogdan (http://blog.medianotions.de/en/articles/2009/smoothscroll-for-jquery)
  	
	var scrollTime = 1000  //number of millisecond of the smoothScroll effect duration
	$('a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
		&& location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target
			|| $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body')
				.animate({scrollTop: targetOffset}, scrollTime);
				return false;
			}
		}
	});
	 
	// jQuery Ajax Mail Script courtesy of Antonio Fullone - LastWebDesigner - http://lastwebdesigner.com (modified by WEBBOgrafico)
	$('#contactUs').removeAttr('action');
	$('#sendmail').click(function(){
		// restoring normal appearance of the fields before the validation occurs (useless the first time)
		$('#name, #mail, #text').removeClass("error");
		var valid = '';
		var isr = ' is required.<br />';
		// getting the values of the fields
		var name = $('#name').val();
		var mail = $('#mail').val();
		var text = $('#text').val().replace(/[\r\n]/g,"<br />"); //transforming textarea carriage returns in break line tags 
		// validating them. If not, change the class of the empty field to .error and add it to the message div html
		if (name.length<1) {
			valid += 'Name'+isr;
			$('#name').addClass("error");
		}
		if (!mail.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
			valid += 'A valid Email'+isr;
			$('#mail').addClass("error");
		}
		if (text.length<1) {
			valid += 'Text'+isr;
			$('#text').addClass("error");
		}
		// if there's at list one field empty let the user know
		if (valid!='') {
			$("#response").addClass("contactUnknown");
			$('#response').fadeIn(1000);
			$('#response').html(valid);
		}
		// if everything is ok send the data to mail.php
		else {
			var datastr ='name=' + name + '&mail=' + mail + '&text=' + text;
			$("#response").addClass("contactWait");
			$('#response').css('display', 'block');
			$('#response').html('Sending message .... ');
			$('#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("contactOk");
		$("#response").fadeIn("slow");
		$("#response").html(html);
		setTimeout('$("#response").fadeOut("slow")',2000);
		// clear out the fields after succesfully sending a message
		$('#name, #mail, #text').val("");
	}
	});
}




