var LsFader = new Class(
{
  Implements: [Options, Events],
  
  options:
  {
    parent: null,
    fadeTime: 3000,
    showTime: 5000,
    childrenTag: 'div'
  },

  initialize: function(options)
  {
    this.setOptions(options);
    
    this.currentIndex = 0;
    this.nextIndex = 1;
    this.interval = null;
    this.children = this.options.parent.getElements(this.options.childrenTag);
    this.numberOfLoops = 0;

    // Alle außer dem ersten Container ausblenden
    // und die Container übereinander legen
    for (var i = 0; i < this.children.length; i++)
    {
      this.children[i].setStyle('position', 'absolute');
      if (i > this.currentIndex)
      {
        this.hide(this.children[i]);
      }
    }

    // Fading stoppen bei Mouseover
    this.options.parent.addEvents(
    {
			mouseenter: function()
      {
        this.stop();
      }.bind(this),
			mouseleave: function()
      {
        this.start();
      }.bind(this)
		});

    // Animation starten
    this.start();
  },

  fadeOut: function(element)
  {
    var effect = new Fx.Morph(element,
    {
      duration: this.options.fadeTime
    });
    effect.start({
      'opacity': 0
    });
  },

  fadeIn: function(element)
  {
    var effect = new Fx.Morph(element,
    {
      duration: this.options.fadeTime
    });
    effect.start({
      'opacity': 1
    });
  },

  fade: function()
  {
    this.fadeOut(this.children[this.currentIndex]);
    this.fadeIn(this.children[this.nextIndex]);
    this.currentIndex = this.getNextIndex();
    this.nextIndex = this.getNextIndex();
    this.numberOfLoops++;
  },

  hide: function(element)
  {
    var effect = new Fx.Morph(element);
    effect.set({
      'opacity': 0
    });
  },

  getNextIndex: function()
  {
    if (this.children[this.currentIndex + 1] != null)
    {
      return this.currentIndex + 1;
    }
    else
    {
      return 0;
    }
  },

  start: function()
  {
    this.interval = this.fade.periodical(this.options.showTime + this.options.fadeTime, this);
  },
  
  stop: function()
  {
    clearTimeout(this.interval);
  }
});
