var xo, yo, popup, e, show=0, logo
function showPopup(img) {
		if (!popup) popup=document.getElementById("popup")
	    	show=0
			popup.src=img
			popup.style.visibility="visible"
}
function hidePopup() {
		/* popup.innerHTML=txt */
		if (!popup) popup=document.getElementById("popup")
		popup.style.visibility="hidden"
		show=0
}
function movePopup(e) {
		if (!popup) popup=document.getElementById("popup")
		if (show==1) popup.style.visibility="visible"
if (e || navigator.userAgent.indexOf('Mozilla')) {
		var xo=window.pageXOffset
		var yo=window.pageYOffset
	}	else {
		if (!e) e = event
		var xo=document.body.scrollLeft
		var yo=document.body.scrollTop
	}

	popup.style.left=e.clientX+xo-50+"px"
	popup.style.top=e.clientY+yo+10+"px"
}


var slideshow = {
url: '',
premiere_image: 0,
derniere_image: 0,
image_actuelle: { img: 0, num: 0, opacity: 100 }, // img = l'élément du DOM,  num = le numéro de l'image
image_cachee: { img: 0, num: 0, opacity: 0 },
image_prechargee: { img: 0, num: -1, opacity: 0 },
palier_opacite: 5,
periode_transition: 40, // vitesse de transition du crossfade (période en ms) 
periode_slideshow: 5000, // attente entre chaque image (période en ms) 
timer_avancement: 0,
timer_changement: 0,

tempo_echange: 0, // variable temporaire pour le swap

choisi_opacite: function(image, opacity) {
	image.style.opacity = opacity/100
	image.style.MozOpacity = opacity/100
	//image.img.style.KhtmlOpacity = opacity/100
	image.style.filter="alpha(opacity="+opacity+");"
	//image.img.style.KhtmlOpacity = opacity/100
	//image.img.style.setProperty("-khtml-opacity",opacity/100,null)
	//image.img.style.setProperty('-moz-opacity',opacity/100,null)
},

transition: function() { //cette fonction fait une transition depuis image_actuelle vers image_cachee
	// au début de la transition
	if (this.image_cachee.opacity==0) {
		// on réhausse la vignette de l'image voulue
		this.choisi_opacite(document.getElementById("thumbnail"+this.image_cachee.num), 100)
		// puis on efface le commentaire texte de l'ancienne image
		document.getElementById("slideshow-text"+this.image_actuelle.num).style.display="none"
		// puis on affiche le nouveau commentaire texte
		document.getElementById("slideshow-text"+this.image_cachee.num).style.display="block"
	}
	// tant que l'image cachée n'est pas completement visible
	if (this.image_cachee.opacity < 100) {
		// on diminue l'opacité de current
		this.image_actuelle.opacity -= this.palier_opacite
		this.choisi_opacite(this.image_actuelle.img, this.image_actuelle.opacity)
		//puis on boucle
// réactiver les 2 lignes suivantes pour transformer le crossfade en fade-out-fade-in
//		setTimeout("slideshow.transition()", this.periode_transition)
//	} else if (this.image_cachee.opacity < 100) {
		// on augmente l'opacité de hidden
		this.image_cachee.opacity += this.palier_opacite
		this.choisi_opacite(this.image_cachee.img, this.image_cachee.opacity)
		//puis on boucle
		setTimeout("slideshow.transition()", this.periode_transition)
	} else { // fin de la transition
		//sinon current devient hidden et vice versa
		this.tempo_echange = this.image_actuelle
		this.image_actuelle = this.image_cachee
		this.image_cachee = this.tempo_echange
		// à la fin de la transition
		if(this.image_actuelle.num != -1 && this.image_cachee.num != this.image_actuelle.num) {
			// on diminue l'opécité de la vignette de l'ancienne image
			this.choisi_opacite(document.getElementById("thumbnail"+this.image_cachee.num), 40)
		}
	}
},

va_image: function(i) {
	// si on est en pleine transition, on reessaye dans 1s
	if (this.image_actuelle.opacity != 100) {
		clearTimeout(this.timer_changement)
		this.timer_changement = setTimeout("slideshow.va_image("+i+")", 1)
		return
	}
	// si l'image voulue est déjà chargée (dans hidden) on l'affiche
	if (this.image_cachee.num==i) {
		this.transition()
	// si l'image est déjà chargée dans preloaded, on la place dans hidden puis on l'affiche
	} else if (this.image_prechargee.num==i) {
		this.tempo_echange = this.image_prechargee
		this.image_prechargee = this.image_cachee
		this.image_cachee = this.tempo_echange
		this.transition()
	} else { // sinon on la charge puis on l'affiche
		this.image_cachee.num = i
		this.image_cachee.opacity = 0
		this.choisi_opacite(this.image_cachee.img, this.image_cachee.opacity)
		this.image_cachee.img.src = this.url + "/?img=" + i
		this.transition()
	}
	// puis on précharge la suivante
	if (i >= this.derniere_image) i=this.premiere_image
	this.image_prechargee.num = i+1
	this.image_prechargee.img.src = this.url + "/?img=" + this.image_prechargee.num
},

run: function() {
	// on va chercher l'image suivante de celle affichée
	var i = this.image_actuelle.num+1
	clearTimeout(this.timer_avancement)
	// si on est à la fin, on retourne au début
	if (i > this.derniere_image) i=this.premiere_image
	// si la transparence de current n'est pas à 100, on attend 1s de plus
	if(this.image_actuelle.opacity != 100) {
		this.timer_avancement = setTimeout("slideshow.run()", 1000)
		return
	}
	// on affiche d'abord l'image numéro i puis on boucle sur le numéro suivant
	this.va_image(i)
	this.timer_avancement = setTimeout("slideshow.run()", this.periode_slideshow)
},

stop: function() {
	clearTimeout(this.timer_avancement)
}
} 

function initslideshow() {
    slideshow.url = url
    slideshow.derniere_image = imagenb-1
	slideshow.image_cachee.img = document.getElementById("slideshow-image1")
	slideshow.choisi_opacite(slideshow.image_cachee.img, slideshow.image_cachee.opacity)
	slideshow.image_cachee.img.src = url + "/?img=" + slideshow.image_cachee.num

	slideshow.image_actuelle.img = document.getElementById("slideshow-image2")
	slideshow.choisi_opacite(slideshow.image_actuelle.img, slideshow.image_actuelle.opacity)

	slideshow.image_prechargee.img = document.getElementById("slideshow-image3")
	slideshow.choisi_opacite(slideshow.image_prechargee.img, slideshow.image_prechargee.opacity)

	// on affiche la 1ere image, puis on lance le slideshow apres le timeout
	slideshow.va_image(slideshow.premiere_image)
	slideshow.timer_avancement = setTimeout("slideshow.run()", slideshow.periode_slideshow)

	document.getElementById("slideshow").oncontextmenu=new Function("return false")
}

//addLoadListener(initslideshow)
window.onload = initslideshow
