var thisimg
var thisblc
var imgH
var imgW
var thisMaxW
var thisMaxH
var nbDiapo
var speedMoove = 2
var thisPos
var valmoove
var retour=0
var time=30
var thistime
//Variable diapo intro
var nbDiapo
var thisDiapo = 0
var arDiapo
var thisVign1
var thisVign2
var thisVign3
var thisVign4
var ieop = 0
var op_moov=null
var aller=0
var timeOk=0
//Temps d'affichage minimum entre 2 images de diapo
var limiTime=5000
document.image_chargee = new Array();

// La fonction de préchargement
/*function prechargement(array) {
//alert(nbDiapo)
// Pour chaque image
for ( i = 1; i < array.length; i++ ) {
	for ( j = 0; j < array[i].length; j++ ) {
		// on créé virtuellement une image
		document.image_chargee[i] = new Image;
		
		// On en indique la source en assemblant le chemin, le nom et l'extension
		document.image_chargee[i].src = array[i][j];
		//alert( array[i][j])
	}
}
}*/
function prechargement(array) {
//alert(nbDiapo)
// Pour chaque image
for ( i = 1; i < array.length; i++ ) {
	for ( j = 0; j < array[i].length; j++ ) {
		// on créé virtuellement une image
		//document.image_chargee[i] = new Image;
		
		// On en indique la source en assemblant le chemin, le nom et l'extension
		//document.image_chargee[i].src = array[i][j];
		document.write('<img src="'+array[i][j]+'" style="visibility:hidden;height:0;width:0;">');
		//alert( array[i][j])
	}
}
}
function initMoove(idimg,idblc,maxW,maxH){
	thisimg = document.getElementById(idimg)
	thisblc = document.getElementById(idblc)
	imgH = thisimg.height
	imgW = thisimg.width
	thisMaxW = maxW
	thisMaxH = maxH
	if(imgW > maxW)
		hMoove()
	if(imgH > maxH)
		vMoove()
}
function hMoove(){
	changDiapo = 0
	thistime=time
	if(thisblc.style.backgroundPosition!='')
		thisPos = thisblc.style.backgroundPosition
	else if(thisPos=='')
		thisPos = '0px 0px'
	bgPos = getBgPos(thisPos)
	//alert(thisPos)
	//alert(thisPos+' -- '+bgPos)
	if(retour!=1) {
		//thisblc.style.backgroundPosition = (bgPos[0]-speedMoove)+'px 0px'
		valmoove = (bgPos[0]-speedMoove)
	}
	if(retour==0 || bgPos[0]>=0) {
		//alert('ff')
		//thisblc.style.backgroundPosition = (bgPos[0]-speedMoove)+'px 0px'
		valmoove = (bgPos[0]-speedMoove)
		//alert('1')
		retour=0
	}
	if(retour==1 || thisMaxW-bgPos[0]>=imgW) {
		//thisblc.style.backgroundPosition = (bgPos[0]-(-speedMoove))+'px 0px'
		valmoove = (bgPos[0]-(-speedMoove))
		if(thisMaxW-bgPos[0]>=imgW) { 
			thistime=1000
			if(thisDiapo!=0 && op_moov!=null) {
				//alert(imgW)
				changDiapo = 1
			}
		}
		else
			thistime=time
		retour=1
	}
	thisblc.style.backgroundPosition = valmoove+'px 0px'
	if(thisblc.style.backgroundPosition=='')
		thisPos = valmoove+'px 0px'
		if(changDiapo==1) {
			clearTimeout(op_moov)
			//thisblc.style.backgroundPosition = '0px 0px'
			aller=0
			op_moov=null
			effectInTime()
		}
		else
			op_moov=setTimeout('hMoove()', thistime);
		//setInterval("hMoove()", 50);
}
function vMoove(){
	changDiapo = 0
	thistime=time
	if(thisblc.style.backgroundPosition!='')
		thisPos = thisblc.style.backgroundPosition
	else if(thisPos=='')
		thisPos = '0px 0px'
	bgPos = getBgPos(thisPos)
	//alert(thisPos+' -- '+bgPos)
	if(retour!=1) {
		//thisblc.style.backgroundPosition = (bgPos[0]-speedMoove)+'px 0px'
		valmoove = (bgPos[1]-speedMoove)
	}
	if(retour==0 || bgPos[1]>=0) {
		//thisblc.style.backgroundPosition = (bgPos[0]-speedMoove)+'px 0px'
		valmoove = (bgPos[1]-speedMoove)
		if(bgPos[1]>=0) {
			thistime=1000
		}
		else
			thistime=time
		//alert('1')
		retour=0
	}
	if(retour==1 || thisMaxH-bgPos[1]>=imgH) {
		//thisblc.style.backgroundPosition = (bgPos[0]-(-speedMoove))+'px 0px'
		valmoove = (bgPos[1]-(-speedMoove))
		if(thisMaxH-bgPos[1]>=imgH)  {
			thistime=1000
			if(thisDiapo!=0 && op_moov!=null)
				changDiapo = 1
		}
		else
			thistime=time
		retour=1
	}
	//alert(bgPos[1])
	thisblc.style.backgroundPosition = '0px '+valmoove+'px'
	if(thisblc.style.backgroundPosition=='')
		thisPos = '0px '+valmoove+'px'
	//alert('sdsf')
	if(changDiapo==1) {
		clearTimeout(op_moov)
		//thisblc.style.backgroundPosition = '0px 0px'
		aller=0
		op_moov=null
		effectInTime()
	}
	else
		op_moov=setTimeout('vMoove()', thistime);
		//setInterval("hMoove()", 50);
}
function effectInTime(){
	
	if(timeOk==1) {
		effectDiapo()
	}
	else
		setTimeout('effectInTime()', 100)	
}
function getBgPos(pos){
	lPos = ''
	var reg_expt = /[+-]?[0-9]+px$/i
	tPos = reg_expt.exec(pos)
	
	var reg_expl = /[+-]?[0-9]+/i
	lPos = reg_expl.exec(pos)
	
	var reg_exptt = /[+-]?[0-9]+/i
	tPos2 = reg_exptt.exec(tPos)
	
	arrayBgPos = new Array(lPos,tPos2) 
	
	return arrayBgPos
} 

