jQuery(document).ready(function(){
	
	$('.animation .picture').attr('id', function(index){
		index = index || '';
		index++;
		return "bild"+index;
	});	
	
	$(document).everyTime(7000, function() {
			var count = $('.animation .picture').size();
			var id = parseInt($('.animation .picture.active').attr('id').substr(4));
			var newid = id+1;
			preload(newid);
			effect(id, newid, count);
	});
	
	function preload(newid){
		var pfad = geturl($('.animation #bild'+newid).css('background-image'));
		preloadimage = new Image();
		preloadimage.src = pfad;
	}
	
	function geturl($background_image){
		return $background_image.replace("url(","").replace(")","");
	}
		
	function effect(id, newid, count) {
		if (newid>count) newid=1;
		$('.animation #bild'+id).fadeOut(1100, function(){
			$('.animation #bild'+newid).fadeIn(1700, function(){
				$(this).addClass('active');
			});
			$(this).removeClass('active');
		});		
	}
	
	$('.animation .picture:first').addClass('active');
	preload(1);
	
});
