/*
mooSimpleSlide - Simple SlideShow Class
version: 0.3
copyright 2008 04 02 - Huug Helmink, Ace Group bv

modified by Ray Rehbein, mrrehbein@gmail.com, Ray of Code LLC to work with mooTools 1.2

mootools v.1.2 classes
Core: Core
Class: Class, Class.Extras
Native: Array, String, Function, Number, Element
Fx: Fx.Base, Fx.Style


Usage:
  var mySlideShow = new mooSimpleSlide([images::array]);
    or
  var mySlideShow = new mooSimpleSlide([images::array],{period:[interval between images in ms::integer]});
  mySlideShow.displayImage();
*/
var mooSimpleSlide = new Class({
  options: {
    period: 0,
    show_display: 'inline'
  },
  initialize: function(imageArray,options) {
    // Check if imageArray is an array
    if($type(imageArray) != 'array') return;
    
    this.images = imageArray;
    this.setOptions(options);
    this.active = 0;
    var FxTransitionTime = this.options.period/5;
    
    var onComplete = function(item) {
      if (item.getStyle('opacity') < 0.1) {
        item.setStyle('display','none');
      }
    };
    var onStart = function(item) {
       if (item.getStyle('opacity') < 0.1) {
        item.setStyle('display', this.options.show_display);
      }
    };
    
    // Set styles so images will fade in and out nicely
    this.images.each(function(img) {
      img.setStyles({
        'display': 'none',
        'position': 'absolute',
        'opacity': 0
      });
      img.set('tween',{'duration': FxTransitionTime, 'onComplete': onComplete, 'onStart': onStart});
    });
    this.images[0].getParent().setStyle('position','relative');
    this.displayImage(0);
    // If period options is set > 0, periodical display an image
    if(this.options.period > 0) this.next.periodical(this.options.period,this);
  },

  displayImage: function(active) {
    // Hide image
    if ($defined(this.images[this.active])) {
      this.images[this.active].tween('opacity', 1, 0);
    }
    
    // Set new image
    this.active = active;
    
    // Show new image
    if (!$defined(this.images[this.active])) {
      this.active=0;
    }
    
    this.images[this.active].tween('opacity', 0, 1);
  },
  
  prev: function() {
    var active = this.active;
    if (--active < 0) { active = this.images.length-1; }
    this.displayImage(active);
  },
  
  next: function() {
    var active = this.active;
    if (++active >= this.images.length) { active = 0; }
    this.displayImage(active);
  }
});
mooSimpleSlide.implement(new Options);
