// Geschwindigkeit des Überblendens
var speedFading = 1500;

// Zeitdauer, wie lange ein Bild angezeigt wird
var speedInterval = 5000;

function rotator() {
	$('#rotator .content li').css({'opacity' : '0.0'});
	$('#rotator .content li:first').css({'opacity' : '1.0'});
	var rotation = setInterval('rotation()',speedInterval);

	$('#rotator .nav a').click(function () {

		if ($(this).parent().hasClass('active')) {  // Links, die gerade aktiv sind, bleiben ohne Aktion
			return false;
		} else {
			clearInterval(rotation);
			var i = parseInt($(this).attr('rel'));
			i--;
			var currentimage = $('#rotator .content li.active');
			var nextimage = $('#rotator .content li:eq(' + i + ')');
			nextimage.addClass('active');
			currentimage.removeClass('active');
			//Set the fade in effect for the next image, the active class has higher z-index
			nextimage.css({'opacity' : '0.0'}).addClass('active').animate({'opacity' : '1.0'}, speedFading);
			//Hide the current image
			currentimage.animate({'opacity' : '0.0'}, speedFading).removeClass('active');

			$('#rotator .nav li').removeClass('active');
			$('#rotator .nav li:eq(' + i + ')').addClass('active');
		}
	});
}

function rotation() {
	var currentButton = ($('#rotator .nav li.active') ?  $('#rotator .nav li.active') : $('#rotator .nav li:first'));
	var nextButton = ((currentButton.next().length) ? ((currentButton.next().hasClass('active')) ? $('#rotator .nav li:first') : currentButton.next()) : $('#rotator .nav li:first'));
	nextButton.addClass('active');
	currentButton.removeClass('active');

	var currentimage = ($('#rotator .content li.active') ?  $('#rotator .content li.active') : $('#rotator .content li:first'));
	var nextimage = ((currentimage.next().length) ? ((currentimage.next().hasClass('active')) ? $('#rotator .content li:first') : currentimage.next()) : $('#rotator .content li:first'));

	//Set the fade in effect for the next image, the active class has higher z-index
	nextimage.css({'opacity' : '0.0'})
	.addClass('active')
	.animate({'opacity' : '1.0'}, speedFading);

	//Hide the current image
	currentimage.animate({'opacity' : '0.0'}, speedFading)
	.removeClass('active');
}

$(document).ready(function() {

	// rotator–navigation wird nur angezeigt, wenn JavaScript aktiviert ist
	$('<div class="nav"><ul></ul></div>').appendTo('#rotator .wrapper');
	for (row = 1; row <= content.length+1; row++) {
		$('<li><a href="#" rel="' + row + '">' + row + '</a></li>').appendTo('#rotator .nav ul');
	}

	for (row = 0; row < content.length; row++) {
		$('<li><img src="' + content[row][0] + '" width="640" height="300" alt="" />' + content[row][1] + '</li>').appendTo('#rotator .content ul');
	}

	$('#rotator .nav li:first, #rotator .content li:first').addClass('active');
	$('#rotator .content ul li p span').css({'opacity' : '0.9'});

	rotator();
});