Element.implement({

 fadeTo : function( amount, whenFinished, duration ) {
   if(typeof whenFinished != 'function') {
	 whenFinished = function() {}   
   }
   this.setStyle('visibility','visible');
   
   this.effect('opacity', {
      transition : new Fx.Transition(Fx.Transitions.Quad.easeOut, 6).easeOut 
    , duration : ( (duration!=null && duration>0) ? duration : 1000 )
    , onComplete : function() { 
	    if(amount==0) this.element.setStyle('visibility','hidden');
	    this.whenFinished.bind(this.element)();
	  }.bind({whenFinished:whenFinished,element:this})
   }).start(this.getOpacity(),amount);
   
   effect = new Fx.Tween(this)
   effect.addEvent('compelte',function() {
     if(amount==0) this.element.setStyle('visibility','hidden');
     this.whenFinished.bind(this.element)();
   }.bind({whenFinished:whenFinished,element:this}));
   effect.start('opacity', amount);
   return this;
 }
		
});

var currentPhoto = 0;
var photos = [];
var gid;

window.addEvent('load', function() {
  photos = $$('#diaporama img');
  var i = 0;
  photos.each(function(myEl){
	  if(i!=currentPhoto) { 
		myEl.setOpacity(0); 
	  } else {
		myEl.setOpacity(1);  
	  }
	  i++; 
  });
  gid = setInterval(function(){
	if(currentPhoto>=photos.length-1) {	
	  myImg = photos[0];
	  currentPhoto = 0;
	} else {
	  myImg = photos[currentPhoto];
	  currentPhoto++; 
	}
	
	i = 0;
	photos[currentPhoto];
	photos.each(function(myEl){
	  if(i!=currentPhoto) { 
		myEl.fadeTo(0); 
	  } else {
		myEl.fadeTo(1);  
	  }
	  i++; 
	});
  },2000);
});
