// JavaScript Document

var Accordion = Class.create();

Accordion.prototype = {

	show: function(index, force) {
		if ( (index >= this.length) || (index < 0) ) {
			//alert("index out of range");
			return;
		}

		if ( $('visible') == this.item[index] ){
			
			if (force) {
				//alert("force to show the visible element.");
				for(var i = 0; i < this.length; i++) {
					if(this._body(this.item[i]).style.display != "none") {
						new Effect.SlideUp(this._body(this.item[i]));
					}
				}
				new Effect.SlideDown(this._body(this.item[index]));
				return;
			}
			
			//alert("it's already shown now.");
			return;
		}

		//alert("show another element.");
		new Effect.Parallel(
			[
				new Effect.SlideUp(   this._body($('visible'))     ),
				new Effect.SlideDown( this._body(this.item[index]) )
			], {
				duration: 0.1
			}
		);
	
		$('visible').id = "";
		this.item[index].id = "visible";
		return;
	},
	
	_index:function(i){
		return function() {
			return i;
		}
	},

	_body: function(e) {
		var tags = e.getElementsByTagName('*');
		for(var i in tags) {
			if (tags[i].className == "panelBody") {
				return tags[i];
			}
		}
	},

	initialize: function(id, tag) {
		this.id = id;
		this.tag_header = tag.toUpperCase();
		this.item = new Array();

		var tags = $(id).getElementsByTagName('*');
		
		for(var i in tags) {

			switch(tags[i].tagName) {
				case "DIV":
					if (tags[i].className == "panel") {
						tags[i]._index = this._index(this.item.length);
						
						this.item[this.item.length] = tags[i];
						if (this.item.length == 1) {
							tags[i].id = "visible";
						}
					}
					if (tags[i].className == "panelBody") {
						tags[i].style.display = "none";
					}
					break;

				case this.tag_header:				
					tags[i].style.cursor = "pointer";
					tags[i].onclick = function() {
						alert(this.parentNode._index());
					}
					break;
			}

		}
		this.length = this.item.length;
		this.show(0, true);
	}

};