// JavaScript Document


function Picture(src, width, height) {
	this.image = new Image();
	this.image.src = src;
		
	if(width>0){
		this.image.width = width;
	}
	if(height>0){
		this.image.height = height;
	}
	
}


// BODY Example:
// var mySlideShow2 = new SlideShow(mySlideList2, 'slide2', 1000, "mySlideShow2");
function SlideShow(slideList, screenName, speed, name) {
	this.slideList = slideList;
	this.screenName = screenName;
	this.speed = speed;
	this.name = name;
	
	this.currentIndex = -1;
	this.timer = null;
	
}

SlideShow.prototype.switchImage = SlideShow_switchImage;
function SlideShow_switchImage(index) {
  if (document.images) {
	with(this){
    	if (currentIndex!=index && slideList[index]) {
		  var img = slideList[index];

        	document.images[screenName].src = img.image.src;
			if(img.image.width!==0)
			    document.images[screenName].width = img.image.width;		
			if(img.image.height!==0)
			   document.images[screenName].height = img.image.height;
			currentIndex = index;
    	}
	}
  }
}

/*
* Image suivante,
* revient au début une fois la dernière image atteinte
*/
SlideShow.prototype.next = SlideShow_next;
function SlideShow_next(){
	with(this){
		if(currentIndex+1==slideList.length){
			switchImage(0);
		} else {
			switchImage(currentIndex+1);
		}
	}
}

/*
* Image précédente,
* va à la fin si la première image est affichée
*/
SlideShow.prototype.previous = SlideShow_previous;
function SlideShow_previous(){
	with(this){
		if(currentIndex-1<0){
			switchImage(slideList.length-1);
		} else {
			switchImage(currentIndex-1);
		}
	}
}

SlideShow.prototype.first = SlideShow_first;
function SlideShow_first(){
	with(this){
		switchImage(0);
	}
}

SlideShow.prototype.last = SlideShow_last;
function SlideShow_last(){
	with(this){
		switchImage(slideList.length-1);
	}
}

SlideShow.prototype.play = SlideShow_play;  
function SlideShow_play() {
  with(this) {
    	timer = setTimeout(name+'.next();'+name+'.play()', speed);
  }
}

SlideShow.prototype.stop = SlideShow_stop;  
function SlideShow_stop(){
	with(this){
	    clearTimeout(timer);
		timer = null;
	}
}

SlideShow.prototype.isRunning = SlideShow_isRunning;
function SlideShow_isRunning(){
	return this.timer != null;
}

SlideShow.prototype.faster = SlideShow_faster;
function SlideShow_faster(){
	with(this){
		speed -= 500;
		speed = speed<0 ? 0 : speed;
	}
}

SlideShow.prototype.slowdown = SlideShow_slowdown;
function SlideShow_slowdown(){
	with(this){
		speed += 500;
	}
}
