/*
*
*  PertimmSlideShow based on prototype.js 
*  & scriptaculous
*
*/

SlideShow = Class.create( {
  initialize: function() {
    // Get config
    this.loadingTime        = CTRloadingTime;
    this.slideInterval      = CTRslideInterval/1000;
    this.transitionDuration = CTRtransitionDuration/1000;
    this.rotateAction       = CTRrotateAction;
    
    // Init slide position
    this.slides = $$('#fpss-slider .slide');
    this.previousSlide = this.slides.size() - 1;
    this.currentSlide  = 0;
    
    // Set listeners on navigation links
    this.buttons = $$('#navi-outer a.button');
    this.buttons.invoke('observe', this.rotateAction, this.onHover.bindAsEventListener(this));
    $('fpss-container_prev').observe(this.rotateAction, this.onPrevious.bindAsEventListener(this));
    $('fpss-container_next').observe(this.rotateAction, this.onNext.bindAsEventListener(this));
    
    // Start slideshow after preloader image delay time in milliseconds.
    this.timeout = setTimeout(this.start.bind(this), this.loadingTime);
  },
  
  onNext: function(event){
    event.stop();
    this.stop();
    this.showNext();
    this.play();
  },
  
  onPrevious: function(event){
    event.stop();
    this.stop();
    this.showPrevious();
    this.play();
  },
  
  onHover: function(event){
    event.stop();
    this.stop();
    this.previousSlide = this.currentSlide;
    for (j = 0; j < this.buttons.length; ++j) {
      if (event.findElement('a') == this.buttons[j]) this.currentSlide = j;
    }
    this.update();
    this.play();
  },
  
  start: function(){
    this.slides.invoke('hide');
    $('slide-loading').hide();
    $('slide-wrapper').setStyle({display: 'block'});
    this.update();
    this.play();
  },
  
  play: function(){
    this.slideShowTimer = new PeriodicalExecuter(this.showNext.bind(this), this.slideInterval);
  },
  
  stop: function(){
    this.slideShowTimer.stop();
  },
  
  showPrevious: function(){
    this.previousSlide = this.currentSlide;
    if (this.currentSlide == 0) this.currentSlide = this.slides.size() - 1;
    else this.currentSlide--;
    this.update();
  },
  
  showNext: function(){
    this.previousSlide = this.currentSlide;
    if (this.currentSlide == this.slides.size() - 1 ) this.currentSlide = 0;
    else this.currentSlide++;
    this.update();
  },
  
  update: function(){
    if (this.previousSlide != this.currentSlide){
      this.buttons[this.previousSlide].removeClassName('navi-active').addClassName('off');
      this.buttons[this.currentSlide].removeClassName('off').addClassName('navi-active');
      new Effect.Fade(this.slides[this.previousSlide], {duration:this.transitionDuration});
      new Effect.Appear(this.slides[this.currentSlide], {duration:this.transitionDuration});
    }
  }
  
});

function showPrev(){ return false; };
function clearSlide(){ return false; };

document.observe('dom:loaded', function(){new SlideShow()});