/**
 * Akkordion Klasse
 * 
 * @copyright 2010 publimind GmbH
 * @author sebastiano marletta
 */
var Accordion = Class.create({
	element: false,
	
	initialize: function(parameters) {
		if(parameters && parameters.element)
			this.element = parameters.element;
				
		this.setup();
	},
	
	setup: function() {
		this.element.select(".accordion-content").invoke("hide");
		this.element.select(".accordion-toggle").invoke(
			"observe", "click", this.accordionToggle.bind(this)
		);
		
		this.element.select(".accordion-content").first().show();
		this.element.select(".accordion-toggle").first().addClassName("active");
		
		$$(".previous[type=button]", ".next[type=button]").each(function(controlButton) {
			controlButton.observe("click", this.controlClicked.bind(this));
		}.bind(this));
	},
	
	controlClicked: function(clickEvent) {
		var button = clickEvent.findElement();
		
		this.element.select(".accordion-toggle.active").each(function(activeShard) {
			activeShard.removeClassName("active").next().hide();
			
			if(button.hasClassName("next")) {
				activeShard.next().next().addClassName("active").next().show();
			}else if(button.hasClassName("previous")) {
				activeShard.previous().previous().addClassName("active").next().show();
			}else {
				throw new Exception("TypeError: No direction for controll click detected.");
			}
		});
	},
	
	accordionToggle: function(clickEvent) {
		this.element.select(".accordion-toggle.active").each(function(toggler) {
			toggler.removeClassName("active").next().hide();
			clickEvent.findElement().addClassName("active").next().show();
		});
	}
});
