/*
inspried by code at:
http://www.hidesign.be/js/simpleimageviewer-1.1.js
*/ 
var FadeInFadeOut = new Class({
	
	options: {
		autoPlay: true,
		autoPlayDuration: 10000,
		duration: 500,
		loop: true,
		transition: Fx.Transitions.Sine.easeOut,
		cur: 0,
		nex: 1,
		testarr: []
	},

	initialize: function(options) {
	
		this.setOptions(options);
		var req = new Request.HTML({url:'/wp-content/themes/myep/includes/testimonials.txt', 
			onSuccess: function(html) {
				$("hiddentest").adopt(html);
			}
			
		}).send();

		// Start
		this.autoPlay();
	},
	
	buildLibrary: function() {
		this.library = {};
		this.library.current = -1;
		this.library.links = this.options.testarr;
	},
	
	autoPlay: function() {
		this.periodical = (function(){ this.next(); }).periodical(this.options.autoPlayDuration, this);
	},

	
	next: function() {
		this.options.testarr = $$("#hiddentest span");
		this.options.max = this.options.testarr.length;
		
		if (this.options.cur == (this.options.max - 1) ) { //we're at the end of the line
			this.options.nex = 0;
			this.options.cur = this.options.nex;
		} else {
			this.options.nex = this.options.cur + 1;
			this.options.cur = this.options.cur + 1;
		};
		
		this.hide_tex(this.options.nex);
	},
		
	show: function(el) {
		var fx = new Fx.Morph(el, { duration: this.options.duration, transition: this.options.transition }).set(0);
		fx.start({'opacity': 1} );
		return this;
	},
	
	hide_tex: function() {
			var fx = new Fx.Morph("testimonial", { 
			duration: this.options.duration / 2,
			delay: 5000,
			onComplete: function() {
				$('testimonial').set("html", this.options.testarr[this.options.nex].get("text") );
				this.show("testimonial");
			}.bind(this) 
		}).start({'opacity': 0});
		return this;
	}

});

FadeInFadeOut.implement(new Chain, new Options, new Events);


