var $currentPic = 1;
var intervalId;

function iniAnimationsButtons() {
	var $container = jQuery('#animation-box img');
	var $anzahlBilder = $container.length;
	jQuery('#animation-buttons').html("");
	for ($i=1;$i<=$anzahlBilder;$i++) {
		if ($i == $currentPic) {
			var $activeClass = " class='active'";
		} else {
			var $activeClass = "";
		}
		var $currentHtml = jQuery('#animation-buttons').html();
		jQuery('#animation-buttons').html($currentHtml + "<a href='javascript:void(0);' onclick='showImage(" + ($i - 1) + ");'" + $activeClass + "></a>");
	}
	var $currentHtml = jQuery('#animation-buttons').html();
	jQuery('#animation-buttons').html($currentHtml + "<br style='clear: both;' />");
}

function showImage($image) {
	if ($currentPic == ($image + 1)) {
		return false;
	} else {
		var $container = jQuery('#animation-box img');
		var $active = jQuery('#animation-box img.active');
		if ($active.length == 0) {
			$active = jQuery('#animation-box img:last');
		}
		$active.addClass('last-active');
		var $next = jQuery('#animation-box img:eq('+$image+')');
		$next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 500, function() {
			$active.removeClass('active last-active');
		});
		$currentPic = $image + 1;
		iniAnimationsButtons();
		clearInterval(intervalId);
	}
}

function startAnimation() {
	var $container = jQuery('#animation-box img');
	var $anzahlBilder = $container.length;
	var $active = jQuery('#animation-box img.active');
	if ($active.length == 0) {
		$active = jQuery('#animation-box img:last');
	}
	var $next = $active.next().length ? $active.next() : jQuery('#animation-box img:first');
	$active.addClass('last-active');
	$next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000, function() {
		$active.removeClass('active last-active');
	});
	if (($currentPic + 1) > $anzahlBilder) {
		$currentPic = 1;
	} else {
		$currentPic = $currentPic + 1;
	}
	iniAnimationsButtons();
}

jQuery(document).ready( function() {
	if(jQuery('#animation-box').length > 0) {
		iniAnimationsButtons();
		intervalId = setInterval("startAnimation()", 5000);
	}
});
