var Reveal = Class.create();

Reveal.prototype = {
	initialize:function() {
		this._faqlist = null;
		this._current = null;
		this._allOpen = false;

		this._version = '1.0.0';
	},

	setup:function(dat) {
		this._list = dat;
		this.closeAll(false);	
		this.clickListener = this._clickEvent.bindAsEventListener(this);
		for (var faq in this._list)
		{
			faqdesc = this._list[faq];
			for (var i=0; i<faqdesc.length; i++) {
				Event.observe(faqdesc[i], 'click', this.clickListener);
			}

		}
	},

	toggleHead:function(faq, animate) {

		animate = (typeof(animate)=='boolean') ?animate:true;
		var head;

		var current;
		if (this._allOpen==false) {
			current = this._isFaqHeadOpen();
		}
		if(current) {
			head = current + '_head';
			Effect.toggle(head, 'blind' , {duration:0.5, queue:{scope:'faqs',limit:2, position:'start'}});
		}
		if(current == faq) {
			return;
		}
		head = faq + '_head';
		Effect.toggle(head, 'blind' , {duration:0.5, queue:{scope:'faqs',limit:2, position:'end'}});
	},

	toggle:function(faq, animate) {

		animate = (typeof(animate)=='boolean') ?animate:true;
		var desc;

		var current;
		if (this._allOpen==false) {
			current = this._isFaqOpen();
		}

		if(current) {
			desc = current + '_desc';
			Effect.toggle(desc, 'blind' , {duration:0.5, queue:{scope:'faqs',limit:2, position:'start'}});
			var elfaq = document.getElementById(current);
			elfaq.className = 'end';
		}

		if(current == faq) {
			return;
		}

		desc = faq + '_desc';

		var elfaq = document.getElementById(faq);
		if(elfaq)
			elfaq.className ='down';

		Effect.toggle(desc, 'blind' , {duration:0.5, queue:{scope:'faqs',limit:2, position:'end'}});
	},

	openHead:function(faq, animate) {
		animate = (typeof(animate)=='boolean') ?animate:true;
		var current;
		var head;
		if(this.allOpen ==false) {
			current = _isFaqHeadOpen();
		}
		if(current) {
			head = current + '_head';
			Effect.toggle(head, 'blind' , {duration:0.5, queue:{scope:'faqs',limit:2, position:'start'}});
		}
		if(current == faq) {
			return;
		}
		head = faq + '_head';
		Effect.toggle(head, 'blind' , {duration:0.5, queue:{scope:'faqs',limit:2, position:'end'}});
	},
	openAll:function(animate) {
		this._clearqueues();

		animate = (typeof(animate)=='boolean') ?animate:true;
		var faqdesc;
		var elem;
		for (var faq in this._list) {
			faqdesc = this._list[faq];
			for (var i=0; i<faqdesc.length; i++) {
				elem = faqdesc[i] + '_desc';
				if(animate) {
					Effect.BlindDown(elem, {duration:0.5, queue:{scope:'faqs', position:'end'}});
				} else {
					Element.show(elem);
				}
			}
		}

		this._allOpen = true;
	},
	
	closeAll:function(animate) {

		this._clearqueues();

		animate = (typeof(animate)=='boolean') ?animate:true;
		var faqdesc;
		var elem;
		for (var faq in this._list) {

			faqdesc = this._list[faq];
			for (var i=0; i<faqdesc.length; i++) {
				elem = faqdesc[i] + '_desc';
				if(animate) {
					Effect.BlindUp(elem, {duration:0.5, queue:{scope:'faqs', position:'end'}});
				} else {
					Element.hide(elem);
				}
			}
		}
		this._allOpen = false;
	},

	_clickHeadEvent:function(event) {
		var ele = Event.element(event);
		if(ele) {
			this.toggleHead(ele.id);
		}
	},
	
	_clickEvent:function(event) {
		var ele = Event.element(event);
		if(ele) {
			this.toggle(ele.id);
		}
	},

	_isFaqHeadOpen:function(faq) {
		var text;

		if(faq) {
			text = faq + '_desc';
			if( Element.visible(text) != '') {
				return faq;
			}
		} else {
			for (var fname in this._list) {
				text = fname + '_head';
				if( Element.visible(text) != '') {
					return fname;
				}
			}
		}
		return null;
	},

	_isFaqOpen:function(faq) {
		var faqdesc;
		var text;

		if(faq) {
			text = faq + '_desc';
			if( Element.visible(text) != '') {
				return faq;
			}
		} else {
			for (var fname in this._list) {

				faqdesc = this._list[fname];
				for (var i=0; i<faqdesc.length; i++) {

					text = faqdesc[i] + '_desc';
					if( Element.visible(text) != '') {
						return faqdesc[i];
					}
				}
			}
		}
		return false;
	},
	
	_clearqueues:function() {
		var queue = Effect.Queues.get('faqs');
		queue.each(function(e) { e.cancel() });
	}

};