var mooAccordion = new Class({

	Implements: [Events, Options],
	options: {
		debug: false,
		duration: 500,
		toggler: 'h3',
		acthor: '.details'
	},

	cObj: [],
	cCur: false,
	action: false,

	/* Initialisierung */
	initialize: function(node, options) {
		this.setOptions(options);

		node.addClass('mooAccordion');
		node.getElements('[rel="mooAccordionItem"]').each(function(item, id){
			this.createBox(item, id);
		}.bind(this));

	},

	createBox: function (node, idx) {
		var acthor = node.getElement(this.options.acthor);

		this.cObj[idx] = {
			toggle: node.getElement(this.options.toggler).addEvent('click', function(event){ if(this.action) return; this.toggle(idx);}.bind(this)),
			acthor: acthor,
			state : acthor.get('open'),
			effect: new Fx.Slide(acthor, { link:'cancel', duration: this.options.duration})
		};


		new Element('div.toggle').wraps(this.cObj[idx].toggle);

		if(this.cObj[idx].state == 'open' && !this.cCur) {
			this.cObj[idx].toggle.addClass('open');
			this.cCur = idx;
		} else {
			this.cObj[idx].toggle.addClass('close');
			this.cObj[idx].effect.hide();
		}

	},

	toggle: function(id) {
		this.action = true;
		if(this.cObj[id].state == 'open') {
			this.close(this.cObj[id]);
		} else if(this.cObj[id].state == 'close') {
			this.open(this.cObj[id]);
		}
	},

	open:function(obj) {
		obj.effect.chain(function(){this.action=false;}.bind(this));
		obj.effect.slideIn('vertical');
		obj.state = 'open';
		obj.toggle.addClass('open').removeClass('close');
	},

	close:function(obj) {
		obj.effect.chain(function(){this.action=false;}.bind(this));
		obj.effect.slideOut('vertical');
		obj.state = 'close';
		obj.toggle.addClass('close').removeClass('open');
	}

});

