$(document).ready(function() {

	Cufon.replace('.futura');
	//$('.menulist>li').each(function() { $(this).find('.nav').hide(); });
	$('.menulist>li').mouseenter(function() { $(this).find('.nav').show(); });
	$('.menulist>li').mouseleave(function() { $(this).find('.nav').hide(); });

	$('select#utilizzo').change( function () { aggiornaS2('utilizzo','linea','caratteristiche'); } );
	$('select#linea').change( function () { aggiornaS2('linea','caratteristiche','utilizzo'); });
	$('select#caratteristiche').change( function () { aggiornaS2('caratteristiche','utilizzo','linea'); });

} );

function aggiornaS2( base, agg1, agg2 ) {
	url  =  'ajax.aggiornasearch3steps.php?base='+base+'&baseval=' + $('#'+base).val()+'&agg1='+agg1+'&agg1val=' + $('#'+agg1).val()+'&agg2='+agg2+'&agg2val=' + $('#'+agg2).val();
	$.get(url, function(data) {
			var arr = data.split('|');	
			$('#'+agg1).html(arr[0]);
			$('#'+agg2).html(arr[1]);
		}
	);
}

function search() {
	document.location.href="cerca.php?c1="+escape($("#utilizzo").val())+"&c2="+escape($("#linea").val())+"&c3="+escape($("#caratteristiche").val());
}

function newsletter2() {
	$.get('ajax.newsletter2.php?x=' + $("#email").val() + '&nome=' + $("#nome").val() + '&cognome=' + $("#cognome").val()  + '&indirizzo=' + $("#indirizzo").val() + '&nascita=' + $("#nascita").val()  + '&citta=' + $("#citta").val() + '&prov=' + $("#prov").val() +'&check='+($('#checknl').attr("checked")?"1":"0"), function(data) {
		$('#risposta').html(data);
		//var arr = data.split('|');	
		//newsletterErr(arr[0]);
	});
}

function contatti2() {

	$.ajax({	'type' : 'POST',
		'url' : "ajax.contatti.php",
		'data' : {	'email' : $('#email').val(),
			'nome' : $('#nome').val(),
			'cognome' : $('#cognome').val(),
			'indirizzo' : $('#indirizzo').val(),
			'nascita' : $('#nascita').val(),
			'citta' : $('#citta').val(),
			'prov' : $('#prov').val(),
			'check' : ($('#checknl').attr("checked")?"1":"0"),
			'msg' : $('#msg').val()
		},
		'dataType' : 'json',
		'success' : function( $response ) { 
			if ($response.ok == 'ok') { 
				//...
				$('#containernl').hide("slow");
				$('#risposta').html($response.msg);
			 } else {
				 $('#risposta').html($response.msg);
				 $('#risposta').css("text-decoration","blink");
				 setTimeout(function() {$('#risposta').css("text-decoration","none");},5000);
			 }
		},
		'error' : function () { alert("errore"); }
	});
	
}




//----------------------------------------------------------------------------
//per la gallery
var galleria_ar = null;
var galleria_links_ar = null;
var gallery_pos = 1;	//gallery counter position, 0 first
var gallery_seconds = 5;
var gallery_status = "stop";
var gallery_timer = null;
var gallery_show_buttons = true;

//var galleria_ar = Array('ciccio.jpg','pippo.jpg');
//onload chiamare goGallery();
//viene cambiato il background all'id #galleria
//----------------------------------------------------------------------------

function goGallery() {	//init
	if (galleria_ar.length>1) {
		//preload
		var a = new Array();
		for (i=0; i<galleria_ar.length; i++) { a[i] = new Image(); a[i].src=galleria_ar[i]; }
		if (gallery_show_buttons) {
			$('#galleria').after("<div id='galleria_comandi'><a id='gallery_back' href='#' onclick='gallery_back(event);'>indietro</a> <a href='#' id='gallery_stopplay' onclick='gallery_stopplay(event);' class='play'>stop</a> <a href='#' id='gallery_next' onclick='gallery_next(event);'>avanti</a></div>");
		}
		gallery_status = "play";
		setTimeout(	function () { startGallery(1); }, gallery_seconds * 1000);
	}
}


function gallery_back(e) {
	e.preventDefault();
	if (gallery_status=="play") { gallery_stopplay(e); }
	startGallery(-1);
}
function gallery_next(e) {
	e.preventDefault();
	if (gallery_status=="play") {  gallery_stopplay(e); }
	startGallery(1);
}
function gallery_stopplay(e) {
	e.preventDefault();
	if (gallery_status=="play") {
		gallery_status = "stop";
		$('#gallery_stopplay').attr("class","stop");
		$('#gallery_stopplay').html("play");
		clearTimeout( gallery_timer );
	} else {
		gallery_status = "play";
		$('#gallery_stopplay').attr("class","play");
		$('#gallery_stopplay').html("stop");
		startGallery(1);
	}
	
}

function startGallery(step) {
	if (step==1 && gallery_pos>galleria_ar.length - 1) gallery_pos =0;
		else if (step==-1 && gallery_pos<0) gallery_pos =galleria_ar.length - 1;
	$('#galleria').fadeTo("fast", 0 , function () {
		$('#galleria').css("background-image","url(" + galleria_ar[gallery_pos] + ")");
		if ( galleria_links_ar != null) {
			$('#galleria a.link').attr("href", galleria_links_ar[gallery_pos]);
		}
		$('#galleria').fadeTo("slow", 1);
		gallery_pos += step;
	});
	if (gallery_status=="play") gallery_timer = setTimeout(	function () { startGallery(step); }, gallery_seconds * 1000);
}


