

// declaring the class
var SlideShow = Class.create();

// implementing the class
SlideShow.prototype = {
	initialize: function(element, data) {
		// playMode : mode de lecture (0 pour passage automatique et 1 pour passage manuel de la diapositive)
	    this.playMode = 0;
		this.currentIter = 0;
		this.lastIter = 0;
		this.maxIter = 0;
		this.slideShowElement = element;
		this.slideShowData = data;
		this.slideShowInit = 1;
		this.slideElements = Array();
		this.slideInfoZone = "";
		
		this.stretchEnabled = true;
		
		this.debug = false;
		
		// pour la lecture automatique
		this.slideShowDelay = 4000;
		this.playStopButton = '';

		element.style.display="block";

		this.maxIter = data.length;
		this.lastIter = this.maxIter - 1;
		for(i=0;i<data.length;i++)
		{
			var currentImg = null;
			if (this.stretchEnabled) {
				currentImg = document.createElement('img');
				currentImg.src=data[i][0];
			} else {
				currentImg = document.createElement('div');
				currentImg.style.backgroundImage="url('" + data[i][0] + "')";
			}
			currentImg.className = "slideElement";
			currentImg.style.position="absolute";
			currentImg.style.width = element.style.width;
			currentImg.style.height = element.style.height;
			currentImg.style.margin="0px";
			currentImg.style.border="0px";
			currentImg.style.backgroundPosition="center center";
			currentImg.style.backgroundRepeat =  'no-repeat';
			element.appendChild(currentImg);
			new Effect.Opacity(currentImg, { from: 1.0, to: 0.0, duration: 0.0 });
			//currentImg.hide();
			this.slideElements[parseInt(i)] = currentImg;
		}
		//new Effect.Opacity(this.slideElements[0],{ from: 0.0, to: 1.0, duration: 0.0 });
		//this.slideElements[0].show();
		if (data.length>1)
		{
			
			var leftArrow = document.createElement('a');
			leftArrow.className = 'left';
			leftArrow.onclick = this.pushPrevSlideShow.bind(this);
			element.appendChild(leftArrow);

			var rightArrow = document.createElement('a');
			rightArrow.className = 'right';
			rightArrow.onclick = this.pushNextSlideShow.bind(this);
			element.appendChild(rightArrow);
			
			var center = document.createElement('a');
			center.className = 'stop';
			center.onclick = this.stopSlideShow.bind(this);
			element.appendChild(center);
			this.playStopButton = center;
		}
		this.loadingElement = document.createElement('div');
		this.loadingElement.className = 'loadingElement';
		element.appendChild(this.loadingElement);

		this.slideInfoZone = document.createElement('div');
		this.slideInfoZone.className = 'slideInfoZone';
		element.appendChild(this.slideInfoZone);
		this.slideInfoZone.style.display = 'none';
		
		/* on montre le 1er slide */
		this.showFirst();
		this.startSlideShowInAutomaticMode(true); /* on laisse le 1er slide affiche */
	},
	
	destroySlideShow: function(element) {
		var myClassName = element.className;
		var newElement = document.createElement('div');
		newElement.className = myClassName;
		element.parentNode.replaceChild(newElement, element);
	},
	
	// les evenements sur les fleches ne sont pris en consideration uniquement lorsque le play_mode est en manuel
	pushNextSlideShow: function() {
		if (this.debug) alert('pushNextSlideShow');
		if (0 == this.playMode) {
			this.stopSlideShow();
		}
	    if (0 != this.playMode) {
			setTimeout(this.hideInfoSlideShow.bind(this),10);
			setTimeout(this.nextSlideShow.bind(this),500);
		}
	},
	pushPrevSlideShow: function() {
		if (this.debug) alert('pushPrevSlideShow');
		if (0 == this.playMode) {
			this.stopSlideShow();
		}
		if(0 != this.playMode) {
			setTimeout(this.hideInfoSlideShow.bind(this),10);
			setTimeout(this.prevSlideShow.bind(this),500);
		}
	},
	showFirst: function() {
		if (this.debug) alert('showFirst');
		this.loadingElement.style.display = "none";
		this.slideShowInit = 0;
		this.currentIter = 0;
		this.lastIter = this.maxIter - 1;
		if (1 == this.slideShowInit) { /* 1er passage */
			imgPreloader = new Image();
			// once image is preloaded, start slideshow
			imgPreloader.onload=function(){
				setTimeout(this.showFirst.bind(this),10);
			}.bind(this);
			imgPreloader.src = this.slideShowData[parseInt(this.currentIter)][0];
		} else {
			this.doSlideShow();
		}
	},
	// les evenements sur les fleches ne sont pris en consideration uniquement lorsque le play_mode est en manuel
	startSlideShow: function(donotswitchCurrentSlide) {
		if (this.debug) alert('startSlideShow');
		this.loadingElement.style.display = "none";
		this.slideShowInit = 0;
		//this.slideElements[parseInt(this.currentIter)].setStyle('opacity', 1.0);
		//new Effect.Opacity(this.slideElements[parseInt(this.currentIter)],{ to: 1.0, duration: 0.0 });
		/* on montre le slide suivant */
		this.nextSlideShowPlay(donotswitchCurrentSlide); 	
		/*new Effect.Opacity(this.slideElements[0], { from: 0.0, to: 1.0, duration: 0.3 }); */
	},
	stopSlideShow: function() {
		if (this.debug) alert('stopSlideShow');
		this.playMode = 1;
		this.playStopButton.className = 'play';
		this.playStopButton.onclick = this.startSlideShowInAutomaticMode.bind(this);
	},
	startSlideShowInAutomaticMode: function(donotswitchCurrentSlide) {
		if (this.debug) alert('startSlideShowInAutomaticMode');
		if (0 == this.playMode) {
			this.stopSlideShow();
		}
		this.playMode = 0;
		this.playStopButton.className = 'stop';
		this.playStopButton.onclick = this.stopSlideShow.bind(this);
		this.startSlideShow(donotswitchCurrentSlide);
	},
	nextSlideShowPlay: function(donotswitchCurrentSlide) {
		if (this.debug) alert('nextSlideShowPlay');
		if (0 == this.playMode) {
			if (true != donotswitchCurrentSlide) this.nextSlideShow();
			setTimeout(this.showInfoSlideShow.bind(this),1000);
			setTimeout(this.hideInfoSlideShow.bind(this),this.slideShowDelay-1000);
			/* on programme le prochain changement de slide */
			setTimeout(this.nextSlideShowPlay.bind(this),this.slideShowDelay);
		}
	},
	nextSlideShow: function() {
		if (this.debug) alert('nextSlideShow');
		this.lastIter = this.currentIter;
		this.currentIter++;
		if (this.currentIter >= this.maxIter)
		{
			this.currentIter = 0;
			this.lastIter = this.maxIter - 1;
		}
		this.slideShowInit = 0;
		this.doSlideShow.bind(this)();
	},
	prevSlideShow: function() {
		if (this.debug) alert('prevSlideShow');
		this.lastIter = this.currentIter;
		this.currentIter--;
		if (this.currentIter <= -1)
		{
			this.currentIter = this.maxIter - 1;
			this.lastIter = 0;
		}
		this.slideShowInit = 0;
		this.doSlideShow.bind(this)();
	},
	doSlideShow: function() {
		if (this.debug) alert('doSlideShow');
		/* on montre le slide courant et on cache le precedent */
		new Effect.Opacity(this.slideElements[parseInt(this.currentIter)],{ from: 0.0, to: 1.0, duration: 0.5 });
		new Effect.Opacity(this.slideElements[parseInt(this.lastIter)],{ from: 1.0, to: 0.0, duration: 0.5 });
		//new Effect.Scale(this.slideElements[parseInt(this.currentIter)], 50);
		//setTimeout(this.showInfoSlideShow.bind(this),1000);
	},
	showInfoSlideShow: function() {
		/*this.slideShowElement.removeChild(this.slideInfoZone);
		this.slideInfoZone = document.createElement('div');
		this.slideInfoZone.className = 'slideInfoZone';
		this.slideInfoZone.styles = new fx.Styles(this.slideInfoZone);
		this.slideInfoZone.setStyle('opacity',0);
		var slideInfoZoneTitle = document.createElement('h2');
		slideInfoZoneTitle.innerHTML = this.slideShowData[this.currentIter][2]
		this.slideInfoZone.appendChild(slideInfoZoneTitle);
		var slideInfoZoneDescription = document.createElement('p');
		slideInfoZoneDescription.innerHTML = this.slideShowData[this.currentIter][3];
		this.slideInfoZone.appendChild(slideInfoZoneDescription);
		this.slideShowElement.appendChild(this.slideInfoZone);
		this.slideInfoZone.normalHeight = this.slideInfoZone.getStyle('height', true).toInt();
		this.slideInfoZone.styles.custom({'opacity': [0, 0.7], 'height': [0, this.slideInfoZone.normalHeight]});*/
	},
	hideInfoSlideShow: function() {
		/*this.slideInfoZone.styles.custom({'opacity': [0.7, 0]});
		this.slideInfoZone.styles.custom({'opacity': [0.7, 0], 'height': [this.slideInfoZone.normalHeight, 0]});*/
	},
	setSlideShowDelay:function(delay) {
	    this.slideShowDelay = delay;
	},
	getSlideShowDelay:function() {
	    return this.slideShowDelay;
	}
};