window.addEvent('domready', function() {
var div_init = new Element("div", {
"id" : "init"
});
div_init.inject('site','after');
div_init.set('html',0);
var div_compteur = new Element("div", {
"id" : "compteur"
});
div_compteur.inject('site','after');
var div_nbrephotos = new Element("div", {
"id" : "nbrephotos"
});
div_nbrephotos.inject('site','after');
var div_defilementdiapo = new Element("div", {
"id" : "defilementdiapo"
});
div_defilementdiapo.inject('site','after');
var div_dureedufondu = new Element("div", {
"id" : "dureedufondu"
});
div_dureedufondu.inject('site','after');
var div_existefondu = new Element("div", {
"id" : "existefondu"
});
div_existefondu.inject('site','after');
var div_zindexphoto = new Element("div", {
"id" : "zindexphoto"
});
div_zindexphoto.inject('site','after');
div_zindexphoto.set('html',10);

//**diapos blocs flottants
for (b=0;b<10;b++) {
var div_compteur = new Element("div", {
"id" : "compteur"+b
});
div_compteur.inject('site','after');
var div_nbrephotos = new Element("div", {
"id" : "nbrephotos"+b
});
div_nbrephotos.inject('site','after');
var div_dureedufondu = new Element("div", {
"id" : "dureedufondu"+b
});
div_dureedufondu.inject('site','after');
var div_existefondu = new Element("div", {
"id" : "existefondu"+b
});
div_existefondu.inject('site','after');
var div_zindexphoto = new Element("div", {
"id" : "zindexphoto"+b
});
div_zindexphoto.inject('site','after');
div_zindexphoto.set('html',11);
}
//*******************lancements
for (b=0;b<10;b++) verifsidiapoblocs(b);
var pageactive = window.location.search.substring(3,13);
//verifsidiapo(pageactive);
});
function verifsidiapo(pageactive,lancement,diapotag) {
if ($("init").get('text') != 0) { //clear du timeout du diaporama si lancé
clearInterval(affiche);
$("init").set('text',0);
}
//alert ("pageactive = "+pageactive+" lancement = "+lancement);
var id = pageactive.substring(4,10);
var myRequest = new Request({method: 'post', url: 'verifsidiapo.php'});
myRequest.send("operation=verifier&id="+id+"&diapotag="+diapotag);
myRequest.addEvent("onSuccess", function(reponse) {
  var ObjJson = JSON.decode(reponse);
  var existediapo = ObjJson.existediapo;
  if (existediapo == "existe") {
	  var imagealeat = ObjJson.imagealeat;
	  var nbphotos = ObjJson.nbphotos;
	  var defilementdiapo = ObjJson.defilementdiapo;
	  var existefondu = ObjJson.existefondu;
	  if ((existefondu == "fon") || (existefondu == "bal")) var dureedufondu = Math.round(parseInt(defilementdiapo)/2);
	  if (existefondu == "sim") var dureedufondu = 0;
	  //alert ("id = "+id+" nbphotos = "+nbphotos+" imagealeat = "+imagealeat);

		$('compteur').set('text',imagealeat);
		$('nbrephotos').set('text',nbphotos);
		$('defilementdiapo').set('text',defilementdiapo);
		$('dureedufondu').set('text',dureedufondu);
		$('existefondu').set('text',existefondu);

	  if ((nbphotos == 1) || (lancement == 'tagsrecherche')) {
			var hauteurphoto = $("img"+id+imagealeat).getStyle('height').toInt();
			$("enveloppephotos"+id).setStyles({'height':hauteurphoto});
			$("img"+id+imagealeat).setStyles({'left':0,'visibility':'visible'});
			$("legende"+id+imagealeat).setStyles({'display':'block'});
	  }
	  if ((nbphotos > 1) && (lancement == 'diapo')) {
			lancediapo();
	  }
 	} //existence du diaporama

	if (existediapo == "noclediapo") alert ("syntaxe de clediapo incorrecte dans le contenu du texte de la page "+id);
	if (existediapo == "nophoto") alert ("diaporama installé page "+id+" ,mais photos absentes");
});
}
function compte(ope) {
var n = parseInt($("nbrephotos").get('text'));
var c = parseInt($("compteur").get('text'));
//alert ("c  = "+c+" n = "+n);
  if (ope=='plus') {
          if (c < n) $("compteur").set('text',c+1);
          if (c >= n) $("compteur").set('text',1);
  }
  if (ope=='moins') {
          if (c>1) $("compteur").set('text',c-1);
          if (c<=1) $("compteur").set('text',n);
  }
}
function lancediapo() {
	var c = $("compteur").get('text');
  var defilementdiapo = $('defilementdiapo').get('text');
  var dureedufondu = $('dureedufondu').get('text');
  //alert ("defilementdiapo = "+defilementdiapo+" dureedufondu = "+dureedufondu);
  var id = window.location.search.substring(7,13);
  //*********arret si le diaporama automatique est déjà lancé
  if ($("init").get('text') != 0) stopdiapo();

	var hauteurphoto = $("img"+id+c).getStyle('height').toInt();
	$("enveloppephotos"+id).setStyles({'height':hauteurphoto});
	$("img"+id+c).setStyles({'left':0,'visibility':'visible'});
	$("legende"+id+c).setStyles({'display':'block'});

  affiche=setInterval("diapoauto()",defilementdiapo);
  $("init").set('text',2);
  $('divstart').set('html',"<a class='pictodiapo' onclick='stopdiapo()'><img class='pictodiapo' src='images/stop.png' alt='stop' title='stop'></a>");
}
function stopdiapo() {
  clearInterval(affiche);
  $("init").set('text',0);
  $('divstart').set('html',"<a class='pictodiapo' onclick='lancediapo()'><img class='pictodiapo' src='images/start.png' alt='d&eacute;filement automatique' title='d&eacute;filement automatique'></a>");
}
function diapozoom(c) {
var dureedufondu = $('dureedufondu').get('text');
var existefondu = $('existefondu').get('text');
var id = window.location.search.substring(7,13);
if ($("init").get('text') != 0) stopdiapo();
var c0 = $("compteur").get('text');
if (c != c0) { //sinon efface affiche la même image
	//efface précédente
	var largeurphoto0 = $("img"+id+c0).getStyle('width').toInt(); 
	if (existefondu != 'sim') {
		//setTimeout('cachephoto('+id+','+c0+','+dureedufondu+')',dureedufondu);
		cachephoto(id,c0,dureedufondu);
		cachelegende(id,c0);
	}
	if (existefondu == 'sim') {
		var hauteurphoto = $("img"+id+c0).getStyle('height').toInt();
		$("enveloppephotos"+id).setStyles({'height':hauteurphoto});
		$("img"+id+c0).setStyles({'left': -largeurphoto0,'visibility':'hidden'});
		$("legende"+id+c0).setStyles({'display':'none'});
	}
	//affiche
	$('compteur').set('html',c);
	if (existefondu == 'sim') {
		$("img"+id+c).setStyles({'left': 0,'visibility':'visible'});
		$("legende"+id+c).setStyles({'display':'block'});
	}
	if (existefondu != 'sim') {
		affichephoto(id,c,dureedufondu);
	}
}
}

