var rotateReference = new Class({
  initialize:function(start, nextRotate, id){
    this.start = start;
    this.nextRotate = nextRotate;
    this.id= id;
    this.imagesList = new Hash();
    this.counter = 1;
    this.init();
    this.current = 1;
  },

  init:function(){
    // create a new hash and put all the images in the hash
    $(this.id).getElements('img').each(function(el){
      this.imagesList.set('image'+this.counter, el);
      this.counter++;
    }.bind(this)
    )
    this.counter = 2; // get the next image
    (function(){ this.nextImage()}.bind(this)).delay(this.start * 1000);
  },

  nextImage:function(){
    if (!this.imagesList.has('image'+this.counter)) { // there is not a next image
      this.counter = 1;
    }
    if (this.counter != this.current) // there are more than 1 images in the dir, so chnage the image
    {
      $(this.id+this.current).setStyle('display', 'none');
      $(this.id+this.counter).setStyle('opacity', 0);
      $(this.id+this.counter).setStyle('display', '');
      $(this.id+this.counter).fade('in'); // is the next image 
      this.current = this.counter;
      this.counter++;
      (function(){ this.nextImage()}.bind(this)).delay(this.nextRotate * 1000);
    }
  }
});

window.addEvent('domready', function(){
  if ($('rotateReference')) {
    var reference = new rotateReference(2,4,'rotateReference');
    var project = new rotateReference(4,4,'rotateProject');
  }
});
