var PeriodicalExecuter = Class.create({
  initialize: function(callback, frequency) {
    this.callback = callback;
    this.frequency = frequency;
    this.currentlyExecuting = false;
    this.lastExec = this.now();
    this.paused = false;
    this.pausedAt = 0;
    this.accumTimePaused = 0;
    this.registerCallback();
  },
  registerCallback: function() {
    this.timer = setInterval(this.onTimerEvent.bind(this),
    this.frequency * 1000);
  },
  execute: function() {
    this.callback(this);
  },
  pause: function(){
    if(this.timerRescheduler>0){clearTimeout(this.timerRescheduler)}
    if (this.paused) return;
    this.stop();
    this.paused = true;
    this.pausedAt = this.now();
    return true;
  },
  timeLeft: function(){
    if(this.paused){
      return this.frequency - (this.pausedAt - this.accumTimePaused -
this.lastExec);
    }else if(this.timer==null){
      return null;
    }else{
      return this.frequency - (this.now() - this.accumTimePaused -
this.lastExec);
    }
  },
  restart: function(){
    if (this.paused){
      if(this.timerRescheduler>0){clearTimeout(this.timerRescheduler)}
      this.timerRescheduler =
setTimeout(this.reJobCallback.bind(this), this.timeLeft()*1000);
      this.accumTimePaused += this.now() - this.pausedAt;
    }else if(this.timer == null){
      this.registerCallback();
    }
    this.pausedAt = null;
    this.paused = false;
  },
  reJobCallback : function(){
    this.onTimerEvent();
    this.registerCallback();
    this.accumTimePaused = 0;
  },
  now: function(){ return new Date().getTime() / 1000;},
  stop: function() {
    if (!this.timer) return;
    clearInterval(this.timer);
    this.timer = null;
  },
  onTimerEvent: function() {
    if (!this.currentlyExecuting) {
      try {
        this.currentlyExecuting = true;
        this.execute();
      } finally {
        this.currentlyExecuting = false;
        this.lastExec = this.now();
      }
    }
  }
  });

	var Slideshow = Class.create({
		 initialize: function(delay, totalElmt, elmtName) {
			this.delay = delay;
			this.totalElmt = totalElmt;
			this.paused = 0;
			this.arrSlideElmt = [totalElmt];
			//this.numbers = $('numbers').select("a");
			this.executor;
			//console.info(this.arrSlideElmt);
			this.curElmtNum = 1; //randomize first element
		 	//preload all elements into array and preload all images within element
		 	//$$('ul#numbers a').each(function(tmp) {
						//$(tmp).removeClassName("active");
					//});
		 	//$(this.numbers[0]).addClassName("active");	
			//console.info($(elmtName + i));
			for (i = 1; i <= totalElmt; i++) {
				this.arrSlideElmt[i] = $(elmtName + i);
				
				this.arrSlideElmt[i].observe('mouseover', function() { this.paused = 1; }.bind(this));  //pause on mouseover
				this.arrSlideElmt[i].observe('mouseout', function() { this.paused = 0; }.bind(this));  //resume on mouseout
				
			}
			/*
			$$('ul#numbers a').each(function(node) {
				node.observe('click', function(s){
					this.executor.pause();
					new Effect.Fade(this.arrSlideElmt[this.curElmtNum],{afterFinish:function(){
						this.curElmtNum = parseInt(node.innerHTML);
						new Effect.Appear(this.arrSlideElmt[this.curElmtNum],{afterFinish:function(){
							this.executor.restart();
						}.bind(this)});
						
					}.bind(this)});

					
					$$('ul#numbers a').each(function(tmp) {
						//$(tmp).removeClassName("active");
					});
					//$(node).addClassName("active");
					
						
					s.stop();
				}.bind(this));
			}.bind(this));	*/		
		 },
		start: function() {
			//show first element without effect
			this.executor = new PeriodicalExecuter(function() { 
				this.next(); //start slidehow
			}.bind(this), this.delay);		
		},

		
		next: function(){
			if (!this.paused) {
				this.update();
			}
		},
		update: function() {
			new Effect.Fade(this.arrSlideElmt[this.curElmtNum],{afterFinish:function(){
				this.checkSlide();
				new Effect.Appear(this.arrSlideElmt[this.curElmtNum]);
			}.bind(this)});
		},
		checkSlide: function() {
			var current_number1 = 0;
			if (this.curElmtNum == this.totalElmt) { 
				this.curElmtNum = 1; 
				current_number1 = 0;
			}
			else { 
				this.curElmtNum ++; 
				current_number1 = this.curElmtNum-1;
			}
			$$('ul#numbers a').each(function(tmp) {
						//$(tmp).removeClassName("active");
					});
			//$(this.numbers[current_number1]).addClassName("active");		
		}
	});