/**
 *  jail fader.js v0.0.1, Fri Mar 14 2008
 *    (c) 2008 Melissa Forrest (http://www.melforrest.com)
 *    and is released under the MIT License:
 *    http://www.opensource.org/licenses/mit-license.php
 *
 */

var Fader = Class.create();
Fader.prototype = {

  active: null,
  showing: null,
  effects: [],
  
  initialize: function(container, options) {
    
    if (!$(container)) {
      throw(container+" doesn't exist!");
      return false;
    }
    
    this.container = container;
    this.options = this._set_options(options);
    
    $(this.container).childElements().invoke('hide');
    
    Effect.Appear($(this.container).firstDescendant(), { duration: this.options.duration });
    
    this.active = $(this.container).firstDescendant();
    
    new PeriodicalExecuter(this.show_next.bind(this), this.options.timer);
    
  },
  
  show_next: function() {
    if($(this.active).next()) {
      this.showing = $(this.active).next();
    } else {
      this.showing = $(this.container).firstDescendant();
    }
    
    this.effects = [];
    this.effects.push(
      new Effect.Fade(this.active, {duration: this.options.hide_duration}),
      new Effect.Appear(this.showing, {duration: this.options.duration})
    );
    
    new Effect.Parallel(this.effects);
    
    this.active = this.showing;
  },
  
  _option_defaults: {
    timer: 15,
    duration: 2.0,
    hide_duration: 0.3
  },
  
  _set_options: function(options) {
    if(typeof options != "undefined") {		
      var results = [];
      for(option in this._option_defaults) {
        if(typeof options[option] == "undefined") {
          results[option] = this._option_defaults[option];
        } else {
          results[option] = options[option];
        }
      }
      return results;
    } else {
      return this._option_defaults;
    }
  }

}