// JavaScript Document


function esiste(idPassato){
		if ($(idPassato).length)return true;
}


//SET BLOCCO 01
function bl_primo(){
	$('.immagine_estesa img').hide();
	wmax = 576;
	hmax = 419;
	imgW_old = $('.immagine_estesa img').width();
	imgH_old = $('.immagine_estesa img').height();
	
	
	$('blo_1 center').css('padding-top','0px!important')
	
	if(imgH_old > imgW_old){
		$('.immagine_estesa img').width(wmax);
	}else{
		$('.immagine_estesa img').height(hmax);
	}
	
	$('.immagine_estesa img').show();
	
	$('.immagine_estesa img').css('cursor','pointer')
	
	if(esiste('.immagine_estesa .txt_blo a')){
		
		
		
		$('.immagine_estesa img').click(function() {
					
					curlink = $('.immagine_estesa .txt_blo a').attr('href');
					
					
					if($('.immagine_estesa .txt_blo a').attr('target')){
						
						targetlink = $('.immagine_estesa .txt_blo a').attr('target')
					
					}else {
						
						targetlink = '_self'	
					}
					
					
		
					open(curlink,targetlink);
		})
		
	}
	
	/*$('.immagine_estesa img').css({
		
		'position':'absolute',
		'left':'0px',
		'top':'0px',
		'width':'570px',
		
	});*/


    
	
	
		
}



// SPAZIATURA IN PX TRA I SINGOLI CLIP
var offsetClip = 4;

// VELOCITA' SLIDE IN SECONDI es un secondo emmezzo  = time(1.3);
var vel = time(4);

// POZIZIONA BANNER IN VERTICALE
// "center" or "top" or "bottom" or Valore numerico in px dal top senza le virgolette es: var verticalPosition = 20; 
var verticalPosition = "top";

// PULSANTI DI NAVIGAZIONE
// YES OR NO or Nothing
var navigation = "yes";
var btnWidth = 30;

// DIREZIONE ANIMAZIONE OPPURE Ferma ANIMAZIONE
var animazione = "yes" // "Yes" or "NO"
var direzione = "rtl" // "ltr" --> Or <-- "rtl"

// DIMENSIONE SLIDE
var altezza = 96; // or "AUTO"
var larghezza = 176; //or "AUTO"

///////////////////////////////////////////////////////////

var sommalunghezze = 0;
var ultimadiapo = 0;
var curClip = 0;
var lunghezzeSingole = [];
var nuovadimensione = 0;
var posixInit = 0;
var backCliccato = 0;
var animazionestato = 0;
var contaCiclo = 0;

//var obj =[];

var obj = new Array();

///////////////////////////////////////////////////////////

function creArray() {
	
	$('.contenitore').each(function(index) {

		//ASSEGNO L'ID ALLA GALLERY
		$(this).attr('id', 'galleria_' + index);

		//CARICO GLI ID NELL'ARRAY OBJ
		obj.push('galleria_' + index);

		//$('.next',this).click(function () {

		//	console.log(obj[index])

		//})
		//creaRullini(obj[index]);
		creaRullini(index);
	})

}

function creaRullini(idPassato) {


	//RESET DI EVENTUALI STILI SULL'UL ESTERNI


	//SETTA DIMENSIONE SE E' PRESENTE O MENO IL TASTO DI MENU NAVIGAZIONE START
	
	var altezza_contenitore = $(".galleria_outer").height();
	
	if (navigation == "YES" || navigation == "Yes" || navigation == "yes") {

		nuovadimensione = $("#" + obj[idPassato] + " .galleria_outer").width()
				- (btnWidth * 2);

		$("#" + obj[idPassato] + " .galleria_outer").width( nuovadimensione + "px");
		$("#" + obj[idPassato] + " .galleria_outer").css("left", btnWidth+3 + "px");
		$(".btnBack, .btnNext").css({
			"width" : btnWidth+"px",
			"height": altezza_contenitore+"px"
			
		});

	} else {
		$(".galleria_outer").width("100%");
		$(".btnBack, .btnNext").css("display", "none");
	}

	

	

	
	$(".galleria_outer ul li").css( {

		
		"height" : altezza + "px",
		"width" : larghezza + "px",
		"overflow" : "hidden",
		"display" : "block",
		
		
		"margin-left" : "0px",
		"margin-right" : offsetClip + "px",
		"padding-left" : "0px",
		"padding-right" : "0px"

		

	});

	//VIENE LANCIATA PER OGNI GALLERY
	$("#" + obj[idPassato] + " .galleria_outer ul:first li").each(
			function(index) {

				sommalunghezze += $(this).width() + offsetClip;

				++ultimadiapo;

			});

	//VALORIZZO IL VALORE DELL'ULTIMA DIAPOSITIVA e Lunghezza in PX della
	// gallery x RECUPERARLO IN UNA CLASSE $( "#" + idPassato).data('ultima')

	$("#" + obj[idPassato]).data('valori', {
		ultimaImmagine : ultimadiapo,
		lunghezzaGallery : sommalunghezze,
		currentFrame : 0
	});

	//RIAZZERO I VALORI
	ultimadiapo = 0;
	sommalunghezze = 0;

	//	console.log("Ultima diapo : " + $( "#" + idPassato).data('valori').ultimaImmagine + "  lunghezzaGallery " + $( "#" + idPassato).data('valori').lunghezzaGallery)

	//SETTO DIMENSIONI DIAPO
	$("#" + obj[idPassato] + " .galleria_outer ul li").css( {
		"width" : larghezza + "px"
	}); //SINGOLI LI WIDTH
	$("#" + obj[idPassato] + " .galleria_outer ul li").css( {
		"height" : altezza + "px",
		"overflow" : "hidden"
	}); //SINGOLI LI HEIGHT

	if ($("#" + obj[idPassato]).data('valori').lunghezzaGallery > $(
			".galleria_outer").width()) {

		$('#' + obj[idPassato] + ' .galleria_outer ul:first').clone().appendTo(
				'#' + obj[idPassato] + ' .rullino');
		$('#' + obj[idPassato] + ' .galleria_outer ul:first').clone()
				.prependTo('#' + obj[idPassato] + ' .rullino');
		$('#' + obj[idPassato] + ' .rullino').css(
				"width",
				($("#" + obj[idPassato]).data('valori').lunghezzaGallery * 3)
						+ "px");
		$('#' + obj[idPassato] + ' .galleria_outer ul').css("width",
				$("#" + obj[idPassato]).data('valori').lunghezzaGallery + "px");

		posizionaclip(idPassato);

	} else {
		animazione = "no";
	}

	//SETTA DIMENSIONE SE E' PRESENTE O MENO IL TASTO DI MENU NAVIGAZIONE END	

}

