/******************************************************************************************************************/
// CARUSEL FUNCTIONS
/******************************************************************************************************************/

/*
function slideItem(i){
	items = $('#barraLibri LI').size();
	
	w = items * 148;
	$('#barraLibri UL').width(w);
	
	offset = i * 148;
	offset = '-'+ offset +'px';
	
	$('#barraLibri UL').animate({ left: offset }, 500);
	
	i++;
	if (i >= (items - 4)) i = 0;
	
	SliderTimerId = window.setTimeout('slideItem('+ i +')', 3000);
}
*/

function handleCarousel(){
	items = $('#barraLibri LI').size();
	
	if (items > 1) {
	
		$('#barraLibri UL').css({ left: 0 });
		$('#barraLibri UL').hide().fadeIn();
		
		w = items * 148;
		$('#barraLibri UL').width(w);
		
		offset = '-'+ w +'px';
		delay = items * 5000;
		
		$('#barraLibri UL').animate({ left: offset }, delay, 'linear', function(){
			$(this).fadeOut();
			handleCarousel();
		});
		
		$('#barraLibri LI').hover(function(){
			index = $('#barraLibri LI').index(this);
			$('#barraLibri UL').stop(true);
			
			$(this).css('background','#FF6600');
			
			seloffset = $(this).position();
			selpos = seloffset.left - 32;
			$(this).find('.info').css({ left: selpos });
			
			$(this).find('.info').show();
		}, function(){
			delay = (items - index) * 5000;
			$('#barraLibri UL').animate({ left: offset }, delay, 'linear', function(){
				$(this).fadeOut();
				handleCarousel();
			});
			$(this).find('.info').hide();
			$(this).css('background','transparent');
		});
	}
}

/******************************************************************************************************************/
// Aggiungo la nuova funzione controlla formattazione codice fiscale...
/******************************************************************************************************************/

jQuery.validator.addMethod("codice_fiscale", function(value, element) {
	var regex = /[A-Z]{6}[\d]{2}[A-Z][\d]{2}[A-Z][\d]{3}[A-Z]/;
	return this.optional(element) || regex.test(value);
}, "Please insert a valid italian identification number");

/******************************************************************************************************************/
// On document load...
/******************************************************************************************************************/

$(function(){
	// carousel handle
	handleCarousel();
	
	$("#azienda").hide();
	$("#checkfattura").click(function(){
		$("#azienda").toggle();
	});
	
	
	// $('#form-iscrizione_newsletter').validate({
		// errorElement: 'div',
		// rules: {
			// email: { 
				// required: true,
				// email: true
			// }
		// },
		// messages: {
			// email: {
				// required: function(){alert('un mail è richiesta')} ,
				// email: function(){alert('la mail inserita non è valida')}
			// }
		// }
	// });

	// form validation
	$('#form-register').validate({
		errorElement: 'div',
		rules: {
			nomecorso: 'required',
			cittacorso: 'required',
			datacorso: 'required',
			checkpago: 'required',
			nome: 'required',
			cognome: 'required',
			indirizzo: 'required',
			cap: 'required',
			citta: 'required',
			checkpago_congresso: 'required',
			checkpago_liberta: 'required',
			metodo_pagamento_liberta: 'required',
			//prov: 'required',
			//stato: 'required',
			//codf: 'required',
			//datanascita: 'required',
			//luogonascita: 'required',
			tel: 'required',
			email: { 
				required: true,
				email: true
			},
			codf: {
				required: false,
				rangelength: [16, 16],
				codice_fiscale: true
				
			},
			datanascita: {
				required: false,
				date: true
			},
			fattura_ragione_sociale: {
				 required: {
					depends:function(element) {
								if($("#checkfattura:checked").val() == '1')
									return true
								else
									return false
							}
				} 
			},
			fattura_piva: {
				 required: {
					depends:function(element) {
								if($("#checkfattura:checked").val() == '1')
									return true
								else
									return false
							}
				} 
			},
			fattura_codice_fiscale: {
				 required: {
					depends:function(element) {
								if($("#checkfattura:checked").val() == '1')
									return true
								else
									return false
							}
				},
				rangelength: [16, 16],
				codice_fiscale: {
					depends:function(element) {
								if($("#checkfattura:checked").val() == '1')
									return true
								else
									return false
							}
				}
			},
			fattura_telefono: {
				 required: {
					depends:function(element) {
								if($("#checkfattura:checked").val() == '1')
									return true
								else
									return false
							}
				} 
			},
			fattura_indirizzo: {
				 required: {
					depends:function(element) {
								if($("#checkfattura:checked").val() == '1')
									return true
								else
									return false
							}
				} 
			},
			fattura_cap: {
				 required: {
					depends:function(element) {
								if($("#checkfattura:checked").val() == '1')
									return true
								else
									return false
							}
				} 
			},
			fattura_citta: {
				 required: {
					depends:function(element) {
								if($("#checkfattura:checked").val() == '1')
									return true
								else
									return false
							}
				} 
			}
		},
		messages: {
			nomecorso: '&uarr; campo obbligatorio',
			cittacorso: '&uarr; campo obbligatorio',
			datacorso: '&uarr; campo obbligatorio',
			checkpago: '&uarr; campo obbligatorio',
			nome: '&uarr; campo obbligatorio',
			cognome: '&uarr; campo obbligatorio',
			indirizzo: '&uarr; campo obbligatorio',
			cap: '&uarr; campo obbligatorio',
			citta: '&uarr; campo obbligatorio',
			prov: '&uarr; campo obbligatorio',
			stato: '&uarr; campo obbligatorio',
			checkpago_congresso: '&uarr; campo obbligatorio',
			checkpago_liberta: '&uarr; campo obbligatorio',
			metodo_pagamento_liberta: '&uarr; Il metodo di pagamento &egrave; obbligatorio',
			//codf: '&uarr; campo obbligatorio',
			//datanascita: '&uarr; campo obbligatorio',
			luogonascita: '&uarr; campo obbligatorio',
			tel: '&uarr; Uno dei campi Tel. o Cellulare deve essere inserito',
			email: {
				required: '&uarr; campo obbligatorio',
				email: '&uarr; inserire una email valida'
			},
			codf: {
				rangelength: '&uarr; Il codice fiscale deve essere lungo 16 cifre',
				codice_fiscale: '&uarr; Il codice fiscale non &egrave; corretto'
			},
			datanascita: {
				date: '&uarr; la data non &egrave; valida'
			},
			fattura_ragione_sociale: '&uarr; campo obbligatorio per la compilazione della fattura',
			fattura_piva: '&uarr; campo obbligatorio per la compilazione della fattura',
			fattura_codice_fiscale: {
				required: '&uarr; campo obbligatorio per la compilazione della fattura',
				rangelength: '&uarr; Il codice fiscale deve essere lungo 16 cifre',
				codice_fiscale: '&uarr; Il codice fiscale non &egrave; corretto'
			},
			fattura_telefono: '&uarr; campo obbligatorio per la compilazione della fattura',
			fattura_indirizzo: '&uarr; campo obbligatorio per la compilazione della fattura',
			fattura_cap: '&uarr; campo obbligatorio per la compilazione della fattura',
			fattura_citta: '&uarr; campo obbligatorio per la compilazione della fattura'
		},
		groups: {
			username: "tel cell"
		},
		errorPlacement: function(error, element) {
			if (element.attr("name") == "tel" || element.attr("name") == "cell" )
				error.insertAfter("#cell");
			else
				error.insertAfter(element);
	   }
	});
});
