// JavaScript Document

function diapoDisplay(targetFrame)
{
	this.images=new Array();
	this.targetFrame=targetFrame;
	this.delay=4;
	this.execDiapo=null;
	this.frames=new Array($("diapo_1"),$("diapo_2"));
}
diapoDisplay.prototype=
{
	init:function()
	{
			this.loadDiapoImages();
	},
	loadDiapoImages:function()
	{
			var request= new Ajax.Request('/requests/accueil.php',
						{
							parameters: 	{
											  cmd					: 	'loaddiapo'
										  },
							onSuccess: function(request)
							{
								var rep=request.responseJSON;
								if(request.responseJSON!="")
								{
									this.images.length=0;
									rep.each(function(enr,x)
									{
										this.images.push(enr);
									}.bind(this)
									);
									this.start();
								}
							}.bind(this)
						}
					   )
	},
	start:function()
	{
		var tout=8;
		var diapoCounter=1;
		
		this.frames[0].update('<img src="'+this.images[0]+'">');
		this.execDiapo= new PeriodicalExecuter(
											  function(fn)
											  {
												  var c=diapoCounter % 2;
												  this.frames[diapoCounter % 2].setOpacity(0);
												  this.frames[diapoCounter % 2].update('<img src="'+this.images[diapoCounter]+'">');
												  xAniOpacity(this.frames[diapoCounter % 2], 1, 3000, null,null);//element,opacityTarget,time,sinus?,oe=endFunction or eval
												  
												 // this.frames[diapoCounter % 2].update('<img src="'+this.images[diapoCounter]+'">');
												  xAniOpacity(this.frames[(diapoCounter+1) % 2], 0, 3000, null, null);//element,opacityTarget,time,sinus?,oe=endFunction or eval
												  diapoCounter++;
												  if(diapoCounter>=this.images.length) diapoCounter=0;
											  }.bind(this),tout
										 )
	},
	stopDiapo:function()
	{
		if(this.execDiapo)
			this.execDiapo.stop();
		this.execDiapo=null;
	}
}

function xAniOpacity(e, o, t, a, oe)//element,opacityTarget,time,sinus?,oe=endFunction or eval
{
  if (!(e=$(e))) return;
  var o0 = e.getOpacity(); // start value
  var dx = o - o0; // displacement
  var fq = 1 / t; // frequency
  if (a) fq *= (Math.PI / 2);
  var t0 = new Date().getTime(); // start time
  if(e.tmr) {clearInterval(e.tmr);e.tmr=null;}
  e.tmr = setInterval(
    function() {
      var et = new Date().getTime() - t0; // elapsed time
      if (et < t) {
        var f = et * fq; // constant velocity
        if (a == 1) f = Math.sin(f); // sine acceleration
        else if (a == 2) f = 1 - Math.cos(f); // cosine acceleration
        f = Math.abs(f);
        e.setOpacity( f * dx + o0); // instantaneous value
      }
      else
	  {
        clearInterval(e.tmr);e.tmr=null;
        e.setOpacity( o); // target value
		//if(o==0) e.hide(); else if(o==1) e.show();
        if (typeof oe == 'function') oe(); // 'onEnd' handler
        else if (typeof oe == 'string') eval(oe);
      }
    }, 10 // timer resolution
  );
}
