$(document).ready(function() {
    function animateTeaser(element, i, flag){
        if(flag == 1){
            $('body').animate( { timer:"10"}, 2000, function(){
                $(element).css('display','block');
                animateTeaser(element, i , 0);
            });        
        }else{
            if(i > 0){
                $(element[i-1]).animate( { opacity:"0"}, 2000);
            }    
            if(i == 0){
                $(element[element.length-1]).animate( { opacity:"0"}, 2000);
            }
            $(element[i]).animate( { opacity:"1"}, 3000, function(){
				if(i == element.length-1){
					window.setTimeout(function(){animateTeaser(element, 0, 0)}, 2500);
				}else{
					window.setTimeout(function(){animateTeaser(element, (i+1),0)}, 2500);
				}
            });    
        }        
    }
    function shortString(str){
        var newStr = '';
        var counter = 80;
        for (var i = 0; i < counter && i < str.length ; i++) {
            if((i+1) == counter && str.charAt(i+1) != '.'){
                if(str.charAt(i) != ' '){ 
                    newStr += str.charAt(i);
                    counter++;
                }
            }else{
                newStr += str.charAt(i);
            }
        }
        return newStr;
    }
    function checkLength(str){
        if(str.length < 80){
            return '';
        }
        return '...';
    }
	$.ajax({
	  url: "teaser_startseite.html",
	  success: function(data){
		$('#wrapper_teaser').html(data);		
		var teaserList = $('.teaser_box_kundenportal');
		teaserList.each(function(i, element){
			var str = $(element).children('p').html();
			$(element).children('p').html(shortString(str) + checkLength(str));
			if(i != 0){
				$(element).animate( { opacity:"0"}, 1);
			}
		});
		animateTeaser(teaserList , 1, 1);	
	  }
	});
});
