var pages = $A( [ "Page1", "page2", "page3", "page4"] );
var ContentSwapper = Class.create();
/*
 * This class will swap a div or object's innerHTML content between
 * different supplied content values.
 *
 * Usage: new ContentSwapper('elementId', {
 *    pages: [ 'Page1', 'Page2' ],
 *    effectIn: (function reference to Scriptaculous effect),
 *    effectOut: (function reference to Scriptaculous effect),
 *    effectDuration: (defaults to .5)
 *    swapInterval: (defaults to 6000ms)
 * };
 */
ContentSwapper.prototype = {
  initialize: function(elem, options) {
    this.element = $(elem);
    this.setOptions(options);
    this.timer = "";
    this.init = false;
  },
  setOptions: function(options) {
    this.options = {
      duration: 1.5,
      effectIn: Effect.BlindDown,
      effectOut: Effect.BlindUp,
      swapInterval: 6000
    };
    Object.extend(this.options, options || {});
    this.pages = $A(this.options.pages||[]);
  },
  addPage: function(c) { 
    this.pages.push(c);
  },
  _next: function() {
	 var content = this.pages.shift();
     this.pages.push(content);
     this.element.innerHTML = content;
  },
  _previous: function() {
	 var content = this.pages.pop();
     this.pages.unshift(content);
     content = this.pages.pop();
     this.pages.push(content);
     this.element.innerHTML = content;
  },
  nextImmediate: function() {
  	this._next();
  	Element.show(this.element);
  },
  previousImmediate: function() {
  	this._previous();
  	Element.show(this.element);
  },
  next: function() {
	 Effect.BlindUp(this.element, {
	    duration: this.options.duration/2, 
	    afterFinish: 
	      function() {
	        Element.hide(this.element);
	        this._next();
	        Effect.BlindDown(this.element, {duration: this.options.duration}); 
	      }.bind(this)
	 });
  },
  previous: function() {
	 Effect.BlindUp(this.element, {
	    duration: this.options.duration/2, 
	    afterFinish: 
	      function() {
	        Element.hide(this.element);
	        this._previous();
	        Effect.BlindDown(this.element, {duration: this.options.duration}); 
	      }.bind(this)
	 });
  },
  _setTimer: function() {
  	 this.timer = setTimeout(function() {
  	  	this.next();
  	  	this._setTimer();
  	 }.bind(this), this.options.swapInterval); 
  },
  start: function() {
  	if (!this.timer) { 
  	  if (!this.init) { this.nextImmediate(); this.init=true; }
  	  this._setTimer();
  	} 
  },
  stop: function() {
  	if (this.timer) { clearTimeout(this.timer); this.timer=""; } 
  }

};
