

jQuery(function($) {
			
			
	// Falls die Konstante nicht gesetzt wurde...
	if(typeof tx_kiwiaccordion_effect == 'undefined') {
		tx_kiwiaccordion_effect = 'none';
	}
	
	if(typeof tx_kiwiaccordion_openingSpeed == 'undefined') {
		tx_kiwiaccordion_openingSpeed = 500;
	}
	if(typeof tx_kiwiaccordion_closingSpeed == 'undefined') {
		tx_kiwiaccordion_closingSpeed = 500;
	}
	
	if(typeof tx_kiwiaccordion_easing == 'undefined') {
		tx_kiwiaccordion_easing = 'easeOutExpo';
	}
	// Elemente vorbereiten
	$('.ka-panel').each(function() {
		//Erste Überschrift suchen
		$header = $(':header:first', this);		
		//Fehler Behandlung wenn keine Überschrift vorhanden ist
		if($header.length == 0) {
			$(this).addClass('ka-error').removeClass('ka-panel');
			console.error('This panel contains no header.', this);
		}
		else {
			//kleiner trick um <div class="csc-header"><h1>... abzufangen			
			if($header.parent().find('*').length == 1 && !$header.parent().is('.ka-panel')) {
				$header.parent().addClass('ka-handler');
			}
			else {
				$header.addClass('ka-handler');
			}
			
			//Inhalte umschließen für die Ansprache
			$('.ka-handler', this).siblings().wrapAll('<div class="ka-content"></div>');
			    
			//höhe der divs auslesen und inithöhe setzen
			contentHeight = $('.ka-content', this).height();
			
			$('.ka-handler>h3', this).wrapAll('<div class="h3"></div>');
			//höhe des headers auslesen
			headerHeight = $('.ka-handler>div.h3', this).height();
			
			//bild margin-top setzen
			$('.ka-content div.csc-textpic div.csc-textpic-imagewrap .csc-textpic-image img', this).css('margin-top', headerHeight+25);
			
			// die Zielhöhe fürs ausklappen speichern
				//console.log('headerHeight:'+ headerHeight);
			$('.ka-content', this).data('targetHeight', contentHeight);
			
			
			//$('.ka-content', this).css('height', contentHeight[i])
			$('.ka-content', this).css('height', heightClosed);
			
			
		
				
			//prüfen ob ein Fehler aufgetreten ist
			if($('.ka-content .ka-handler', this).length > 0) {
				console.error('Handler may not be wrapped by more then one element.', this);
				$(this).addClass('ka-error').removeClass('ka-panel');
			}
		}
	});

	// Für ein paar Effekte
	$('.ka-panel .ka-handler').hover(function() {
		$(this).parents('.ka-panel').addClass('hover');
	}, function() {
		$(this).parents('.ka-panel').removeClass('hover');
	});
	
	// Versteckte Inhalte nicht anzeigen
	//$('.ka-panel.accclose .ka-content').hide();
	
	// Eventhandler
	$('.ka-handler').click(function(event, data) {
									
		//console.log(datas)
		
		$panel = $(this).parents('.ka-panel');
		$content = $panel.find('.ka-content');	
		
		
		if($panel.is('.accclose')) {
			$('.ka-panel.ka-opend').removeClass('ka-opend');
			//Dieses Panel aufklappen
			switch(tx_kiwiaccordion_effect) {
				case 'slide':
					//$content.slideDown();
					$content.animate({height: $content.data('targetHeight'), easing: tx_kiwiaccordion_easing }, tx_kiwiaccordion_openingSpeed);
					break;
				case 'fade':
					$content.fadeIn(tx_kiwiaccordion_speed, tx_kiwiaccordion_easing);
					break;
				default:
					$content.show();				
			}
			$panel.removeClass('accclose').addClass('accopen');
			$(this).css('overflow', 'visible');
			//Wenn nur ein offenes Panel erlaubt ist, andere Panels schließen
			if(tx_kiwiaccordion_exclusive) {
				
				$('.ka-panel.accopen .ka-handler').trigger('click', {clicked: $('.ka-panel').index($panel)});
				//$('.ka-panel .accopen .ka-handler').click();
				
			}
		}
		else {
			if(!data) {
				data = { clicked: -1 };
				
			}
			if(data.clicked != $('.ka-panel').index($panel)) {
				
				//Diesen Panel zuklappen
				switch(tx_kiwiaccordion_effect) {
					case 'slide':
						//$content.slideUp();
						$content.animate({height: heightClosed, easing: tx_kiwiaccordion_easing }, tx_kiwiaccordion_closingSpeed);
						break;
					case 'fade':
						$content.fadeOut(tx_kiwiaccordion_speed, tx_kiwiaccordion_easing);
						break;
					default:
						$content.hide();				
				}
				$panel.removeClass('accopen').addClass('accclose');
				$(this).css('overflow', 'hidden');
			}
		}
	});	
});
