// Accordion
var klickbar = 1;


var onloadHooks = [];
window.onload = function(){
	// Get onload Hooks
	for(i = 0; i < onloadHooks.length; i++)	{
		onloadHooks[i]();
	}
	// Remove Blur
	$$("a").addEvent('focus', function(e) {
		this.blur();
	});
	
	checkAccordion();
}

function printme(text){
	text=document;
	print(text);
}









function checkAccordion() {
	
	if($$('.accordion_on_off_plugin_wrap').length) {
		
		var myContent = $$('.c_center');
		var myAccordions = $$('.accordion_on_off_plugin_wrap');
		var AccordionWrap = new Element('div', {id: 'reclay_accordion_on_off'}); 
		
		// Wrap Div in Content-Bereich
		myContent.grab(AccordionWrap);
		// Accordions in eben erstelltes Wrap
		myAccordions.each(function(item, index) {
			$('reclay_accordion_on_off').grab(item);			
		});
		
		// Leere Divs (csc-default) rauskegeln
		var csc_default_divs = $$('.c_center .csc-default');
		csc_default_divs.each(function(item, index) {
			if(!item.getChildren().length) {
				item.destroy();
			};
		});
		
		// Höhe 
		var headlines = $$('.accordion_on_off_plugin_wrap .headline');
		headlines.each(function(item, index) {
			var item_height = item.getNext().getStyle('height');
			item.setProperty('rel', item_height);
			item.getNext().setStyle('height', 0);
			item.addClass('close');
		});
		
		// Einfaden
		$('reclay_accordion_on_off').fade('in');
		
		
		$$('.accordion_on_off_plugin_wrap .headline').addEvents({
		'click': function(event){
			if(klickbar == 1 && this.hasClass('open') == false) {
				klickbar == 0;
				// Checken ob anders offen (wenn ja, dann anderes schließen)
				checkAktivAccordion();
				// Neues Einblenden
				OpenCloseAccordion(this, 'open');
				this.removeClass('close');
				this.addClass('open');
			}
			
		}
	});
		
		
		
		
	}
	
}

function checkAktivAccordion() {
	var headlines = $$('.accordion_on_off_plugin_wrap .headline');
	
	headlines.each(function(item, index) {
		if(item.hasClass('open')) {
			item.removeClass('open');
			item.addClass('close');
			OpenCloseAccordion(item, 'close');
			
		}
	});
	
}

function OpenCloseAccordion(item, openClose) {
	
	var new_height;
	
	if(openClose == 'close') {
		new_height = 0;
	}else {
		new_height = getAccHeight(item);
	}
	
	var Accordion = new Fx.Tween(item.getNext(), {
		'duration': 600,
		'transition': Fx.Transitions.Quint.easeInOut
		});
		Accordion.addEvent('complete', function(){
			klickbar = 1;
			
    });		
		Accordion.start('height',new_height);
	
	
	
}


function getAccHeight(item) {
	return item.getProperty('rel');
}


