team23.headimg = (function($) {
	var $headimg;
	var $img;
	var $menu;
	var images;
	var count;
	var visible = -1;
	var timeout = null;
	var wait = 6000;
	var fade = 1000;
	
	function show_image(num)
	{
		if (visible == num)
		{
			reset_timeout();
			return;
		}
		$.each(images, function(i, image) {
			if (num == i)
				image.$li.addClass('active');
			else
				image.$li.removeClass('active');
		});
		var new_image = images[num];
		var $new_img = new_image.$box.clone();
		var $old_img = $img;
		$new_img.css({
			position: 'absolute',
			top: '0px',
			left: '0px'
		});
		if (visible != -1)
		{
			var cur_image = images[visible];
			$new_img.hide();
			$old_img.after($new_img);
			$new_img.fadeIn(fade, function() {
				$old_img.remove();
			});
			/*
			$new_img.css({
				top: $old_img.height() + 'px'
			});
			$new_img.show();
			$new_img.animate({
				top: '0px'
			}, fade, 'linear', function() {
				$old_img.remove();
			})
			$old_img.animate({
				top: '-' + $old_img.height() + 'px'
			}, fade, 'linear')
			*/
		}
		else
		{
			$old_img.after($new_img);
			$old_img.remove();
		}
		$img = $new_img;
		reset_timeout();
		visible = num;
	}
	
	function reset_timeout()
	{
		if (timeout)
			window.clearTimeout(timeout);
		timeout = window.setTimeout(next_image, wait);
	}
	
	function next_image()
	{
		var next = visible + 1;
		if (next >= count)
			next = 0;
		show_image(next);
	}
	
	function init()
	{
		$headimg = $('#portauswahl');
		$img = $headimg.find('> :first-child');
		$menu = $('#portauswahlmenu');
		images = {};
		count = 0;
		$menu.find('li').each(function(i, li) {
			var $li = $(li);
			var $box = $li.find('> *');
			images[i] = {
				$li: $li,
				$box: $box
			};
			$li.bind('click', function (e) {
				e.preventDefault();
				show_image(i);
			});
			count++;
		});
		if (count > 0)
		{
			show_image(0);
		}
	}
	
	return {
		init: init
	}
})(jQuery);

jQuery(document).ready(function () {
	team23.headimg.init();
});

