(function($){

/* READY */

$(function()
{
	$('.gallery').each
	(
		function()
		{
			$(this).find('a:first').css('margin-left',0);
		}
	);

	$('.gallery a').each
	(
		function()
		{
			if(this.href != '')
				$(this).colorbox();
		}
	);

	var $allegato = $('#allegato');
	var $allegatoFake = $('#allegatoText');

	$('#mostraBoxAllegato').click
	(
		function()
		{
			$('#allegaFile').toggle();
			return false;
		}
	);

	$('#cancellaAllegato').click
	(
		function()
		{
			$('#allegato').remove();
			$('#allegaFile').prepend('<input id="allegato" type="file" name="allegato">');
			return false;
		}
	);

	function validateEmail(emailAddress)
	{
		var pattern = new RegExp(/^(("[\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);

		return(pattern.test(emailAddress));
	}

	function validateDate(d)
	{
		return isDate(d,'d/M/yyyy');
	}

	var $formAlert = $('#formAlert');
	function alertMsg(msg)
	{
		if(msg == '')
			$formAlert.hide();
		else
			$formAlert.show().html(msg);
	}

	function validateForm(id)
	{
		var email = $('#email').attr('value');
		var messaggio = $('#messaggio').attr('value');
		var antispam = $('#antispam').attr('value');

		if(id == "guestbook")
		{
			if(!validateEmail(email))
			{
				alertMsg('Indirizzo email non valido');
				return false;
			}
	
			if(email == '' || messaggio == '' || antispam == '')
			{
				alertMsg('Compilare i campi obbligatori');
				return false;
			}
		}
		else if(id == 'cvForm')
		{
			var nome = $('#nome').attr('value');
			var cognome = $('#cognome').attr('value');
			var dataNascita = $('#dataNascita').attr('value');
			var comuneResidenza = $('#comuneResidenza').attr('value');
			var provincia = $('#provincia').attr('value');
			var tipologiaLavoro = $('#tipologiaLavoro').attr('value');
			var telefono = $('#telefono').attr('value');

			if(!validateDate(dataNascita))
			{
				alertMsg('Data di nascita non valida');
				return false;
			}

			if(nome == '' || cognome == '' || dataNascita == '' || comuneResidenza == '' || provincia == '' ||
			   tipologiaLavoro == '' || email == '' || messaggio == '' || antispam == ''  || telefono == '')
			{
				alertMsg('Compilare i campi obbligatori');
				return false;
			}
		}
		return true;
	}

	$('form').submit
	(
		function()
		{
			var id = $(this).attr('id');
			return validateForm(id);
		}
	);


	$('form input').change
	(
		function()
		{
			alertMsg('');
		}
	);



	// ZOOM HEADER
	var $header = $('#header');
	var $zoomHeader = $('#zoomHeader');

	$header.mouseenter
	(
		function()
		{
			$zoomHeader.stop().fadeTo('slow',.95);
		}
	);
	$header.mouseleave
	(
		function()
		{
			$zoomHeader.stop().fadeOut('slow');
		}
	);

});     //  closing $(document).ready()

})(jQuery);
