var Crossfade = Class.create();
Object.extend(Object.extend(Crossfade.prototype, Abstract.prototype), {

	initialize: function(wrapper, options) {
		this.fading		= false;
	    this.wrapper	= $(wrapper);
	    this.fader		= this.wrapper.down('div.qcontent');
	    this.sections   = this.wrapper.getElementsBySelector('div.qwrapper');
	    this.options    = Object.extend({ duration: 1.0, frequency: 3, autoFade: true }, options || {});

	    this.sections.each( function(section, index) {
	      section._index = index;
	    });

	    if(this.sections.length > 1) {
			if(this.options.autoFade) this.start();
		}
	},

	fadeNow: function(blendIn, blendOut, duration) {
		new Effect.Opacity(blendOut,
			{from: 1.0, to: 0.0, duration: duration, afterFinish: function(){
				poma.dom.$id(blendOut).style.display = 'none';
				new Effect.Opacity(blendIn, {from: 0.0, to: 1.0, duration: duration, beforeStart: function(){
					poma.dom.$id(blendIn).style.display = 'block';
				}
				});
			}
		});
		return false;
	},

	next: function(){
		if(this.sections.length > 1) {
			if(this.currentIndex) {
				if(this.nextIndex >= this.sections.length) {
					this.nextIndex = 1;
					this.currentIndex = this.sections.length;
				} else {
					this.nextIndex++;
					this.currentIndex = this.nextIndex -1;
				}
			} else {
				for(var i=1;i<=this.sections.length;i++) {
					if(poma.dom.$id("question-"+i).style.display == "block") {
						this.currentIndex = i;
					} else {
						this.currentIndex = 1;
					}
				}
				this.nextIndex = this.currentIndex+1;
				if(this.nextIndex >= this.sections.length) {
					this.nextIndex = 1;
				}
			}
		}
		var currentDiv = 'question-' + this.currentIndex;
		var nextDiv = 'question-' + this.nextIndex;
		var duration = this.options.duration;
		this.fadeNow(nextDiv, currentDiv, duration);
	},

	previous: function(){
		if(this.sections.length > 1) {
			if(this.currentIndex) {
				if(this.nextIndex <= 1) {
					this.nextIndex = this.sections.length;
					this.currentIndex = 1;
				} else {
					this.nextIndex--;
					this.currentIndex = this.nextIndex +1;
				}

			} else {
				for(var i=1;i<=this.sections.length;i++) {
					if(poma.dom.$id("question-"+i).style.display == "block") {
						this.currentIndex = i;
						this.nextIndex = this.currentIndex --;
					} else {
						this.currentIndex = 1;
						this.nextIndex = this.sections.length;
					}
				}
			}
		}
		var currentDiv = 'question-' + this.currentIndex;
		var nextDiv = 'question-' + this.nextIndex;
		var duration = this.options.duration;
		this.fadeNow(nextDiv, currentDiv, duration);
	},

	stop: function()
	{
		clearTimeout(this.timer);
	},

	start: function()
	{
		this.periodicallyUpdate();
	},

	periodicallyUpdate: function()
	{
		if (this.timer != null) {
			clearTimeout(this.timer);
			this.next();
		}
		this.timer = setTimeout(this.periodicallyUpdate.bind(this), this.options.frequency*1000);
	},

	stopFader: function(pauseDiv, playDiv){
		if(poma.dom.$id(pauseDiv).style.display == "inline") {
			poma.dom.$id(pauseDiv).style.display = "none";
			poma.dom.$id(playDiv).style.display = "inline";
			this.stop();
		}
	},

	changeStatus: function(pauseDiv, playDiv) {
		if(poma.dom.$id(pauseDiv).style.display == "none") {
			poma.dom.$id(pauseDiv).style.display = "inline";
			poma.dom.$id(playDiv).style.display = "none";
			this.start();
		} else {
			poma.dom.$id(pauseDiv).style.display = "none";
			poma.dom.$id(playDiv).style.display = "inline";
			this.stop();
		}
	}

});
