var Diaporamas=new Array();

function Diaporama(title, interval, width, height){
	this.Index=Diaporamas.length;
	this.Images=new Array();
	this.ImgIndex=0;
	this.ID=null;
	this.interval=interval==undefined ? 3000 : new Number(interval);
	this.Add=function(src, caption, width, height){
		var Index=this.Images.length;
		this.Images[Index]=new Array();
		this.Images[Index]["rel"]=src;
		this.Images[Index]["src"]=src;
		this.Images[Index]["caption"]=caption;
		this.Images[Index]["width"]=width;
		this.Images[Index]["height"]=height;
		this.Images[Index]["image"]=undefined;
		if (Index<=1) this.Load(Index);
	}
	this.Load=function(Index){
		if(Index < 0)Index=this.Images.length - 1;
		if(Index > this.Images.length - 1)Index=0;
		if (this.Images[Index]["image"]) return;
		this.Images[Index]["image"]=new Image();
		this.Images[Index]["image"].src=this.Images[Index]["src"];
	}

	this.Show=function(Index){
		if(Index < 0)Index=this.Images.length - 1;
		if(Index > this.Images.length - 1)Index=0;
		if (this.Images[Index]["image"]==undefined) this.Load(Index);
		document.images["disc" + this.Index].src=this.Images[Index]["image"].src;
		if (navigator.userAgent.indexOf('Safari')!=-1) {
			document.images["disc" + this.Index].width=this.Images[Index]["width"];
			document.images["disc" + this.Index].height=this.Images[Index]["height"];
		}
		document.getElementById("photo_caption" + this.Index).innerHTML = this.Images[Index]["caption"];
		this.ImgIndex=Index;
		this.Load(Index+1);
	}
	this.First=function(){
		this.Show(0);
	}
	this.Last=function(){
		this.Show(this.Images.length - 1);
	}
	this.Previous=function(){
		//if(this.ImgIndex > 0)this.Show(this.ImgIndex - 1);
		this.Show(this.ImgIndex - 1);
	}
	this.Next=function(){
		//if(this.ImgIndex < this.Images.length - 1)this.Show(this.ImgIndex + 1);
		this.Show(this.ImgIndex + 1);
	}
	this.Play=function(){
		this.ID=setInterval("Diaporamas[" + this.Index + "].Show(Diaporamas[" + this.Index + "].ImgIndex + 1);", this.interval);
		if (this.Images.length>1){
			button_play_stop = '<span class="button_cell" id="button_play_stop"><a href="javascript:Diaporamas[' + this.Index + '].Stop();">&nbsp;&nbsp;interrompre <img src="http://www.ericgarault.com/images/pause.gif" alt="" title="démarrer / interrompre" height="16" width="16" /></a></span>';
			document.getElementById("button_play_stop").innerHTML=button_play_stop;
		}
	}
	this.Stop=function(){
		clearInterval(this.ID);
		if (this.Images.length>1){
			button_play_stop = '<span class="button_cell" id="button_play_stop"><a href="javascript:Diaporamas[' + this.Index + '].Play();">&nbsp;&nbsp;démarrer <img src="http://www.ericgarault.com/images/pause.gif" alt="" title="démarrer / interrompre" height="16" width="16" /></a></span>';
			document.getElementById("button_play_stop").innerHTML=button_play_stop;
		}
		this.ID=null;
	}
	this.Click=function(Index){
		this.Stop();
		this.Show(Index);
	}
	this.Build=function(play, start_index){
		if (start_index==undefined) start_index=0;
		document.write("<div id=photo_full><img src=" + this.Images[start_index]["src"] + " name=disc" + this.Index + (width==undefined ? " " : " width=" + width) + (height==undefined ? " " : " height=" + height) + "></div>");
		document.write('<div id="photo_caption' + this.Index + '">' + this.Images[start_index].caption + '</div>');
		if (this.Images.length>1){
			if (this.ID) {
				button_play_stop = '<span class="button_cell" id="button_play_stop"><a href="javascript:Diaporamas[' + this.Index + '].Stop();">&nbsp;&nbsp;interrompre <img src="http://www.ericgarault.com/images/pause.gif" alt="" title="démarrer / interrompre" height="16" width="16" /></a></span>';
			} else {
				button_play_stop = '<span class="button_cell" id="button_play_stop"><a href="javascript:Diaporamas[' + this.Index + '].Play();">&nbsp;&nbsp;démarrer <img src="http://www.ericgarault.com/images/pause.gif" alt="" title="démarrer / interrompre" height="16" width="16" /></a></span>';
			}
			document.write('<div id="photo_control">' + button_play_stop + '&nbsp;&nbsp;<span class="button_cell"><a href="javascript:Diaporamas[' + this.Index + '].Previous();"><img src="http://www.ericgarault.com/images/previous.gif" alt="" title="précédent" height="16" width="16" /></a></span>&nbsp;&nbsp;<span class="button_cell"><a href="javascript:Diaporamas[' + this.Index + '].Next();"><img src="http://www.ericgarault.com/images/next.gif" alt="" title="suivant" height="16" width="16" /></a></span></div>');
		}
		if(start_index) this.Show(start_index);
		if(play) this.Play();
	}

	Diaporamas[this.Index]=this;
}

