function sleep(delay){
    var start = new Date().getTime();
    while (new Date().getTime() < start + delay);
}

var SlideShow = Class.create({
	initialize: function(target, images, effect, frequency) {
    
	this.target = target;
	this.images = images;
    this.effect = effect;
    this.frequency = frequency;
    
    this.actualImageIndex = 0;
    this.currentlyExecuting = false;
    
    
    this.imageOne = new Image();
    this.imageOne.id = 'slideshow_one';
    this.imageOne.src = this.images[0];
    this.imageOne.style.display = 'none';
    this.target.appendChild(this.imageOne);
    
    this.imageTwo = new Image();
    this.imageTwo.id = 'slideshow_two';
    this.imageTwo.src = null;
    this.imageTwo.style.display = 'none';
    this.target.appendChild(this.imageTwo);
    
    Effect.Appear(this.imageOne);
    this.isImageOneShow = true;
    
    this.registerCallback();
  },

  getActualImageIndex: function() {
    return this.actualImageIndex;
  },
  
  registerCallback: function() {
    this.timer = setInterval(this.onTimerEvent.bind(this), this.frequency * 1000);
  },

  execute: function() {
    this.registerCallback();
  },

  stop: function() {
    if (!this.timer) return;
    clearInterval(this.timer);
    this.timer = null;
  },
  
  next: function() {
	
	if (!this.currentlyExecuting) {
      try {
        this.currentlyExecuting = true;
        if(this.isImageOneShow){
		    Effect.Fade(this.imageOne);
		    this.actualImageIndex = (this.actualImageIndex + 1)%(this.images.length);
		    this.imageTwo.src = this.images[this.actualImageIndex];
		    setTimeout('Effect.Appear(document.getElementById(\''+this.imageTwo.id+'\'));', 1000);
		    this.isImageOneShow = false;
	    }else{
		    Effect.Fade(this.imageTwo);
		    this.actualImageIndex = (this.actualImageIndex + 1)%(this.images.length);
		    this.imageOne.src = this.images[this.actualImageIndex];
		    setTimeout('Effect.Appear(document.getElementById(\''+this.imageOne.id+'\'));', 1000);
		    this.isImageOneShow = true;
	    }
		this.currentlyExecuting = false;
      } catch(e) {
        this.currentlyExecuting = false;
        throw e;
      }
    }
	 
  },

  changeTo: function(index) {
  
	 if (!this.currentlyExecuting) {
      try {
        this.currentlyExecuting = true;
        if(this.isImageOneShow){
		     Effect.Fade(this.imageOne,{ duration: 0.1 });
		     this.actualImageIndex = index;
		     this.imageTwo.src = this.images[this.actualImageIndex];
		     setTimeout('Effect.Appear(document.getElementById(\''+this.imageTwo.id+'\'),{ duration: 0.1 });', 100);
		     this.isImageOneShow = false;
	    }else{
		     Effect.Fade(this.imageTwo,{ duration: 0.1 });
		     this.actualImageIndex = index;
		     this.imageOne.src = this.images[this.actualImageIndex];
		     setTimeout('Effect.Appear(document.getElementById(\''+this.imageOne.id+'\'),{ duration: 0.1 });', 100);
		     this.isImageOneShow = true;
	    }
		this.currentlyExecuting = false;
      } catch(e) {
        this.currentlyExecuting = false;
        throw e;
      }
    }
	
  },
  
  onTimerEvent: function() {
    this.next();
  }
  
});
