/*
 *  JavaScript Dom Carousel
 *  By Piero Bozzolo
 *  Ogilvy Interactive - 2009
 *  
 *  uso:
 *  1) includere nella pagina html i seguenti script:
		- prototype.js
		- scriptaculous.js
		- effects.js (scriptaculous - nel caso si includino gli script non nell head della pagina
		- il file corrente
		
	2) preparare un elemento html (ad esempio un div) e assegnargli un ids
	3) inserire all'interno dell'elemento gli elementi da far 'girare'
	4) instanziare un oggetto carousel come segue
		istanza_oggetto = new Carousel('istanza_oggetto');
		
		ad esempio:
		titles = new Carousel('titles');
		
	5) chiamare la funzione istanza_oggetto.starCarousel(carouselDivId, delay, interval, effectDuration, resumeInterval, stopAndGoInterval)
		parametri:
		
		carouselDivId = div i cui elementi figli devono essere fatti routare
		delay = ritardo partenza rotazione (in millisencondi oppure true/false per un tempo casuale)
		effectDuration
		resumeInterval
		stopAndGoInterval
 */
 
Carousel = function(name,interval){
	if(!name){throw "USAGE: instance_name = new Carousel('instance_name');"; }
	this.name = name;
	this.currentCarouselViewedImage = 0;
	this.carouselPlayingSem = 1;
	this._carouselDivId;
	this.interval = interval;
	this.effectDuration = 1100;
	this.resumeInterval = 3000;
	this.stopAndGoInterval = 10000;

	Carousel.prototype.carouselPlaying = function(){
		if(this.carouselPlayingSem == 0) return true;
		return false;
	}

	
	Carousel.prototype.startCarousel = function(carouselDivId, delay, interval, effectDuration, resumeInterval, stopAndGoInterval ){
		if(interval){this.interval = interval;}
		if(effectDuration){this.effectDuration = effectDuration;}
		if(resumeInterval){this.resumeInterval = resumeInterval;}
		if(stopAndGoInterval){this.stopAndGoInterval = stopAndGoInterval;}
		
		if(!carouselDivId) carouselDivId = this._carouselDivId;
		this._carouselDivId = carouselDivId;
		this.carouselPlayingSemDown();
		this.listOfImages = $(carouselDivId).childElements();
		this.listOfImages.each(function(image) {
			image.hide();
		});
		startDelay = 0;
		if(typeof delay == "number"){ startDelay = delay;}
		if(typeof delay == "boolean"){ startDelay = this.getRandomStartDelay();}			
		setTimeout(this.name + ".imageCarousel(" + this.name + ".listOfImages)", startDelay);
	}
	
	Carousel.prototype.getRandomStartDelay = function(){
		r_time = Math.random() * 10000;
		if(r_time >  this.interval){r_time = (r_time / 2)}
		return Math.ceil(r_time);			
	}

	Carousel.prototype.imageCarousel = function(listOfImages){
		if(!this.carouselPlaying()) return;
		this.listOfImages[this.currentCarouselViewedImage].fade();
		this.setNextIndex();
		setTimeout(this.name + ".listOfImages[" + this.currentCarouselViewedImage + "].appear();", this.effectDuration);
		setTimeout(this.name + ".imageCarousel(" + this.name + ".listOfImages)", this.interval); //intervallo tra una diapositiva e l'altra
	}

	Carousel.prototype.setNextIndex = function(){
		this.currentCarouselViewedImage = (this.currentCarouselViewedImage + 1) % this.listOfImages.size();
	}

	Carousel.prototype.setPrevIndex = function(){
		this.currentCarouselViewedImage = (this.currentCarouselViewedImage - 1);
		if(this.currentCarouselViewedImage < 0){
			this.currentCarouselViewedImage = this.listOfImages.size() + this.currentCarouselViewedImage;
		}
	}

	Carousel.prototype.carouselBack = function(){
		this.stopCarousel();
		this.listOfImages[this.currentCarouselViewedImage].hide();
		this.setPrevIndex();
		this.listOfImages[this.currentCarouselViewedImage].show();
		setTimeout("this.startCarousel()", this.resumeInterval);
	}

	Carousel.prototype.carouselNext = function(){
		this.stopCarousel();
		this.listOfImages[this.currentCarouselViewedImage].hide();
		this.setNextIndex();
		this.listOfImages[this.currentCarouselViewedImage].show();
		setTimeout(this.name + ".startCarousel()", this.resumeInterval);
	}

	Carousel.prototype.stopCarousel = function(){
		this.carouselPlayingSemUp();
	}

	Carousel.prototype.carouselPlayingSemDown = function(){
		this.carouselPlayingSem--;
		if(this.carouselPlayingSem < 0) this.carouselPlayingSem = 0;
	}

	Carousel.prototype.carouselPlayingSemUp = function(){
		this.carouselPlayingSem++;
	}

	Carousel.prototype.carouselGoto = function(elementID){
		element2Show = $(elementID);
		for(var i=0; i < listOfImages.length; i++){
			if(element2Show.identify() == this.listOfImages[i].identify()){
					this.listOfImages[this.currentCarouselViewedImage].hide();
					this.currentCarouselViewedImage = i;
					this.listOfImages[this.currentCarouselViewedImage].show();
					this.carouselPlayingSemUp();
					setTimeout(this.name + ".startCarousel()", this.stopAndGoInterval);
			}
		}
	}
}