function diapoauto() {
var id = window.location.search.substring(7,13);
var defilementdiapo = $('defilementdiapo').get('text');
var dureedufondu = $('dureedufondu').get('text');
var existefondu = $('existefondu').get('text');
//alert ("defilementdiapo = "+defilementdiapo+" dureedufondu = "+dureedufondu);

//*******************efface précédente
var c0 = $("compteur").get('text');
var largeurphoto0 = $("img"+id+c0).getStyle('width').toInt();
if (existefondu != 'sim') {
	//setTimeout('cachephotoplus('+id+','+c0+','+dureedufondu+')',dureedufondu);
	cachephotoplus(id,c0,dureedufondu);
	cachelegende(id,c0);
}
if (existefondu == 'sim') {
	var hauteurphoto = $("img"+id+c0).getStyle('height').toInt();
	$("enveloppephotos"+id).setStyles({'height':hauteurphoto});
	$("img"+id+c0).setStyles({'left': -largeurphoto0,'visibility':'hidden'});
	$("legende"+id+c0).setStyles({'display':'none'});
}

//*******************affiche
compte('plus');
var c = $("compteur").get('text');
//alert ("encours = "+c0+" a venir = "+c);
if (existefondu == 'sim') {
	$("img"+id+c).setStyles({'left': 0,'visibility':'visible'});
	$("legende"+id+c).setStyles({'display':'block'});
}
if (existefondu != 'sim') affichephotoplus(id,c,dureedufondu);
} //diapoauto

