//-------------------------------------------------------------
/* per slide										*/
/* richiede esternamente creazione dell'array		*/
/* var ImgArray = new Array();						*/
/* ImgArray[0]="resources/images/fotogallery1.jpg"; */
var indx = 0;

	// avanzamento immagine
	function doNext()
	{	
		if (indx == ImgArray.length - 1)
			indx = 0;
		else
			indx = indx + 1;	
			
		refreshData();
	}
	
	// arretramento immagine (M.Casati)
	function doPrevious()
	{
		if (indx == 0)
			indx = ImgArray.length - 1;
		else
			indx = indx -1;	
		refreshData();		
	}
	
	function refreshData()
	{
		var currentImgSrc = ImgArray[indx];
		
		swapImage(currentImgSrc);
	}
	
	
	// funzione gestione immagini
	function swapImage(source)
	{
		 var obj = document.images["myImg"];
		 var newImg = new Image();
		 newImg.src = source;
		 obj.src = newImg.src;	  
	}

//-------------------------------------------------------------


/* per slideshow automatico della home */
function mran(ma,mi)   {return(Math.round(Math.random()*(ma-mi))+mi)}

var Slideshow = Class.create({
	 initialize: function(delay, totalElmt, elmtName) {
		this.delay = delay;
		this.totalElmt = totalElmt;
		this.paused = 0;
		this.arrSlideElmt = [totalElmt];
		this.curElmtNum = 1;//mran(totalElmt, 1); //randomize first element
		//preload all elements into array and preload all images within element
		for (i = 1; i <= totalElmt; i++) {
			this.arrSlideElmt[i] = $(elmtName + i);
			this.arrSlideElmt[i].observe('mouseover', function() { this.paused = 1; }.bind(this));  //pause on mouseover
			this.arrSlideElmt[i].observe('mouseout', function() { this.paused = 0; }.bind(this));  //resume on mouseout
			$$(this.arrSlideElmt[i].img).each(function(img) {	
				img = new Image();
			});			
		}
		$$('img.arrow').each(function(node) {  //looks for all arrows (pointing right) and handles click event
			node.observe('click', function(s){
				this.arrSlideElmt[this.curElmtNum].setStyle({
				  display: 'none'
				});
				this.checkSlide();
				this.arrSlideElmt[this.curElmtNum].setStyle({
				  display: 'block'
				});
				s.stop();
			}.bind(this));
		}.bind(this));			
	 },
	start: function() {
		//show first element without effect
		this.arrSlideElmt[this.curElmtNum].setStyle({
		  display: 'block'
		});
		this.executor = new PeriodicalExecuter(function() { 
			this.next(); //start slidehow
		}.bind(this), this.delay);		
	},
	
	next: function(){
		if (!this.paused) {
			this.update();
		}
	},
	update: function() {
		new Effect.Fade(this.arrSlideElmt[this.curElmtNum],{duration:.5, afterFinish:function(){
			this.checkSlide();
			//	$("nomeimmagineslide").innerHTML=$('immagine'+this.curElmtNum).src;
			new Effect.Appear(this.arrSlideElmt[this.curElmtNum],{duration:.5});
			//	alert(this.curElmtNum);
		}.bind(this)});
	},
	checkSlide: function() {
		if (this.curElmtNum == this.totalElmt) { this.curElmtNum = 1; }
		else { this.curElmtNum ++; }		
	}
});



