var CollapseAbleContent = new Class({
	Implements: [Options, Events],
	options: {
		scanContainer    		: $empty,
		scanClicker				: 'h3',
		addContentContainerCls	: 'cFrame'
		//onTabChanged: $empty
	},
	lastIndex: -1,
	tabPages : [],
	tabContents: [],

	initialize: function(options){
		this.setOptions(options);
		this.makeTabs();
	},
	
	makeTabs: function(){
		var self = this;
		var scanContainer = $(this.options.scanContainer);
		var contents = this.tabContents;
		var heads = this.tabPages = this.options.scanContainer.getElements(this.options.scanClicker); 	
		var content;
		var next = null;

		heads.each(function(el,elIndex){
			content = new Element('div',{'class':self.options.addContentContainerCls});
			content.setStyle('overflow','hidden');
			content.setStyle('height','0');
			//content.setStyle('height',elIndex == 0 ? 'auto' : '0');
			
			//if(elIndex==0) el.addClass("active");
			
			next = el.getNext();
			while(next){
				if(!next.match(self.options.scanClicker)){
					content.adopt(next);
				} else {
					break;
				}
				next = el.getNext();
			}
			content.inject(el,'after');
			self.tabContents[elIndex] = content;
		});
		
		heads.each(function(el){
			el.addEvent('click',function(){
				var cIndex = self.tabPages.indexOf(this);
				var _fx1;			
				if(cIndex == self.lastIndex) {
					el.setStyle('backgroundImage', 'url(/fileadmin/_img/ico-plus.png)');
					_fx1 = new Fx.Morph(self.tabContents[self.lastIndex], {duration : 200,transition : Fx.Transitions.Quart.easeInOut});
					realSize = self.tabContents[self.lastIndex].getScrollSize().y;
					_fx1.start({'height' : [realSize, 0]});
					heads.each(function(el2,iIndex){el2.removeClass('active');});
					self.lastIndex = -1
					return;
				}
				heads.each(function(el2,iIndex){
					el2.setStyle('backgroundImage', 'url(/fileadmin/_img/ico-plus.png)');
					if(cIndex == iIndex) {
						el2.addClass('active');
					} else {
						el2.removeClass('active');
					}
				});
				var realSize = 0;
				if(self.lastIndex > -1 && self.tabContents[self.lastIndex]){
					el.setStyle('backgroundImage', 'url(/fileadmin/_img/ico-plus.png)');
					_fx1 = new Fx.Morph(self.tabContents[self.lastIndex], {duration : 200,transition : Fx.Transitions.Quart.easeInOut});
					realSize = self.tabContents[self.lastIndex].getScrollSize().y;
					_fx1.start({'height' : [realSize, 0]});
				}
				if(cIndex > -1 && self.tabContents[cIndex]){
					el.setStyle('backgroundImage', 'url(/fileadmin/_img/ico-minus.png)');
					var _fx2 = new Fx.Morph(self.tabContents[cIndex], {duration : 200,transition : Fx.Transitions.Quart.easeInOut});
					realSize = self.tabContents[cIndex].getScrollSize().y;
					_fx2.start({'height' : [0 , realSize]});
				}
				self.lastIndex = cIndex;
				self.fireEvent('onTabChange',this,cIndex,200);
			});
		});
		self.lastIndex = -1;
		//if(heads[0]) heads[0].fireEvent('click',200);
	}
});

window.addEvent('domready',function(){
	$$('.fadeContent').each(function(el){
		new CollapseAbleContent({scanContainer:el});
	});
});

