window.addEvent('domready', function() {
//blocs
for (b=0;b<10;b++) verifsidiapoblocs(b);
});
function verifsidiapo(pageactive) {
	if ($("init0").getText() == "M") {clearInterval(affiche0); diapoautob(0); affiche0 = setInterval("diapoautob(0)",3000);}
	if ($("init1").getText() == "M") {clearInterval(affiche1); diapoautob(1); affiche1 = setInterval("diapoautob(1)",3000);}		
	if ($("init2").getText() == "M") {clearInterval(affiche2); diapoautob(2); affiche2 = setInterval("diapoautob(2)",3000);}
	if ($("init3").getText() == "M") {clearInterval(affiche3); diapoautob(3); affiche3 = setInterval("diapoautob(3)",3000);}
	if ($("init4").getText() == "M") {clearInterval(affiche4); diapoautob(4); affiche4 = setInterval("diapoautob(4)",3000);}
	if ($("init5").getText() == "M") {clearInterval(affiche5); diapoautob(5); affiche5 = setInterval("diapoautob(5)",3000);}		
	if ($("init6").getText() == "M") {clearInterval(affiche6); diapoautob(6); affiche6 = setInterval("diapoautob(6)",3000);}
	if ($("init7").getText() == "M") {clearInterval(affiche7); diapoautob(7); affiche7 = setInterval("diapoautob(7)",3000);}
	if ($("init8").getText() == "M") {clearInterval(affiche8); diapoautob(8); affiche8 = setInterval("diapoautob(8)",3000);}
	if ($("init9").getText() == "M") {clearInterval(affiche9); diapoautob(9); affiche9 = setInterval("diapoautob(9)",3000);}
	
	var id = parent.location.hash.substring(5,11);
	if ($("init").getText() == 1){
	stop();
	}
	var url = "verifsidiapo.php";
	var id = pageactive.substring(4,10);
	var options =  {method : "post", data : "operation=verifier&id="+id }; 
	var myAjax = new Ajax(url, options).request();
	myAjax.addEvent("onSuccess", function(reponse) {
		var ObjJson = Json.evaluate(reponse);
		var existediapo = ObjJson.existediapo;
		if (existediapo == "existe") {
		var imagealeat = ObjJson.imagealeat;
		var nbphotos = ObjJson.nbphotos;
		//alert ("nbphotos = "+nbphotos+" imagealeat = "+imagealeat);
		$('compteur').setText(imagealeat);
		$('nbrephotos').setText(nbphotos);
		if (nbphotos == 1)
		{
		$("photo"+id+imagealeat).setStyles({"display" : "block"});
		var myphotoFx = new Fx.Style("photo"+id+imagealeat, 'opacity',  {fps:100,duration:800});
		myphotoFx.start(0,1);
		}
		if (nbphotos > 1) start();
		}
	//blocs flottants
	});
}
function AfficheDiapoRequest(pageactive,ordre) {
	var id = parent.location.hash.substring(5,11);
	if ($("init").getText() == 1){
	stop();
	}
	clearInterval(histofirefox);
	$("mouline").effect("opacity", {duration:100}).start(0,1);
	var url = "page.php";
	var options = {update : $("page"), method : "get", data : "a="+pageactive }; 
	var myAjax = new Ajax(url, options).request();
	myAjax.addEvent("onSuccess", function(resultat){
	$('compteur').setHTML(ordre);
	$("photo"+id+ordre).setStyles({"display" : "block"});
	var myphotoFx = new Fx.Style("photo"+id+ordre, 'opacity',  {fps:100,duration:800});
	myphotoFx.start(0,1);
	$("mouline").effect("opacity").start(1,0);
	histofirefox = setInterval("handleHistory();", 500);
	});
}
function compte(ope) {
	n = parseInt($("nbrephotos").getText());
	c = parseInt($("compteur").getText());
	//alert ("c  = "+c+" n = "+n);
		  if (ope=='plus') {
		  	      if (c < n) $("compteur").setText(c+1);
							if (c >= n) $("compteur").setText(1);
			}
		  if (ope=='moins') {
		          if (c>1) $("compteur").setText(c-1);
							if (c<=1) $("compteur").setText(n);
			}
}
function start() {
	var id = parent.location.hash.substring(5,11);
	if ($("init").getText() == 1) stop();
	efface();
	c = $("compteur").getText();
	$("photo"+id+c).setStyles({"display" : "block"});
	var myphotoFx = new Fx.Style("photo"+id+c, 'opacity',  {fps:100,duration:800});
	myphotoFx.start(0,1);
	affiche=setInterval("diapoauto()",4000);
	$("init").setText(1);
	$('divstart').setHTML("<a class='pictodiapo' onclick='stop()'><img class='pictodiapo' src='images/stop.png' alt='stop' title='stop'></a>");
}
function stop() {
	clearInterval(affiche);
	$("init").setText(0);
	$('divstart').setHTML("<a class='pictodiapo' onclick='start()'><img class='pictodiapo' src='images/start.png' alt='d&eacute;filement automatique' title='d&eacute;filement automatique'></a>");
}
function diapozoom(c) {
	var id = parent.location.hash.substring(5,11);
  if ($("init").getText() == 1) stop();
  efface();
  $("photo"+id+c).setStyles({"display" : "block"});
  var myphotoFx = new Fx.Style("photo"+id+c, 'opacity',  {fps:100,duration:800});
  myphotoFx.start(0,1);
}
function efface() {
	var id = parent.location.hash.substring(5,11);	d=1;
	while ($("photo"+id+d)) {
		if ($("photo"+id+d)) {
			$("photo"+id+d).setOpacity(0);
			$("photo"+id+d).setStyles({"display" : "none"});
			}
		d = parseInt(d)+1;}
}
function diapoauto() {
	var id = parent.location.hash.substring(5,11);
  compte('plus');
  c = $("compteur").getText();
  efface();
  $("photo"+id+c).setStyles({"display" : "block"});
  var myphotoFx = new Fx.Style("photo"+id+c, 'opacity',  {fps:100,duration:800});
  myphotoFx.start(0,1);
}
function diapoplus() {
	var id = parent.location.hash.substring(5,11);
  if ($("init").getText()  == "1" ) {stop();}
  compte('plus');
  c = $("compteur").getText();
  efface();
  $("photo"+id+c).setStyles({"display" : "block"});
  var myphotoFx = new Fx.Style("photo"+id+c, 'opacity',  {fps:100,duration:800});
  myphotoFx.start(0,1);
}
function diapomoins() {
	var id = parent.location.hash.substring(5,11);
  if ($("init").getText()  == "1" ) {stop();}
  compte('moins');
  c = $("compteur").getText();
  efface();
  $("photo"+id+c).setStyles({"display" : "block"});
  var myphotoFx = new Fx.Style("photo"+id+c, 'opacity',  {fps:100,duration:800});
  myphotoFx.start(0,1);
}
function defildiapod() {
var id = parent.location.hash.substring(5,11);
  x=$("film"+id).getStyle('width').toInt();
  y=$("fenetre"+id).getStyle('width').toInt();
  h=$("film"+id).getStyle('left').toInt();
  tdiapo=setTimeout("defildiapod()",5);
  if ((parseInt(h))>=0) clearTimeout(tdiapo);
  else $("film"+id).setStyle('left', (parseInt(h)+ 1));
}
function defildiapog() {
var id = parent.location.hash.substring(5,11);
  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 = parent.location.hash.substring(5,11);
	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 = parent.location.hash.substring(5,11);
	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)+ 1));
}
function defildiapogL() {
var id = parent.location.hash.substring(5,11);
	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 = parent.location.hash.substring(5,11);
	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)- 1));
}
//***********************************blocs

