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;
		this.options.effectIn= Prototype.Browser.IE?Effect.SlideDown:Effect.BlindDown;
		this.options.effectOut= Prototype.Browser.IE?Effect.SlideUp:Effect.BlindUp;
	},
	setOptions: function(options) {
		this.options = {
		  duration: 3.5,
		  effectIn: Effect.BlindDown,
		  effectOut: Effect.BlindUp,
		  swapInterval: 12000
		};
		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).update(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();
		this.element.show();
	},
	previousImmediate: function() {
		this._previous();
		this.element.show();
	},
	next: function() {
		this.options.effectOut(this.element, {
			duration: this.options.duration/2, 
			afterFinish: 
			  function() {
				this.element.hide();
				this._next();
				this.options.effectIn(this.element, {duration: this.options.duration}); 
			}.bind(this)
		});
	},
	previous: function() {
		this.options.effectOut(this.element, {
			duration: this.options.duration/2, 
			afterFinish: 
			  function() {
				this.element.hide();
				this._previous();
				this.options.effectIn(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=""; } 
	}
};

