/*var tab_containers_id = [["idCont", tab, 
"sildes[0]"
var slides = new Array();
charge_img = function(tab_containers_id) {
	var i=0;
	while(i < tab_containers_id.length){
		slides[i] = new Slideshow(tab[i][0]
		i++;
	}

}*/

Slideshow = function(idContainer, imgs, width, height, objectVarName, pause) {
	if(idContainer) {
		this.init(idContainer, imgs, width, height, objectVarName, pause);
	}
}

Slideshow.prototype = {
	/*
	 * Container where to write slideshow
	 */
	idContainer : null,
	/*
	 * Table of pictures
	 *
	 * imgs[][0] : image path
	 * imgs[][1] : link
	 * imgs[][2] : target for the link
	 */
	imgs : null,
	
	/*
	 * Element containing image 1
	 */
	img0 : null,
	/*
	 * Element containing image 2
	 */
	img1 : null,
	/*
	 * Image currently at top
	 */
	currentImg : null,
	/*
	 * Id of the picture in the table currently displayed
	 */
	idImg : null,
	
	/*
	 * Interval Id
	 */
	slide: null,
	/*
	 * Name of the var
	 */
	objectVarName: null,
	/*
	 * Time to wait
	 */
	pause : null,
	
	preloadedImgs: null,
	
	
	/*
	 * Initialisation
	 */
	init : function(idContainer, imgs, width, height, objectVarName, pause) {
		this.idContainer = idContainer;
		this.imgs = imgs;
		this.width = width;
		this.height = height;
		
		this.objectVarName = objectVarName;
		
		this.pause = pause;
		
		this.currentImg = 0;
		this.idImg = 0;
		
		this.preload();
		
		this.create();
	},
	
	/*
	 * Preload imgs not to loose time after...
	 */
	preload : function() {
		this.preloadedImgs = new Array();
		for(var i = 0; i < this.imgs.length; i++) {
			this.preloadedImgs[i] = new Image();
			this.preloadedImgs[i].src = this.imgs[i][0];
		}
	},
	
	/*
	 * Create the structure
	 */
	create : function() {
		var container = document.createElement('div');
		container.style.width = this.width + 'px';
		container.style.height = this.height + 'px';
		//container.style.overflow = 'hidden';
		
		this.img0 = document.createElement('div');
		this.img0.style.position = 'absolute';
		//this.img0.style.top = 0;
		//this.img0.style.left = 0;
		this.img0.width = this.width + 'px';
		this.img0.height = this.height + 'px';
		
		this.img1 = document.createElement('div');
		this.img1.style.position = 'absolute';
		//this.img1.style.top = 0;
		//this.img1.style.left = 0;
		this.img1.width = this.width + 'px';
		this.img1.height = this.height + 'px';
		
		container.appendChild(this.img0);
		container.appendChild(this.img1);
		
		document.getElementById(this.idContainer).appendChild(container);
		
		setOpacity(this.img0, 1);//passé à 1 pour que la première img soit visible
		setOpacity(this.img1, 0);
		this.setCurrentImg();//rajouté pour charger la première img (et donc enlevé de la fonction start)
	},
	
	setCurrentImg : function() {
		var id = this.idImg;
		var imgCont = eval('this.img' + this.currentImg);
		var img = document.createElement('img');
		img.width = this.width + 'px';
		img.height = this.height + 'px';
		img.style.width = this.width + 'px';
		img.style.height = this.height + 'px';
		img.src = this.preloadedImgs[id].src;
		//img.src = this.imgs[id][0];
		
		// clear imgCont
		while(imgCont.childNodes.length > 0) {
			imgCont.removeChild(imgCont.childNodes[0]);
		}
		
		if(this.imgs[id][1] == "") {
			imgCont.appendChild(img);
		} else {
			var a = document.createElement('a');
			a.href = this.imgs[id][1];
			a.target = this.imgs[id][2];
			a.appendChild(img);
			imgCont.appendChild(a);
		}
	},
	
	start : function() {
		// set the first img
		//this.setCurrentImg();
		this.transition();
	},
	
	transition : function() {
		var inter = 'setInterval("' + this.objectVarName + '.fadeIn()", 25);';
		this.slide = eval(inter);
	},
	
	swap : function() {
		this.currentImg = this.currentImg == 0 ? 1 : 0;
		this.idImg++;
		if(this.idImg == this.imgs.length) {
			this.idImg = 0;
		}
		this.setCurrentImg();
		var tmp = 'setTimeout("' + this.objectVarName + '.transition()", ' + this.pause + ');';
		eval(tmp);
	},
	
	fadeIn : function() {
		var imgCont = eval('this.img' + this.currentImg);
		var n = this.currentImg == 0 ? 1 : 0;
		var img2 = eval('this.img' + n);
		if(getOpacity(imgCont) < 1) {
			setOpacity(imgCont, parseFloat(getOpacity(imgCont)) + 0.05);
			setOpacity(img2, parseFloat(getOpacity(img2)) - 0.05);
		} else {
			clearInterval(this.slide);
			imgCont.style.zIndex++;
			this.swap();
		}
	}
};

setOpacity = function(o, opacity) {
	if(opacity < 0) {
		opacity = 0;
	}
	if(document.all) {
		o.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + opacity*100 + ")";
	} else {
		o.style.opacity = opacity;
	}
}

getOpacity = function(o) {
	if(document.all) {
		return (o.filters.item("DXImageTransform.Microsoft.Alpha").opacity) / 100;
	} else {
		return o.style.opacity;
	}
}
//}