// indice di partenza
var k1 = 0;
var k2 = 0;
var k3 = 0;
var k4 = 0;
var k5 = 0;
var k6 = 0;
var t;
	
var fcontent = new Array();
var index = 0; //Math.floor(Math.random()*20);
// intervallo di tempo tra i vari messaggi
var delay = 5000;
var delay_in = 1000;
var delay_out = 1200;
var preload = true;

function makeslideshow() {

	if ( fcontent.length <= 0 )
		return;
		
	if ( index >= fcontent.length )
		index = 0;
	
	if ( fcontent.length > 1) {
		if (preload) {
			$('.image1').html( fcontent[index] );
			preload = false;
			index++;
			t = setTimeout("makeslideshow()", delay);
		} else {
			$(".image1").fadeOut(delay_out, function() {
			$('.image1').html( fcontent[index] );
			$('.image1').fadeIn(delay_in);
			index++;
		});

			t = setTimeout("makeslideshow()", delay);
		}
	}
	else {
		$('.image1').html( fcontent[index] );
	} 
}

function addArray(i, text) {
	fcontent[i] = text;
}

function showIt(classe, time) {
//	$(".image1").fadeOut(1200);
//	clearTimeout(t);
//	t = setTimeout("makeslideshow()", 20000);
	$(".image div."+ classe + " img").delay(time).fadeIn("slow");
	$(".image div." + classe + " div.opc").delay(500 + time).animate(  { width: 'toggle' }, "slow" );
	$(".image div." + classe + " p").delay(1200 + time).animate( { opacity: 'toggle' }, "slow" );
}

function hideIt(classe) {
	$(".image div." + classe + " p").animate( { opacity: 'toggle' }, "slow" );
	$(".image div." + classe + " div.opc").delay(500).animate(  { width: 'toggle' }, "slow" );
	$(".image div." + classe + " img").delay(1200).fadeOut("slow");
}

function remIt() {
	if (k1 == 1) {
		$("a.ricostruzione").removeClass("qui");
		Cufon.replace("a.ricostruzione");
		hideIt("ricostruzione");
		k1--;
		return true;
	} else if (k2 == 1) {
		$("a.trattamento").removeClass("qui");
		Cufon.replace("a.trattamento");
		hideIt("trattamento");
		k2--;
		return true;
	} else if (k3 == 1) {
		$("a.reffil").removeClass("qui");
		Cufon.replace("a.reffil");
		hideIt("reffil");
		k3--;
		return true;
	} else if (k4 == 1) {
		$("a.manicure").removeClass("qui");
		Cufon.replace("a.manicure");
		hideIt("manicure");
		k4--;
		return true;
	} else if (k5 == 1) {
		$("a.pedicure").removeClass("qui");
		Cufon.replace("a.pedicure");
		hideIt("pedicure");
		k5--;
		return true;
	} else if (k6 == 1) {
		$("a.paraffina").removeClass("qui");
		Cufon.replace("a.paraffina");
		hideIt("paraffina");
		k6--;
		return true;
	}
	return false;
}

function startFlash() {
	remIt();
	$(".image1").delay(2200).fadeIn("slow");
	t = setTimeout("makeslideshow()", 10000);
}

function stopFlash() {
	clearTimeout(t);
	$(".image1").fadeOut("slow");
	t = setTimeout("startFlash()", 45000);
	return true;
}


$(document).ready(function() {

	var time = 1;
	var i = 0;
	
	$('.slider_page li').each(function() {
	   	addArray(i, $(this).html());
		i++;
	});

	makeslideshow();
	
	//--
	$("a.ricostruzione").click(function() {
			if (k1 == 0) {
				if ( stopFlash() ) { time = 900; }
				
				if ( remIt() ) { time = 2200; }
				
				$(this).addClass("qui");
				showIt("ricostruzione", time);
				k1++;
			} else {
				$(this).removeClass("qui");
				hideIt("ricostruzione");
				k1--;
			}
		});
		
		$("a.trattamento").click(function() {
			if (k2 == 0) {
				if ( stopFlash() ) { time = 900; }
				
				if ( remIt() ) { time = 2200; }
				
				$(this).addClass("qui");
				showIt("trattamento", time);
				k2++;
			} else {
				$(this).removeClass("qui");
				hideIt("trattamento");
				k2--;
			}
		});
		
		$("a.reffil").click(function() {
			if (k3 == 0) {
				if ( stopFlash() ) { time = 900; }
	
				if ( remIt() ) { time = 2200; }

				$(this).addClass("qui");
				showIt("reffil", time);
				k3++;
			} else {
				$(this).removeClass("qui");
				hideIt("reffil");
				k3--;
			}
		});
		
		$("a.manicure").click(function() {
			if (k4 == 0) {
				if ( stopFlash() ) { time = 900; }
				
				if ( remIt() ) { time = 2200; }

				$(this).addClass("qui");
				showIt("manicure", time);
				k4++;
			} else {
				$(this).removeClass("qui");
				hideIt("manicure");
				k4--;
			}
		});
		
		$("a.pedicure").click(function() {
			if (k5 == 0) {
				if ( stopFlash() ) { time = 900; }
				
				if ( remIt() ) { time = 2200; }

				$(this).addClass("qui");
				showIt("pedicure", time);
				k5++;
			} else {
				$(this).removeClass("qui");
				hideIt("pedicure");
				k5--;
			}
		});
		
		$("a.paraffina").click(function() {
			if (k6 == 0) {
				if ( stopFlash() ) { time = 900; }
				
				if ( remIt() ) { time = 2200; }

				$(this).addClass("qui");
				showIt("paraffina", time);
				k6++;
			} else {
				$(this).removeClass("qui");
				hideIt("paraffina");
				k6--;
			}
		});
	//--
});