function initDiapo(idiapo,arrayfile,idimg,idblc,maxW,maxH){
	arDiapo = arrayfile
	//Préchargement des images
	nbDiapo = arrayfile.length
	//prechargement()
	thisDiapo = idiapo
	thisMaxW = maxW
	thisMaxH = maxH
	thisimg = document.getElementById(idimg)
	thisblc = document.getElementById(idblc)
	if(document.getElementById(arrayfile[0][2]))
		thisVign1 = document.getElementById(arrayfile[0][2])
	if(document.getElementById(arrayfile[0][3]))
		thisVign2 = document.getElementById(arrayfile[0][3])
	if(document.getElementById(arrayfile[0][4]))
		thisVign3 = document.getElementById(arrayfile[0][4])
	if(document.getElementById(arrayfile[0][5]))
		thisVign4 = document.getElementById(arrayfile[0][5])
	timeOk=0
	effectDiapo()
}
function diapoUp() {
	if(thisDiapo>nbDiapo-1)
		thisDiapo = 1
	//alert(thisDiapo+' -- '+nbDiapo)
	thisblc.style.backgroundImage = 'url('+arDiapo[thisDiapo][0]+')'
	//alert(arDiapo[thisDiapo][0])
	thisimg.src = arDiapo[thisDiapo][0];
	if(thisVign1 != null)
		thisVign1.style.backgroundImage = 'url('+arDiapo[thisDiapo][1]+')'
	if(thisVign2 != null)
		thisVign2.style.backgroundImage = 'url('+arDiapo[thisDiapo][2]+')'
	if(thisVign3 != null)
		thisVign3.style.backgroundImage = 'url('+arDiapo[thisDiapo][3]+')'
	if(thisVign4 != null)
		thisVign4.style.backgroundImage = 'url('+arDiapo[thisDiapo][4]+')'
	imgH = thisimg.height
	imgW = thisimg.width
	//alert(imgH+' -- '+imgW)		
	thisDiapo++
	opUp()
}
function effectDiapo() {
	if(thisDiapo>1) {
		//alert('1');
		opDown()
	}
	else {
		//alert('2');
		diapoUp()
	}
}
function timeLimit() {
	timeOk = 1
	//alert('ok')
}
function startDiapo(){		
	imgH = thisimg.height
	imgW = thisimg.width
	timeOk=0
	setTimeout('timeLimit()', limiTime)
	//alert(imgH+' -- '+imgW)
	if(imgW > thisMaxW) {
		if(imgW-50 < thisMaxW)
			setTimeout('effectDiapo()', limiTime)
		else
			hMoove()
	}
	else if(imgH > thisMaxH) {
		if(imgH-50 < thisMaxH)
			setTimeout('effectDiapo()', limiTime)
		else
		vMoove()
	}
}