function diapoplus() {
var dureedufondu = $('dureedufondu').get('text');
var existefondu = $('existefondu').get('text');
var id = window.location.search.substring(7,13);
if ($("init").get('text') != 0 ) stopdiapo();

//**************efface précédente
var c0 = $("compteur").get('text');
var largeurphoto0 = $("img"+id+c0).getStyle('width').toInt();
if (existefondu != 'sim') {
	//setTimeout('cachephotoplus('+id+','+c0+','+dureedufondu+')',dureedufondu);
	cachephotoplus(id,c0,dureedufondu);
	cachelegende(id,c0);
}
if (existefondu == 'sim') {
	var hauteurphoto = $("img"+id+c0).getStyle('height').toInt();
	$("enveloppephotos"+id).setStyles({'height':hauteurphoto});
	$("img"+id+c0).setStyles({'left': -largeurphoto0,'visibility':'hidden'});
	$("legende"+id+c0).setStyles({'display':'none'});
}

//***************affiche
compte('plus');
var c = $("compteur").get('text');
if (c != c0) { //sinon efface affiche la même image
if (existefondu == 'sim') {
	$("img"+id+c).setStyles({'left': 0,'visibility':'visible'});
	$("legende"+id+c).setStyles({'display':'block'});
}
if (existefondu != 'sim') {
	affichephotoplus(id,c,dureedufondu);
}
}
	//alert ("var c0 = "+c0+" var c = "+c+" dureedufondu = "+dureedufondu);
}//diapoplus

function diapomoins() {
var dureedufondu = $('dureedufondu').get('text');
var existefondu = $('existefondu').get('text');
var id = window.location.search.substring(7,13);
if ($("init").get('text') != 0 ) stopdiapo();

//**************efface précédente
var c0 = $("compteur").get('text');
var largeurphoto0 = $("img"+id+c0).getStyle('width').toInt(); 
if (existefondu != 'sim') {
	//setTimeout('cachephoto('+id+','+c0+','+dureedufondu+')',dureedufondu);
	cachephoto(id,c0,dureedufondu);
	cachelegende(id,c0);
}
if (existefondu == 'sim') {
	var hauteurphoto = $("img"+id+c0).getStyle('height').toInt();
	$("enveloppephotos"+id).setStyles({'height':hauteurphoto});
	$("img"+id+c0).setStyles({'left': -largeurphoto0,'visibility':'hidden'});
	$("legende"+id+c0).setStyles({'display':'none'});
}

//***************affiche
compte('moins');
var c = $("compteur").get('text');
if (c != c0) { //sinon efface affiche la même image
if (existefondu == 'sim') {
	$("img"+id+c).setStyles({'left': 0,'visibility':'visible'});
	$("legende"+id+c).setStyles({'display':'block'});
}
if (existefondu != 'sim') {
	affichephoto(id,c,dureedufondu);
}
}
}//diapomoins

