var show_animation = true; // legt fest ob animiert werden soll > admin/settings
var speedfaktor = 1.6;	   // speedfaktor > admin/settings

$(function(){
	new Image().src = "images/nbutt_close.png";
	var maxTextheight = 168;   // Die höhe ab der abgeschnitten wird
	var clipTextheight = 128;  // Die höhe auf die reduziert wird

	// @CTO: please do object magic !
	$( ".start_b_more" ).each(function(){
		this.ausgeklappt = false;
		this.wird_animiert = false;

		this.realHeight = $( ".klapptext", this.parentNode.parentNode.parentNode )[0].scrollHeight;         //die tatsächliche höhe des textcontainers
		this.duration = Math.floor( this.realHeight * speedfaktor ); //= millisekunden: höhe * speedfaktor

		if( this.realHeight < maxTextheight ){
			$( this ).css( "display", "none" );
			$( ".klapptext", this.parentNode.parentNode.parentNode ).css( "height", maxTextheight +"px" );
		} else {
			$( ".klapptext", this.parentNode.parentNode.parentNode ).css( "height", clipTextheight +"px" ).dotdotdot();
			$( this ).css( "display", "block" );
		}
	});

	$( ".start_b_more" ).click(function () {
		if( this.wird_animiert == false ){
			if( this.ausgeklappt ){
				this.ausgeklappt = false;
				if( show_animation ){
					this.wird_animiert = true;
					var entry = this;
					$( ".klapptext", this.parentNode.parentNode.parentNode ).animate({ height: clipTextheight + "px" }, this.duration, function() { entry.wird_animiert = false; $( ".klapptext", entry.parentNode.parentNode.parentNode ).dotdotdot(); });
				}else{
					$( ".klapptext", this.parentNode.parentNode.parentNode ).css( "height", clipTextheight + "px" );
				}
				$( this ).html( "mehr" );
				$( this ).removeClass( "start_b_close" );
			}else{
				this.ausgeklappt = true;
				$( ".klapptext", this.parentNode.parentNode.parentNode ).trigger("destroy");
				if (show_animation) {
					this.wird_animiert = true;
					var entry = this;
					$( ".klapptext", this.parentNode.parentNode.parentNode ).animate({ height: this.realHeight + "px" }, this.duration, function() { entry.wird_animiert = false; });
				} else {
					$( ".klapptext", this.parentNode.parentNode.parentNode ).css( "height", "auto" );
				}
				$( this ).html( "schließen" );
				$( this ).addClass( "start_b_close" );
			}
		}
	});



	$("#ec1").mouseenter(function () {
		if(jQuery.support.leadingWhitespace) {
			$("#ec1pop").fadeIn('fast');
		}else{
			$("#ec1pop").css("display","block");
		}
	});

	$("#ec1").mouseleave(function () {
		if(jQuery.support.leadingWhitespace) {
			$("#ec1pop").fadeOut('fast');
		}else{
			$("#ec1pop").css("display","none");
		}
	});
});
