// ------------------------------------------------------------------------------------------
// PNGanimation_rnd(divID,imgSrcID,targetFolder,durationFrames,stepFade,titleOn)
// Questa funzione anima delle png che si trovano dentro uno specifico folder SEQUENZA RANDOM
// divID = id del div che contiene l'immagine
// imgSrcID = id dell'immagine
// targetFolder = path del folder
// durationFrames = durata dei frame intermedi
// stepFade = incremento degli step dissolvenza (da 1 a 100)
// titleOn = se l'immagine (oltre a ALT) contiene il tag titleOn valori 0 / 1
// ------------------------------------------------------------------------------------------
function flypageSlide(divID,imgSrcID,productMasterImage,durationFrames,stepFade,titleOn) {
	
	this.targetFolder = "components/com_virtuemart/shop_image/product/";
	this.divID = divID;
	this.imgSrcID = imgSrcID;
	this.productMasterImage = productMasterImage;
	this.filesArray = new Array();
	this.durationFrames = durationFrames;
	this.stepFade = stepFade;
	this.titleOn = titleOn;
	this.opacity = 1;
	
	// ajax - funzione di scansione directory con file php
	this.scanDirectory = function() {
	
	 	if (window.XMLHttpRequest) { // Mozilla, Safari,...
			this.http_request = new XMLHttpRequest();
			if (this.http_request.overrideMimeType) {
				this.http_request.overrideMimeType('text/xml');
			}
		} else if (window.ActiveXObject) { // IE
			try {
				this.http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					this.http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}
		// LANCIA RICHIESTA...
		var _obj = this;
		this.http_request.open('GET', "code/scanDir.php"+"?dir=../"+this.targetFolder, true);
		this.http_request.send(null);
		// ...GESTISCE RISPOSTA
		this.http_request.onreadystatechange = function() {
			if (this.readyState == 4) {
				if (this.status == 200) {
					
					// scansione risultato php
					this.returnFiles = this.responseText.split(";");
					var tempName = _obj.productMasterImage.split(".")[0];
								
					for (scan=0;scan<this.returnFiles.length;scan++){
						if(this.returnFiles[scan].indexOf(tempName)>-1) {
							if(this.returnFiles[scan]!=_obj.productMasterImage){
								_obj.filesArray.push(this.returnFiles[scan]);
							} else {
								_obj.filesArray.unshift(this.returnFiles[scan]);
							}
						}
					}
					if(_obj.filesArray.length >1){
						
						_obj.inizializeImage();
					}
				} else {
					alert('errore');
				}
			}
		}
	}
	
	this.inizializeImage = function() {
	   var _obj = this;
		document.getElementById(this.imgSrcID).src = this.targetFolder+"/"+escape(this.filesArray[0]);
		document.getElementById(this.imgSrcID).alt = this.filesArray[0].substring(0,this.filesArray[0].toString().length-4);
		if (this.titleOn == 1){
			document.getElementById(this.imgSrcID).title = this.filesArray[0].substring(0,this.filesArray[0].toString().length-4);
		}		
		this.filesArray.push(this.filesArray.shift());
		if( this.checkFirstImage != true) {
			document.getElementById(this.divID).style.opacity = "1";
			document.getElementById(this.divID).style.filter = "alpha=100";
			// al termine del caricamento lancia una pausa durationFrames al termine della quale esegue la funzione fadeOut temporeggiata in 20ms
			document.getElementById(this.imgSrcID).onload = function(){window.setTimeout(function(){_obj.fadeOutTimer = window.setInterval(function(){_obj.fadeOut();},20);},_obj.durationFrames);}
			this.checkFirstImage = true
		} else {
			document.getElementById(this.imgSrcID).onload = function(){_obj.fadeInTimer = window.setInterval(function(){_obj.fadeIn();},20);}
		}
	}
	
	// fade out div e cambio immagine
	this.fadeOut = function() {
		if(this.opacity>0){
			this.opacity -= this.stepFade/100;
			document.getElementById(this.divID).style.opacity = this.opacity;
			document.getElementById(this.divID).style.filter = "alpha(opacity="+parseInt(this.opacity*100)+")"; 
		} else {
			window.clearInterval(this.fadeOutTimer);
			this.inizializeImage();
		}
	}
	
	this.fadeIn = function() {
		if(this.opacity<1){
			this.opacity += this.stepFade/100;
			document.getElementById(this.divID).style.opacity = this.opacity;
			document.getElementById(this.divID).style.filter = "alpha(opacity="+parseInt(this.opacity*100)+")"; 
		} else {
			window.clearInterval(this.fadeInTimer);
			var _obj = this;
			window.setTimeout(function(){_obj.fadeOutTimer = window.setInterval(function(){_obj.fadeOut();},20);},_obj.durationFrames);
		}
	}
	// lancia il progresso delle funzioni con la scansione dei files dell'animazione
	this.scanDirectory();
}














function browseSlide(divID,imgSrcID,productMasterImage,durationFrames,stepFade,titleOn) {
	
	this.targetFolder = "components/com_virtuemart/shop_image/product/";
	this.divID = divID;
	this.imgSrcID = imgSrcID;
	this.productMasterImage = productMasterImage;
	this.filesArray = new Array();
	this.durationFrames = durationFrames;
	this.stepFade = stepFade;
	this.titleOn = titleOn;
	this.opacity = 1;
	
	// ajax - funzione di scansione directory con file php
	this.scanDirectory = function() {

	 	if (window.XMLHttpRequest) { // Mozilla, Safari,...
			this.http_request = new XMLHttpRequest();
			if (this.http_request.overrideMimeType) {
				this.http_request.overrideMimeType('text/xml');
			}
		} else if (window.ActiveXObject) { // IE
			try {
				this.http_request = new ActiveXObject("Msxml2.XMLHTTP");
			  } catch (e) {
				try {
				  this.http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}
		// LANCIA RICHIESTA...
		
		
		var _obj = this;
		// LANCIA RICHIESTA...
    	this.http_request.open('POST', "code/scanDir.php", true);
    	this.http_request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    	this.http_request.send("dir=../"+this.targetFolder); 
	
		// ...GESTISCE RISPOSTA
		this.http_request.onreadystatechange = function() {
			if (this.readyState == 4) {
				if (this.status == 200) {
					// scansione risultato php
					this.returnFiles = this.responseText.split(";");
					var tempName = _obj.productMasterImage.split(".")[0];
								
					for (scan=0;scan<this.returnFiles.length;scan++){
						if(this.returnFiles[scan].indexOf(tempName)>-1) {
							if(this.returnFiles[scan]!=_obj.productMasterImage){
								_obj.filesArray.push(this.returnFiles[scan]);
							} else {
								_obj.filesArray.unshift(this.returnFiles[scan]);
							}
						}
					}
					if(_obj.filesArray.length >1){
						_obj.inizializeImage();
					}
				} else {
					alert('errore');
				}
			}
		}
	}
	
	this.inizializeImage = function() {
	   var _obj = this;
		document.getElementById(this.imgSrcID).src = this.targetFolder+"/"+escape(this.filesArray[0]);
		document.getElementById(this.imgSrcID).alt = this.filesArray[0].substring(0,this.filesArray[0].toString().length-4);
		if (this.titleOn == 1){
			document.getElementById(this.imgSrcID).title = this.filesArray[0].substring(0,this.filesArray[0].toString().length-4);
		}		
		this.filesArray.push(this.filesArray.shift());
		if( this.checkFirstImage != true) {
			// al termine del caricamento lancia una pausa durationFrames al termine della quale esegue la funzione fadeOut temporeggiata in 20ms
			document.getElementById(this.imgSrcID).onload = function(){window.setTimeout(function(){_obj.fadeOutTimer = window.setInterval(function(){_obj.fadeOut();},20);},_obj.durationFrames);}
			this.checkFirstImage = true
		} else {
			document.getElementById(this.imgSrcID).onload = function(){_obj.fadeInTimer = window.setInterval(function(){_obj.fadeIn();},20);}
		}
	}
	
	// fade out div e cambio immagine
	this.fadeOut = function() {
		if(this.opacity>0){
			this.opacity -= this.stepFade/100;
			targetDiv = document.getElementById(this.divID);
			targetDiv.style.opacity = this.opacity;
			targetDiv.style.filter = "alpha(opacity="+parseInt(this.opacity*100)+")"; 
			
		} else {
			window.clearInterval(this.fadeOutTimer);
			this.inizializeImage();
		}
	}
	
	this.fadeIn = function() {
		if(this.opacity<1){
			this.opacity += this.stepFade/100;
			targetDiv = document.getElementById(this.divID)
			targetDiv.style.opacity = this.opacity;
			targetDiv.style.filter = "alpha(opacity="+parseInt(this.opacity*100)+")"; 
			
		} else {
			window.clearInterval(this.fadeInTimer);
			var _obj = this;
			window.setTimeout(function(){_obj.fadeOutTimer = window.setInterval(function(){_obj.fadeOut();},20);},_obj.durationFrames);
		}
	}
	// lancia il progresso delle funzioni con la scansione dei files dell'animazione
	this.scanDirectory();
}