function posizionaclip(idPassato) {

	//$('#' + obj[idPassato] + ' .rullino').css("left", -sommalunghezze+"px");
	$('#' + obj[idPassato] + ' .rullino').css("left",-$("#" + obj[idPassato]).data('valori').lunghezzaGallery + "px");
	$('#' + obj[idPassato] + ' .rullino ul:first').css("left", "0px");
	$('#' + obj[idPassato] + ' .rullino ul:odd').css("left", $("#" + obj[idPassato]).data('valori').lunghezzaGallery + "px");
	$('#' + obj[idPassato] + ' .rullino ul:last').css("left", ($("#" + obj[idPassato]).data('valori').lunghezzaGallery * 2)+ "px");

	if (animazione != "no") {
		onFooEndFunc();
	}

	assegnaFunzioni(idPassato)

}

function assegnaFunzioni(idPassato) {

	$("#" + obj[idPassato] + " .btnNext").click(function() {

		//console.log(obj[idPassato]);

		if (animazionestato == 0) {
			if (animazione != "no") {
				stopClip();
			}

			backCliccato = 0;
			animaClip("next", idPassato);

		}

	});

	$("#" + obj[idPassato] + " .btnBack").click(function() {

		//console.log(obj[idPassato]);

		if (animazionestato == 0) {
			if (animazione != "no") {
				stopClip();
			}

			backCliccato = 1;
			animaClip("back", idPassato);

		}

	});

}

function animaClip(valpas, idPassato) {

	
	animazionestato = 1;


	if ($("#" + obj[idPassato]).data('valori').currentFrame == $(
			"#" + obj[idPassato]).data('valori').ultimaImmagine) {

		//curClip = 0	;

		$("#" + obj[idPassato]).data('valori').currentFrame = 0

		valA = $("#" + obj[idPassato]).data('valori').lunghezzaGallery;

		$("#" + obj[idPassato] + " .rullino").css("left", -valA + "px");

	}

	if ($("#" + obj[idPassato]).data('valori').currentFrame == 0
			&& backCliccato == 1) {

		$("#" + obj[idPassato]).data('valori').currentFrame = $(
				"#" + obj[idPassato]).data('valori').ultimaImmagine;

		valB = $("#" + obj[idPassato]).data('valori').lunghezzaGallery;

		$("#" + obj[idPassato] + " .rullino").css("left", -valB + "px");

	}

	if ($("#" + obj[idPassato]).data('valori').currentFrame == 0) {

		valC = $("#" + obj[idPassato]).data('valori').lunghezzaGallery;

		$("#" + obj[idPassato] + " .rullino").css("left", -valC + "px");

	}

	if (animazione == "yes" || animazione == "YES" || animazione == "Yes") {
		
		//animaAuto();
		//executionTimer = setTimeout("animaAuto()", vel);
		onFooEndFunc();

	} else if (animazione == "no" || animazione == "NO" || animazione == "No") {

	}/**/

	if (valpas == "next") {

		$("#" + obj[idPassato] + " .rullino").animate( {
			"left" : "-=" + (larghezza + offsetClip) + "px"
		}, "slow", function() {
			animazionestato = 0;
		})

		$("#" + obj[idPassato]).data('valori').currentFrame++;

	} else if (valpas == "back") {

		$("#" + obj[idPassato] + " .rullino").animate( {
			"left" : "+=" + (larghezza + offsetClip) + "px"
		}, "slow", function() {
			animazionestato = 0;
		})

		$("#" + obj[idPassato]).data('valori').currentFrame--;

	} else {

		if (direzione == "rtl") {
			$("#" + obj[idPassato] + " .rullino").animate( {"left" : "-=" + (larghezza + offsetClip) + "px"}, "slow", function() {
				animazionestato = 0;
			})
		} else if (direzione == "ltr") {
			$("#" + obj[idPassato] + " .rullino").animate( {"left" : "+=" + (larghezza + offsetClip) + "px"}, "slow", function() {
				animazionestato = 0;
			})
		}

		$("#" + obj[idPassato]).data('valori').currentFrame++;

	}

	

}