function verifsidiapoblocs(b) {
	if (parseInt(b)<10) var id = "92000"+b;
	if (parseInt(b)>=10) var id = "9200"+b;

	var url = "verifsidiapo.php";
	var options =  {method : "post", data : "operation=verifier&id="+id }; 
	var myAjax = new Ajax(url, options).request();
	myAjax.addEvent("onSuccess", function(reponse) {
		var ObjJsonb = Json.evaluate(reponse);
		var existediapo = ObjJsonb.existediapo;
		if (existediapo == "existe") {
		var imagealeatb = ObjJsonb.imagealeat;
		var nbphotosb = ObjJsonb.nbphotos;
		//alert ("nbphotos = "+nbphotos+" imagealeat = "+imagealeat);
		if (nbphotosb == 1) {
			$("photo"+id+imagealeatb).setStyles({"display" : "block"});
			var myphotoFx = new Fx.Style("photo"+id+imagealeatb, 'opacity',  {fps:100,duration:800});
			myphotoFx.start(0,1);
			}
		if (nbphotosb > 1) {
			$('compteur'+b).setHTML(imagealeatb);
			$('nbrephotos'+b).setHTML(nbphotosb);
			//setTimeout("diapoautob("+b+"),200");
			diapoautob(b);
			if (b=="0") affiche0 = setInterval("diapoautob("+b+")",3000);
			if (b=="1") affiche1 = setInterval("diapoautob("+b+")",3000);
			if (b=="2") affiche2 = setInterval("diapoautob("+b+")",3000);
			if (b=="3") affiche3 = setInterval("diapoautob("+b+")",3000);
			if (b=="4") affiche4 = setInterval("diapoautob("+b+")",3000);
			if (b=="5") affiche4 = setInterval("diapoautob("+b+")",3000);
			if (b=="6") affiche6 = setInterval("diapoautob("+b+")",3000);
			if (b=="7") affiche7 = setInterval("diapoautob("+b+")",3000);
			if (b=="8") affiche8 = setInterval("diapoautob("+b+")",3000);
			if (b=="9") affiche9 = setInterval("diapoautob("+b+")",3000);
			}
		}
	});
}
function compteb(b) {
	//alert (b);
	n = parseInt($("nbrephotos"+b).getText());
	c = parseInt($("compteur"+b).getText());
	//alert ("c  = "+c+" n = "+n);
	if (c < n) $("compteur"+b).setText(c+1);
	if (c >= n) $("compteur"+b).setText(1);
}
function diapoautob(b) {
	if (parseInt(b)<10) var id = "92000"+b;
	if (parseInt(b)>=10) var id = "9200"+b;
  compteb(b);
  c = $("compteur"+b).getText();
  effaceb(b);
  //alert (id+c);
  $("init"+b).setText("M");
  $("photo"+id+c).setStyles({"display" : "block"});
  var myphotoFx = new Fx.Style("photo"+id+c, 'opacity',  {fps:100,duration:800});
  myphotoFx.start(0,1);
}
function effaceb(b) {
	if (parseInt(b)<10) var id = "92000"+b;
	if (parseInt(b)>=10) var id = "9200"+b;	d=1;
	while ($("photo"+id+d)) {
		if ($("photo"+id+d)) {
			$("photo"+id+d).setOpacity(0);
			$("photo"+id+d).setStyles({"display" : "none"});
			}
		d = parseInt(d)+1;}
}
