jQuery(function($) {
	
	/* Gallery */
	if($('.gallery').length > 0) {
		$('.gallery').each(function() {
			$gallery = $(this);
			//ordentliche reihenfolge bilder
			$(this).find('.image').each(function(i) {
				if(i == 0) {
					$(this).addClass('active');
				}
				$(this).css('z-index', 100-i);
			});
			//vor und zurück links
			$(this).next('.pagination').find('div a').click(function() {
				move_gallery($gallery, $(this).parent().is('.left') ? 'left' : 'right');
				return false;
			});
		});
	}	
});

function move_gallery($gallery, direction) {
	$active = $gallery.find('.image.active').removeClass('active');
	switch(direction) {
		case 'left':
			if($active.prevAll().length > 0) {
				$next = $active.prev();
			}
			else {
				$next = $gallery.find('.image:last');
			}
			break;
		case 'right':
			if($active.nextAll().length > 0) {
				$next = $active.next();
			}
			else {
				$next = $gallery.find('.image:first');
			}
			break;
	}
	$next.addClass('active').css('z-index', 100);
	$gallery.find('.image:not(.active)').each(function(i) {
		$(this).css('z-index', 99-i);
	});
	
}
