/*----------------------------------------------------------------------------
    Title: slideshow  
   Author: MARIO GRUSCHETZKI // MEDIENDESIGN
     Date: 18-06-2008
  Project: puder-consotio.de

  (c) Copyright 2008 Mario Gruschetzki. All Rights Reserved. 
----------------------------------------------------------------------------*/

var Slideshow = Class.create({
  initialize: function(){
    this.current_slide = 0;
    this.stop = false;
  },
  start: function(slideshow,slide_element,timeout){
    this.slideshow  = slideshow;
    this.slide_el   = slide_element;
    this.timeout    = timeout;
    this.slides = $(this.slideshow).select(this.slide_el);                      //  All slides on this.slideshow
    this.setup();                                                               //  prepear the slide list (first li => highest zIndex)
    this.prog_dest ? this.create_progress_display() : null;                     //  Add progress display for first loading if it's required
    this.change();                                                              //  start loop
  },
  next: function(){
    this.setup();

    if(this.prog_opt && $(this.prog_opt.id)){                                   //  if progressbar is loaded then fade it
        Effect.Fade(this.prog_opt.id,{
          duration: 0.3,
          afterFinish: function(effect){effect.element.remove()}
        });
    }

    Effect.Fade(this.slides[this.current_slide], {                              //  fades out the current slide => the next slide becomes visible
      afterFinish: function(effect) {
        effect.element.setStyle({zIndex: 0, opacity: 1}).show();
      }});
    this.current_slide = (this.current_slide + 1) % this.slides.length;   
  },
  change: function(){
    var self = this;

    new PeriodicalExecuter(function(pe){self.stop ? pe.stop() : self.next();},  //  execute this.next() after this.timeout
      self.timeout);
  },
  setup: function(){
    var self = this;

    this.slides.each(function(slide, index){
        slide = self.slides[(self.current_slide + index) % self.slides.length];
        slide.setStyle({zIndex: self.slides.length - index});
    });
  },
  config_progress: function(progress_dest, progress_tag, progress_tag_opt ){
    this.prog_dest  = progress_dest || null;
    this.prog_tag   = progress_tag || null;
    this.prog_opt   = progress_tag_opt || null;   
  },
  create_progress_display: function(){
    var self  = this;   
    var el    = new Element(self.prog_tag,self.prog_opt).hide();

    $(self.prog_dest).insert(el);
    Effect.Appear(el);  
  }
});