/******************* OBJET CARROUSEL ********************/

function Slideshow(){

	this.slides = new Array();
	this.delay	= 5;	//delai en secondes
	this.chrono = 0; // secondes restantes avant changement
	this.currentSlide = 0;

	this.previousSlide = null;
	this.mode = 'mixte';
	this.transition = 'none';
	this.speed = 1;
	
	this.height = 100;
	this.width = 200;

	return this;
}

Slideshow.prototype.setDelay = function(pDelay){
	if(pDelay>0){
		this.delay = pDelay;
	}
		
}
Slideshow.prototype.setMode = function(ps_Mode){
	this.mode = ps_Mode;
}
Slideshow.prototype.isAutoMode = function(){
	if(this.mode!='manual'){
		return true;
	}
	return false;
}
Slideshow.prototype.isManualMode = function(){
	
	if(this.countSlides()<1){
		return false;
	}
	if(this.mode!='auto'){
		return true;
	}
	return false;
}
Slideshow.prototype.setChrono = function(pSec){
	this.chrono = pSec;
}
Slideshow.prototype.setTransition = function(pTrans){
	this.transition = pTrans;
}
Slideshow.prototype.setSpeed = function(pSpeed){
	if(pSpeed>0){
		this.speed = pSpeed/1000;
	}
}
Slideshow.prototype.addSlide = function(titleCms, pCms){
	var div = document.getElementById('slide-content');	
	if (div){
		var i = this.countSlides();
		var content = "<div id='slide-"+i+"' class='slide' style='display:none;'>"+pCms+"</div>";
		div.innerHTML += content;
	}
	this.slides.push(titleCms);
}
Slideshow.prototype.countSlides = function(){
	return this.slides.length;
}
Slideshow.prototype.getCurrentSlide = function(){
	return this.slides[this.currentSlide];
}
Slideshow.prototype.setCurrentSlide = function(pId){
	this.previousSlide = this.currentSlide;
	this.currentSlide = pId;
}
Slideshow.prototype.isCurrentSlide = function(pId){
	return (this.currentSlide==pId);
}
Slideshow.prototype.setCurrentSlideUp = function(){
	this.previousSlide = this.currentSlide;
	this.currentSlide++;
}
Slideshow.prototype.setCurrentSlideDown = function(){
	this.previousSlide = this.currentSlide;
	this.currentSlide--;
}
Slideshow.prototype.turnLeft = function(){
	var nb = this.countSlides();
	if(this.currentSlide==0){
		this.goTo(nb-1);
	}
	else {
		this.setCurrentSlideDown();
	}
	this.refresh();		
}
Slideshow.prototype.turnRight = function(){
	var nb = this.countSlides();
	if(this.currentSlide==(nb-1)){
		this.goTo(0);
	}
	else {
		this.setCurrentSlideUp();
	}
	this.refresh();		
}
Slideshow.prototype.goTo = function(pId){
	if(pId!=this.currentSlide){
		this.setCurrentSlide(pId);
		this.refresh();
	}
}
Slideshow.prototype.getCommande = function(){
	var html = '';
	//html += "<a href='#' id='prev' class='normal' onclick='slideshow.turnLeft();return false;'><span>&lt;</span></a>";
	for (var i=0;i<this.countSlides();i++){
		//html += "<a href='#' id='button"+i+"' onclick='slideshow.goTo("+i+");return false;'><span>"+(i+1)+"</span></a>";
		html += "<a href='#' id='button"+i+"' onmouseover='slideshow.goTo("+i+");return false;'><span>"+this.slides[i]+"</span></a>";
	}
	//html += "<a href='#' id='next' class='normal' onclick='slideshow.turnRight();return false;'><span>&gt;</span></a>";
	return html;
}
Slideshow.prototype.setCurrentButton = function(){
	for (i=0;i<this.countSlides();i++){
		var button = document.getElementById('button'+i);
		if(this.isCurrentSlide(i)){
			button.setAttribute("class", "active"); 
			button.setAttribute("className", "active");
		}
		else{
			button.setAttribute("class", "normal"); 
			button.setAttribute("className", "normal");
		} 
		
	}
}

Slideshow.prototype.show = function(){
	if(this.isManualMode()){
		div = document.getElementById('middle-center');	
		if (div){
			div.innerHTML += '<div id="slide-commands">'+this.getCommande()+'</div>';
		}
	}
	
	if(this.countSlides()>0){
		this.refresh();
		this.run();	
	}
}

Slideshow.prototype.changeSlide = function(){
	if(this.isManualMode()){	
		this.setCurrentButton();
	}
	if(this.transition=='random'){
		this.makeTransition(this.getRandomTransition());
	}
	else{
		this.makeTransition(this.transition);
	}
}

Slideshow.prototype.makeTransition = function(pTransition){
	
	var oldSlide = document.getElementById('slide-'+this.previousSlide);	
	var newSlide = document.getElementById('slide-'+this.currentSlide);
	
	switch(pTransition)	{
		case 'up':
			this.blindUp(oldSlide,newSlide,this.speed);
			break;
		case 'down':
			this.blindDown(oldSlide,newSlide,this.speed);
			break;		
		case 'fade':
			this.crossFade(oldSlide,newSlide,this.speed);
			break;
		case 'none':
		default:
			this.noEffect(oldSlide,newSlide,this.speed);
	}
}

Slideshow.prototype.refresh = function(){
	this.changeSlide();
	this.setChrono(this.delay);
}

Slideshow.prototype.run = function(){
	if(this.chrono==0){
		this.turnRight();
	} else{
		this.chrono--;
	}
	if(this.isAutoMode()){
		setTimeout("slideshow.run()",1000);
	}
}

/******************* TRANSITIONS ************************/
Slideshow.prototype.noEffect = function(pOld,pNew){
	if (pOld){
		pOld.style.display='none';
	}

	if (pNew){
		pNew.style.display='block';
	}
};

Slideshow.prototype.crossFade = function(pOld,pNew,pSpeed){
	if (pNew){
		new Effect.Appear(pNew.id, {
			duration : pSpeed
		});
	}
	if (pOld){
		new Effect.Fade(pOld.id, {
			duration : pSpeed
		});
	}
};

Slideshow.prototype.blindDown = function(pOld,pNew,pSpeed){
	if(pNew){
		pNew.style.zIndex='2';
	}
	if(pOld){
		pOld.style.zIndex='1';
	}
	if (pNew){
		new Effect.BlindDown(pNew.id, {
			duration : pSpeed
		});
	}
	if (pOld){
		new Effect.Fade(pOld.id,{
			duration : pSpeed
		});
	}
};

Slideshow.prototype.blindUp = function(pOld,pNew,pSpeed){
	if(pNew){
		pNew.style.zIndex='1';
	}
	if(pOld){
		pOld.style.zIndex='2';
	}
	if (pNew){
		new Effect.Appear(pNew.id,{
			duration : pSpeed
		});
	}
	if (pOld){
		new Effect.BlindUp(pOld.id, {
			duration : pSpeed
		});
	}
};