function animaAuto(){

		//executionTimer = setTimeout("animaAuto()", vel);
		onFooEndFunc();
		sistemaPosizione();

}




function sistemaPosizione(){
	
	for(i=0;i<obj.length;i++){
		
		
		animazionestato = 0;
		
		if ($("#" + obj[i]).data('valori').currentFrame == $(
			"#" + obj[i]).data('valori').ultimaImmagine) {
		
		//curClip = 0	;

		$("#" + obj[i]).data('valori').currentFrame = 0

		valA = $("#" + obj[i]).data('valori').lunghezzaGallery;

		$("#" + obj[i] + " .rullino").css("left", -valA + "px");

	}

/*	if ($("#" + obj[i]).data('valori').currentFrame == 0 && backCliccato == 1) {
		
		console.log(" backCliccato == 1")
		
		$("#" + obj[i]).data('valori').currentFrame = $(
				"#" + obj[i]).data('valori').ultimaImmagine;

		valB = $("#" + obj[i]).data('valori').lunghezzaGallery;

		$("#" + obj[i] + " .rullino").css("left", -valB + "px");

	}*/

	if ($("#" + obj[i]).data('valori').currentFrame == 0) {

	

		valC = $("#" + obj[i]).data('valori').lunghezzaGallery;

		$("#" + obj[i] + " .rullino").css("left", -valC + "px");

	}

	if (animazione == "yes" || animazione == "YES" || animazione == "Yes") {
		
		//animaAuto();
		//executionTimer = setTimeout("animaAuto()", vel);
		onFooEndFunc();

	} else if (animazione == "no" || animazione == "NO" || animazione == "No") {

	}/**/

	if (direzione == "rtl") {
			$("#" + obj[i] + " .rullino").animate( {"left" : "-=" + (larghezza + offsetClip) + "px"}, "slow", function() {
				animazionestato = 0;
			})
	} else if (direzione == "ltr") {
		$("#" + obj[i] + " .rullino").animate( {"left" : "+=" + (larghezza + offsetClip) + "px"}, "slow", function() {
			animazionestato = 0;
		})
	}

	$("#" + obj[i]).data('valori').currentFrame++;

	
	
	}//FINEFOR	
	
}









function BACKKKsistemaPosizione(){
	
	for(i=0;i<obj.length;i++){
	
		//valB = $("#" + obj[i]).data('valori').lunghezzaGallery
		//console.log(valB)
	
		if ($("#" + obj[i]).data('valori').currentFrame == $("#" + obj[i]).data('valori').ultimaImmagine) {
			
				
				
				$("#" + obj[i]).data('valori').currentFrame = 0
		
				valA = $("#" + obj[i]).data('valori').lunghezzaGallery;
		
				$("#" + obj[i] + " .rullino").css("left", -valA + "px");
		
		}
		if ($("#" + obj[i]).data('valori').currentFrame == 0 && backCliccato == 1) {

				$("#" + obj[i]).data('valori').currentFrame = $("#" + obj[i]).data('valori').ultimaImmagine;

				valB = $("#" + obj[i]).data('valori').lunghezzaGallery;

				$("#" + obj[i] + " .rullino").css("left", -valB + "px");

		}
		if ($("#" + obj[i]).data('valori').currentFrame == 0) {

			valC = $("#" + obj[i]).data('valori').lunghezzaGallery;
	
			$("#" + obj[i] + " .rullino").css("left", -valC + "px");

		}
		
		else{
		
				
			
		}
		
			
		
		
		if (direzione == "rtl") {
			$(".rullino").animate( {"left" : "-=" + (larghezza + offsetClip) + "px"}, "slow", function() {
			animazionestato = 0;
		})
			
		} else if (direzione == "ltr") {
			$(".rullino").animate( {"left" : "+=" + (larghezza + offsetClip) + "px"}, "slow", function() {
			animazionestato = 0;
		})
		
		}
		
		
		
			
	}	
	
}






function stopClip() {
	animazionestato = 0;
	//if(executionTimer){
	//	clearTimeout(executionTimer);
	//}
}

var onFooEndFunc = function() {
	var delay = vel; /* milliseconds - vary as desired */
	var executionTimer;

	return function() {
		if (executionTimer) {
			clearTimeout(executionTimer);
		}
		executionTimer = setTimeout(function() {
			animaAuto();
		}, delay);
	};
}();


function time(sec) {
	return sec * 1000;
}
$(document).ready(function() {
	bl_primo();
})
$(window).load(function() {
	creArray();
	//creaRullini();
	//creaRullino();
});

