/**
* FAQs
* @author Beau Bishop beau [at] webteks [dot] com
* @date 2008-08-06
*/
var MooFAQs = new Class({
	options: {
		question: { 
			_element: "dt", 
			_class: "" 
		},
		answer: {
			_element: "dd",
			_class: ""
		},
		answers: [],
		slideDuration: 800, ///< duration of slide transition
		showAtStart: true,	///< index of slide to display on startup, default is null or none
		hideAtStart: true,	///< hide all slides, except showAtStart (if not null)
		hideOthers: true	///< hide other slides when toggling
	},
	initialize: function (options) {
		this.setOptions(options);
		this.answers = [];
		this.questions = $A($$('.'+this.options.question._class));
		var current = -1;
		$$('.'+this.options.answer._class).each(function(answer) {
			current=current+1;
			this.answers.include(new Fx.Slide(answer.id,{duration:this.options.slideDuration}));
			this.options.answers.include(answer.id);
			if(this.options.hideAtStart && this.options.showAtStart!=null) this.answers[current].hide();
			this.questions[current].addEvent("click", this.displaySlide.bindAsEventListener(this,current,answer));
		}.bind(this));
	},
	displaySlide: function(e,i,slide) {
		e = new Event(e);
		if (this.options.hideOthers) for (var n = 0; n < this.answers.length; n++) if (n != i) this.closeSlide(n);
		$(this.options.answers[i]).style.height = 'auto';
		$(this.options.answers[i]).style.visibility = 'visible';
		(this.answers[i].wrapper.offsetHeight != 0) ? this.fadeSlide(i,0) : this.fadeSlide(i,1);
		e.stop();
	},
	closeSlide: function (i) {
		if (this.answers[i].wrapper.offsetHeight != 0) {
			this.fadeSlide(i,1);
		}
	},
	fadeSlide: function (i,startOpacity) {
		var fx = new Fx.Style(this.options.answers[i], 'opacity', { duration: this.options.slideDuration, wait:false }).start(startOpacity);
		this.answers[i].toggle();
	}	
});
MooFAQs.implement(new Options);