// funzione shuffle per array
shuffle = function(v){
    for(var j, x, i = v.length; i; j = parseInt(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x);
    return v;
};

// ajax - funzione di scansione directory con file php
function scanDirectory(url,directory) {
	// INTESTAZIONE AXAJ
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// Vedi note sotto
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	// CONTROLLO DEBUG
	if (!http_request) {
		alert('Giving up :( Non riesco a creare una istanza XMLHTTP');
		return false;
	}
	// LANCIA RICHIESTA...
	http_request.open('GET', "php/scanDir.php"+"?dir="+directory, true);
	http_request.send(null);
	// ...GESTISCE RISPOSTA
	http_request.onreadystatechange = function() {
		if (http_request.readyState == 4) {
				if (http_request.status == 200) {
					scansioneDirectory(http_request);
				} else {
					alert('Si è verificato un problema con la richiesta');
			}
		}
	};
	// scansione risultato php: ricostruisce la variabile e lancia galleria
	function scansioneDirectory(http_request) {
		files = http_request.responseText.split("|");
		startGallery(files);
	}
}


function startGallery(files) {
	galleryFiles = files.copy();
	shuffle(galleryFiles);
	numImg = 1;
	// funzione di sicurezza: controlla che siano avvenuti i primi caricamenti prima di cambiare l'handler onload
	checkCaricamentoInizialeSicurezza = 0;
	//1o settaggio
	for(scan=1;scan<=4;scan++){
		if(galleryFiles.length ==0){
			galleryFiles = files.copy();
		}
		document.getElementById("gallery"+scan).src = "modules/mod_oziogallery/galleryProdotti/"+escape(galleryFiles.pop());
		document.getElementById("gallery"+scan).onload = function(){checkCaricamentoIniziale();}
	}
}

// cambia l'handler onload delle prime 4 immagini solo dopo che sono state caricate
function checkCaricamentoIniziale(){
	checkCaricamentoInizialeSicurezza ++;
	if(checkCaricamentoInizialeSicurezza>=4){
		galleryTimer = window.setInterval("changeGallery()",4000);
	}
		
}

// fa ripartire la funzione divisa in [changeGallery -> fadeOut -> checkCaricamentoProgressivo -> fadeIn] e daccapo.
function changeGallery() {

	if(galleryFiles.length ==0){
		galleryFiles = files.copy();
		shuffle(galleryFiles);
		
	}
	window.clearInterval(galleryTimer);
	fadeValue = 1;
	fadeOutTimer = window.setInterval("fadeOut()",20);
	fadeOut();
}

// fade out div e cambio immagine
function fadeOut() {
	
	if(fadeValue>0){
		fadeValue -= 0.1;
		document.getElementById("divGallery"+numImg).style.opacity = fadeValue;
		document.getElementById("divGallery"+numImg).style.filter = "alpha(opacity="+fadeValue*100+")"; 
	} else {
		window.clearInterval(fadeOutTimer);
		document.getElementById("gallery"+numImg).src = "modules/mod_oziogallery/galleryProdotti/"+escape(galleryFiles.pop());
		document.getElementById("gallery"+numImg).onload = function(){checkCaricamentoProgressivo();}
	}
	
}

// attende che l'immagine sia stata caricata, poi lancia fadein
function checkCaricamentoProgressivo(){
	fadeInTimer = window.setInterval("fadeIn()",20);
		
}

// fade in immagini caricate
function fadeIn() {
	if(fadeValue<1){
		fadeValue += 0.1;
		document.getElementById("divGallery"+numImg).style.opacity = fadeValue;
		document.getElementById("divGallery"+numImg).style.filter = "alpha(opacity="+fadeValue*100+")"; 
	} else {
		window.clearInterval(fadeInTimer);
		numImg++;
		if (numImg > 4) {
			numImg = 1;
			galleryTimer = window.setInterval("changeGallery()",2000);
		} else {
			changeGallery();
		}
	}
}


scanDirectory("php/scanDir.php","../modules/mod_oziogallery/galleryProdotti");
