//- COPYRIGHT 2009 CITRUS MEDIA GROUP -//

if (typeof(Mootils) === 'undefined') throw 'Mootils.SlideShow requires the Mootils base.';

Mootils.SlideShow = new Class({
  Implements: [Options, Events],
  options: {
  	delay: 4350,
  	fadeDelay: 1000,
    randomStart: true
  },
  initialize: function(container, slides, options) {
    this.index = 0;
    this.div = $(container);
    if (this.div.getStyle('position') === 'static') this.div.setStyle('position', 'relative');
    this.slides = $splat(slides);
    this.setOptions(options);
    if (this.options.randomStart) this.randomize();
    this.setup();
    this.play();
  },
  setup: function() {
    var i = this.slides.length;
    var s = this.div.getSize();
    while (i--) {
      if ($type(this.slides[i]) === 'string') {
        this.slides[i] = new Element('div', {
          'styles': {
            'position': 'absolute',
            'left': 0,
            'top': 0,
            'width': s.x,
            'height': s.y,
            'opacity': 0,
            'background': 'transparent url(' + this.slides[i] + ') 0 0 no-repeat'
          }
        }).inject(this.div);
      }
    }
  },
  play: function() {
    this.tInt = this.transition.periodical(this.options.delay, this);  
  },
  stop: function() {
    this.index = 0;
    this.tInt = $clear(this.tInt);
  },
  transition: function() {
    if ($chk(this.lastIndex)) this.slides[this.lastIndex].get('tween', { property: 'opacity', duration: this.options.fadeDelay, onComplete: function(){
      this.slides[this.lastIndex].setStyle('z-index', 0);
    }.bind(this)}).start(0);
    this.slides[this.index].setStyle('z-index', 4000).get('tween', { property: 'opacity', duration: this.options.fadeDelay }).start(1);
    this.lastIndex = this.index;
    this.advance();
  },
  randomize: function() {
  	this.index = Mootils.randomRange(0, this.slides.length - 1);
  	this.div.setStyle('background-image', 'url(' + this.slides[this.index] + ')');
  	this.advance();
  },
  advance: function() {
    this.index++;
    if (this.slides.length <= this.index) this.index = 0;
  }
});