function opDown(){
if(ieop>0){
	ieop-=20;
	if(thisblc.style.filter){
		thisblc.style.filter="alpha(opacity="+ieop+")"
		if(thisVign1 != null) {
			thisVign1.style.filter="alpha(opacity="+ieop+")"
			thisVign2.style.filter="alpha(opacity="+ieop+")"
			thisVign3.style.filter="alpha(opacity="+ieop+")"
			thisVign4.style.filter="alpha(opacity="+ieop+")"
		}
	}
	if(thisblc.style.MozOpacity) {
		thisblc.style.MozOpacity=ieop/100
		if(thisVign1 != null) {
			thisVign1.style.MozOpacity=ieop/100
			thisVign2.style.MozOpacity=ieop/100
			thisVign3.style.MozOpacity=ieop/100
			thisVign4.style.MozOpacity=ieop/100
		}
	}
	if(thisblc.style.opacity) {
		thisblc.style.opacity=ieop/100
		if(thisVign1 != null) {
			thisVign1.style.opacity=ieop/100
			thisVign2.style.opacity=ieop/100
			thisVign3.style.opacity=ieop/100
			thisVign4.style.opacity=ieop/100
		}
	}
	op_id=setTimeout('opDown()', 70);
}
else {
	clearTimeout(op_id);
	ieop = 0;
	thisblc.style.backgroundPosition = '0px 0px'
	diapoUp()
}
}
function opUp(){
if(ieop < 95){
	ieop+=19.5;
	if(thisblc.style.filter){
		thisblc.style.filter="alpha(opacity="+ieop+")"
		if(thisVign1 != null) {
			thisVign1.style.filter="alpha(opacity="+ieop+")"
			thisVign2.style.filter="alpha(opacity="+ieop+")"
			thisVign3.style.filter="alpha(opacity="+ieop+")"
			thisVign4.style.filter="alpha(opacity="+ieop+")"
		}
		
	}
	if(thisblc.style.MozOpacity) {
		thisblc.style.MozOpacity=ieop/100
		if(thisVign1 != null) {
			thisVign1.style.MozOpacity=ieop/100
			thisVign2.style.MozOpacity=ieop/100
			thisVign3.style.MozOpacity=ieop/100
			thisVign4.style.MozOpacity=ieop/100
		}
		
	}
	if(thisblc.style.opacity) {
		thisblc.style.opacity=ieop/100
		if(thisVign1 != null) {
			thisVign1.style.opacity=ieop/100
			thisVign2.style.opacity=ieop/100
			thisVign3.style.opacity=ieop/100
			thisVign4.style.opacity=ieop/100
		}
		
	}
	op_id=setTimeout('opUp()', 70);
}
else {
	clearTimeout(op_id);
	ieop = 100;
	//alert('1')
	startDiapo()
}
}