function cachephoto(id,c0,dureedufondu) {
var hauteurphoto = $("img"+id+c0).getStyle('height').toInt();
//$("enveloppephotos"+id).setStyles({'height':hauteurphoto});
var largeurphoto0 = $("img"+id+c0).getStyle('width').toInt();
var zindexphoto0 = $('zindexphoto').get('text').toInt()-1;
//alert ("var c0 = "+c0+" zindexphoto0 = "+zindexphoto0);
$("photo"+id+c0).setStyles({'z-index':zindexphoto0});
$("img"+id+c0).fade('out');
//var myphotoFx0 = new Fx.Morph("img"+id+c0, {duration:dureedufondu});
//myphotoFx0.start({'opacity':[1,0]});
}
function cachephotoplus(id,c0,dureedufondu) {
var hauteurphoto = $("img"+id+c0).getStyle('height').toInt();
//$("enveloppephotos"+id).setStyles({'height':hauteurphoto});
var largeurphoto0 = $("img"+id+c0).getStyle('width').toInt();
var zindexphoto0 = $('zindexphoto').get('text').toInt()-1;
$("photo"+id+c0).setStyles({'z-index':zindexphoto0});
$("img"+id+c0).fade('out');
//var myphotoFx0 = new Fx.Morph("img"+id+c0, {duration:dureedufondu});
//myphotoFx0.start({'opacity':[1,0]});
}
function cachephotobloc(id,c0,b,dureedufondu) {
var largeurphoto0 = $("img"+id+c0).getStyle('width').toInt();
var zindexphoto0b = parseInt($('zindexphoto'+b).get('text'))-1;
$("photo"+id+c0).setStyles({'z-index':zindexphoto0b});
$("img"+id+c0).fade('out');
//var myphotoFx0 = new Fx.Morph("img"+id+c0, {duration:dureedufondu});
//myphotoFx0.start({'opacity':[1,0]});
}
function affichephoto(id,c,dureedufondu) {
var existefondu = $('existefondu').get('text');
var largeurphoto = $("img"+id+c).getStyle('width').toInt();
var hauteurphoto = $("img"+id+c).getStyle('height').toInt();
$("enveloppephotos"+id).setStyles({'height':hauteurphoto});
var zindexphoto = $('zindexphoto').get('text').toInt()+1;
$('zindexphoto').set('text',zindexphoto);
$("photo"+id+c).setStyles({'z-index':zindexphoto});
var myphotoFx = new Fx.Morph("img"+id+c, {duration:dureedufondu});
if (existefondu == 'fon') {
	$("img"+id+c).setStyles({'visibility':'hidden','left':0});
	myphotoFx.start({'opacity':[0,1]});
}
if (existefondu == 'bal') myphotoFx.start({'left':[largeurphoto,0], 'opacity':[0,1]});
//setTimeout('affichelegende('+id+','+c+')',dureedufondu);
affichelegende(id,c);
$('divstart').set('html',"<img class='pictodiapo' src='images/commandebloc.png' alt='patientez svp' title='patientez svp'>");
$('divavant').set('html',"<img class='pictodiapo' src='images/commandebloc.png' alt='patientez svp' title='patientez svp'>");
$('divarriere').set('html',"<img class='pictodiapo' src='images/commandebloc.png' alt='patientez svp' title='patientez svp'>");
setTimeout("debloczoom()",dureedufondu);
}
function affichephotoplus(id,c,dureedufondu) { //inverse le sens de balayage
var existefondu = $('existefondu').get('text');
var largeurphoto = $("img"+id+c).getStyle('width').toInt();
var hauteurphoto = $("img"+id+c).getStyle('height').toInt();
$("enveloppephotos"+id).setStyles({'height':hauteurphoto});
var zindexphoto = $('zindexphoto').get('text').toInt()+1;
$('zindexphoto').set('text',zindexphoto);
$("photo"+id+c).setStyles({'z-index':zindexphoto});
var myphotoFx = new Fx.Morph("img"+id+c, {duration:dureedufondu});
if (existefondu == 'fon') {
	$("img"+id+c).setStyles({'visibility':'hidden','left':0});
	myphotoFx.start({'opacity':[0,1]});
}
if (existefondu == 'bal') myphotoFx.start({'left':[-largeurphoto,0], 'opacity':[0,1]});
//setTimeout('affichelegende('+id+','+c+')',dureedufondu);
affichelegende(id,c);
$('divstart').set('html',"<img class='pictodiapo' src='images/commandebloc.png' alt='patientez svp' title='patientez svp'>");
$('divavant').set('html',"<img class='pictodiapo' src='images/commandebloc.png' alt='patientez svp' title='patientez svp'>");
$('divarriere').set('html',"<img class='pictodiapo' src='images/commandebloc.png' alt='patientez svp' title='patientez svp'>");
setTimeout("debloczoom()",dureedufondu);
}
function affichephotobloc(id,c,b,dureedufondu) {
var existefondu = $('existefondu'+b).get('text');
var largeurphoto = $("img"+id+c).getStyle('width').toInt();
var zindexphotob = parseInt($('zindexphoto'+b).get('text'))+1;
$('zindexphoto'+b).set('text',zindexphotob);
$("photo"+id+c).setStyles({'z-index':zindexphotob});
var myphotoFx = new Fx.Morph("img"+id+c, {duration:dureedufondu});
if (existefondu == 'fon') {
	$("img"+id+c).setStyles({'visibility':'hidden','left':0});
	myphotoFx.start({'opacity':[0,1]});
}
if (existefondu == 'bal') myphotoFx.start({'left':[largeurphoto,0], 'opacity':[0,1]});
}
function affichelegende(id,c) {
$("legende"+id+c).setStyles({'display':'block'});
}
function cachelegende(id,c0) {
$("legende"+id+c0).setStyles({'display':'none'});
}
function defildiapod() {
var id = window.location.search.substring(7,13);
  x=$("film"+id).getStyle('width').toInt();
  y=$("fenetre"+id).getStyle('width').toInt();
  h=$("film"+id).getStyle('left').toInt();
  //alert ("position gauche du film = "+h);
  tdiapo=setTimeout("defildiapod()",5);
  if ((parseInt(h))>=0) clearTimeout(tdiapo);
  else $("film"+id).setStyle('left', (parseInt(h)+ 1));
}
function defildiapog() {
var id = window.location.search.substring(7,13);
  x=$("film"+id).getStyle('width').toInt();
  y=$("fenetre"+id).getStyle('width').toInt();
  h=$("film"+id).getStyle('left').toInt();
  tdiapo=setTimeout("defildiapog()",5);
  if ( (parseInt(h)+parseInt(x))<= (parseInt(y)) ) clearTimeout(tdiapo);
  else $("film"+id).setStyle('left', (parseInt(h)- 1));
}
function defildiapodL() {
var id = window.location.search.substring(7,13);
  clearTimeout(tdiapo);
  x=$("film"+id).getStyle('width').toInt();
  y=$("fenetre"+id).getStyle('width').toInt();
  h=$("film"+id).getStyle('left').toInt();
  tdiapo=setTimeout("defildiapodL()",5);
  if ((parseInt(h))>=0) clearTimeout(tdiapo);
  else $("film"+id).setStyle('left', (parseInt(h)+ 1));
}
function defildiapodV() {
var id = window.location.search.substring(7,13);
  clearTimeout(tdiapo);
  x=$("film"+id).getStyle('width').toInt();
  y=$("fenetre"+id).getStyle('width').toInt();
  h=$("film"+id).getStyle('left').toInt();
  tdiapo=setTimeout("defildiapodV()",1);
  if ((parseInt(h))>=0) clearTimeout(tdiapo);
  else $("film"+id).setStyle('left', (parseInt(h)+ 5));
}
function defildiapogL() {
var id = window.location.search.substring(7,13);
  clearTimeout(tdiapo);
  x=$("film"+id).getStyle('width').toInt();
  y=$("fenetre"+id).getStyle('width').toInt();
  h=$("film"+id).getStyle('left').toInt();
  tdiapo=setTimeout("defildiapogL()",5);
  if ( (parseInt(h)+parseInt(x))<= (parseInt(y)) ) clearTimeout(tdiapo);
  else $("film"+id).setStyle('left', (parseInt(h)- 1));
}
function defildiapogV() {
var id = window.location.search.substring(7,13);
  clearTimeout(tdiapo);
  x=$("film"+id).getStyle('width').toInt();
  y=$("fenetre"+id).getStyle('width').toInt();
  h=$("film"+id).getStyle('left').toInt();
  tdiapo=setTimeout("defildiapogV()",1);
  if ( (parseInt(h)+parseInt(x))<= (parseInt(y)) ) clearTimeout(tdiapo);
  else $("film"+id).setStyle('left', (parseInt(h)- 5));
}

