$(document).ready(function() {
jQuery.fn.movieBox = function() {
	movies = this;

	var movieCount = movies.length;
	var currentMovie = 0, oldMovie = 0;
	var animationTimeout = 3000;
	var stichwort;

	// Anfangszustand
	
	if ($('#edit-stichwort-wrapper').val() == '') {
//	$('#edit-stichwort-wrapper').val(stichwort).css('color','#666');
	}
	
	
	movies.children('img').each(function(index) {
		
		$(this).css('z-index', 20-index);

		if (index > 0) {
			$(this).hide();
		}
		else {
			// linkhilite Setzen
//			$(this).css('z-index','8');
			movies.eq(index).children('a').attr('class','hover');
		}
	})
	
	var pause;
	
	var movieboxAnimate = function() {
		currentMovie = (oldMovie + 1) % movieCount;
		changeStates(oldMovie,currentMovie);
		pause = setTimeout(movieboxAnimate, animationTimeout);
	}
	
	var changeStates = function (oldM,currentM){
		// Animation
		// altes Bild z-index = 8
		movies.eq(oldM).children('img').stop(true,'jumpToEnd').css('z-index', '25').show();
		movies.eq(currentM).children('img').hide().css('z-index','30');

		movies.eq(oldM).children('a').attr('class','none');
		movies.eq(currentM).children('a').attr('class','hover');

		oldMovie = currentM;
		// neues Bild z-index = 8
//		console.log(movies.eq(oldM).children('img').css('z-index'));
		movies.eq(currentM).children('img').fadeIn(800, function() {
				movies.eq(oldM).children('img').css('z-index', 20-oldM);
		});

	}
	
	movies.hover(function() {
			clearTimeout(pause);
			changeStates(oldMovie, movies.index(this));
		}, function() {
			pause = setTimeout(movieboxAnimate, animationTimeout);
		});
	
	pause = setTimeout(movieboxAnimate, animationTimeout);
	
}
});
