$(document).ready(function() {
	if ($('#gallery').length>0) { 
		slideShow(); 
	}
	if ($('.flusso_link').length>0) {
		$('.flusso_link').click(function(){
			var h=screen.height-200;
			if (h>1300) h=1300;
			window.open($(this).attr('href'),'flusso_link','location=no,scrollbars=yes,width=820,height='+h+',top=50,screenY=50');
			return false;
		});
	}
	$('span.mailme').mailme();
});

jQuery.fn.mailme = function() {
    var at = / at /;
    var dot = / dot /g;
    this.each( function() {
        var addr = jQuery(this).text().replace(at,"@").replace(dot,".");
        var title = jQuery(this).attr('title')
        $(this)
            .after('<a href="mailto:'+addr+'" title="'+title+'">'+ addr +'</a>')
            .remove();
    });
};

function slideShow() {
	$('#gallery a').css({opacity: 0.0});
	$('#gallery a:first').css({opacity: 1.0});
	setInterval('gallery()',2000);
	
}
function gallery() {
	var current = ($('#gallery a.show')?  $('#gallery a.show') : $('#gallery a:first'));
	var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('#gallery a:first') :current.next()) : $('#gallery a:first'));	
	next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);
	current.animate({opacity: 0.0}, 1000).removeClass('show');
}


function check_job_form(){
	var errs=0; 
	
	// campi obbligatori
	$('#job .required').each(function (i) {
		if ($(this).val()=='') {
			if (errs++==0) $(this).focus();
			$(this).addClass('error');
		}
		else{
			$(this).removeClass('error');
		}
	});
	if (errs>0) return msg_alert('warning','Compilare tutti i dati obbligatori.');

	// campi obbligatori
	if(!$('#job .accept').attr('checked')){
		errs++;
	}
	if (errs>0) return msg_alert('warning','Leggere ed Accettare la "Informativa sulla privacy" per proseguire.');
	
	if (errs==0) return true;
	return false;
}

function check_newsletter_form(){
	var errs=0;
	
	// campi obbligatori
	$('#subForm #name, #subForm #bbduk-bbduk, #subForm #Company').each(function (i) {
		if ($(this).val()=='') {
			if (errs++==0) $(this).focus();
			$(this).addClass('error');
		}
		else{
			$(this).removeClass('error');
		}
	});
	if (errs>0) return msg_alert('warning','Compilare tutti i dati obbligatori.');

	// campi obbligatori
	if(!$('#subForm #accept').attr('checked')){
		errs++;
	}
	if (errs>0) return msg_alert('warning','Leggere ed Accettare la "Informativa sulla privacy" per proseguire.');
	
	if (errs==0) return true;
	return false;
}

function check_newsletter_form_en(){
	var errs=0;
	
	// campi obbligatori
	$('#subForm #name, #subForm #putvu-putvu, #subForm #Company').each(function (i) {
		if ($(this).val()=='') {
			if (errs++==0) $(this).focus();
			$(this).addClass('error');
		}
		else{
			$(this).removeClass('error');
		}
	});
	if (errs>0) return msg_alert('warning','Please, fill all required fields.');

	// campi obbligatori
	if(!$('#subForm #accept').attr('checked')){
		errs++;
	}
	if (errs>0) return msg_alert('warning','Please, accept "Privacy Policy" to proceed.');
	
	if (errs==0) return true;
	return false;
}

function check_contatti_form(){
	var errs=0;
	
	// campi obbligatori
	$('#contatti .required').each(function (i) {
		if ($(this).val()=='') {
			if (errs++==0) $(this).focus();
			$(this).addClass('error');
		}
		else{
			$(this).removeClass('error');
		}
	});
	if (errs>0) return msg_alert('warning','Compilare tutti i dati obbligatori.');

	// campi obbligatori
	if(!$('#contatti .accept').attr('checked')){
		errs++;
	}
	if (errs>0) return msg_alert('warning','Leggere ed Accettare la "Informativa sulla privacy" per proseguire.');
	
	if (errs==0) return true;
	return false;
}

function msg_alert(msg_class, msg_text){
	alert(msg_text);
	return false;
}

function show_video(){
	var v=$(this).attr('href');
	$('.filmato').html('');
	$('.filmato').html('<object width="240" height="205" data="'+v+'" type="application/x-shockwave-flash"><param name="data" value="'+v+'" /><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="'+v+'" /><param name="allowfullscreen" value="true" /></object>');
	return false;
}