//***********************************blocs
function verifsidiapoblocs(b) {
if (parseInt(b)<10) var id = "99000"+b;
if (parseInt(b)>=10) var id = "9900"+b;

  var myRequest = new Request({method: 'post', url: 'verifsidiapo.php'});
  myRequest.send("operation=verifier&id="+id+"&diapotag=vide");
  myRequest.addEvent("onSuccess", function(reponse) {
    var ObjJsonb = JSON.decode(reponse);
    var existediapo = ObjJsonb.existediapo;
    if (existediapo == "existe") {
    var imagealeatb = ObjJsonb.imagealeat;
    var nbphotosb = ObjJsonb.nbphotos;
    var defilementdiapo = ObjJsonb.defilementdiapo;
    var existefondu = ObjJsonb.existefondu;
	  if ((existefondu == "fon") || (existefondu == "bal")) var dureedufondu = Math.round(parseInt(defilementdiapo)/2);
	  if (existefondu == "sim") var dureedufondu = 0;

		$("img"+id+imagealeatb).setStyles({'left':0,'visibility':'visible'});
  
    //démarrage si diapo >1
    if (nbphotosb > 1) {
    $('compteur'+b).set('html',imagealeatb);
    $('nbrephotos'+b).set('html',nbphotosb);
    $('dureedufondu'+b).set('html',dureedufondu);
		$('existefondu'+b).set('html',existefondu);
    setInterval("diapoautob("+b+")",defilementdiapo);
    }

  } //existence du diaporama

	if (existediapo == "noclediapo") alert ("syntaxe de clediapo incorrecte dans le contenu du texte de la page "+id);
	if (existediapo == "nophoto") alert ("diaporama installé page "+id+" ,mais photos absentes");

  });
}
function compteb(b) {
//alert (b);
var n = parseInt($("nbrephotos"+b).get('text'));
var c = parseInt($("compteur"+b).get('text'));
//alert ("c  = "+c+" n = "+n);
if (c < n) $("compteur"+b).set('text',c+1);
if (c >= n) $("compteur"+b).set('text',1);
}
function diapoautob(b) {
	if (parseInt(b)<10) var id = "99000"+b;
	if (parseInt(b)>=10) var id = "9900"+b;
	var dureedufondu = $("dureedufondu"+b).get('text');
	var existefondu = $("existefondu"+b).get('text');
	//alert ("duree du fondu = "+dureedufondu);
	//********************************efface
	var c0 = $("compteur"+b).get('text');
	var largeurphoto0 = $("img"+id+c0).getStyle('width').toInt(); 
		if (existefondu != 'sim') {
			setTimeout('cachephotobloc('+id+','+c0+','+b+','+dureedufondu+')',dureedufondu);
		}
		if (existefondu == 'sim') $("img"+id+c0).setStyles({'left': -largeurphoto0,'visibility':'hidden'});
	//********************************affiche
	compteb(b);
	var c = $("compteur"+b).get('text');
  if (existefondu != 'sim') {
		affichephotobloc(id,c,b,dureedufondu);
  }
  if (existefondu == 'sim') $("img"+id+c).setStyles({'left':0,'visibility':'visible'});
}
function debloczoom() {
var init = $("init").get('text')
//alert (init);
if ( init < 2 ) $('divstart').set('html',"<a class='pictodiapo' onclick='lancediapo()'><img class='pictodiapo' src='images/start.png' alt='défilement automatique' title='défilement automatique'></a>");
if (init == 2) $('divstart').set('html',"<a class='pictodiapo' onclick='stopdiapo()'><img class='pictodiapo' src='images/stop.png' alt='stop' title='stop'></a>");
$('divavant').set('html',"<a class='pictodiapo' onclick='diapoplus()'><img class='pictodiapo' src='images/avant.png' alt='suivant' title='suivant'></a>");
$('divarriere').set('html',"<a class='pictodiapo' onclick='diapomoins()'><img class='pictodiapo' src='images/arriere.png' alt='précédent' title='précédent'></a>");
}

