var Gallery = Class.create();
Gallery.prototype =  {										 
  initialize: function(id, duration, delay) {
	if(!delay) delay = 0;
	if(!duration) duration = 3000;
	//
	this.duration = duration;
    this.imageHolder  = id;
	this.currentAnimationIndex = 0;
	//
	Element.setStyle(this.imageHolder, { position: 'relative'} );
	Element.addClassName(this.imageHolder, 'galleryRotator' );
	this.animations = new Array();
	//
	var imgs = $(this.imageHolder).getElementsByTagName('img');
	for(var i = 0; i<imgs.length; i++)
	{
		Element.setStyle(imgs[i], { position: 'absolute',  zIndex: imgs.length - i} );
		this.animations[this.animations.length] = new fx.Opacity(imgs[i], {duration: 700 });
	}
	//
	this.currentAnimationIndex = 0;
	setTimeout(this.fadeNext.bind(this), delay + duration);
  }
  ,
  fadeNext: function()
  {  
	  if(this.currentAnimationIndex < this.animations.length-1)
	  {
		this.animations[this.currentAnimationIndex].toggle();
		this.currentAnimationIndex++; 
	  }
	  else
	  {
		this.currentAnimationIndex = 0;
		this.animations[this.currentAnimationIndex].toggle();
		setTimeout(this.showAll.bind(this), 1000 );
	  }
	  
	  setTimeout(this.fadeNext.bind(this), this.duration);
  }
  ,
  showAll: function()
  {
	for(var i=1; i < this.animations.length - 1; i++)
	{
		this.animations[i].toggle();
	} 
  }
};

