/*
 * 	Slideshow - jQuery plugin
 *	written by Alexander All
 *	http://www.webdesign-villingen.de
 *
 *	Copyright (c) 2011 Alexander All (http://www.webdesign-villingen.de)
 *	Built for jQuery library
 *	http://jquery.com
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 */

(function($){
	$.fn.slideshow = function(options){
		var config = {
			prev:			'.klickprev', 
			next:			'.klicknext',
			current:		'.current',
			total:			'.total',
			browseSpeed:	'slow'	// slow, fast oder in ms z.b 200
		};
		config = $.extend(config, options);
		return this.each(function(){
			
			var images = $.makeArray($("img", $(this)));
			var aktImg = $(images[0]);
			var current = 1;
			
			var isInteger = function(s){
				return (s%(parseInt(s)/Number(s)))===0;
			};

			$(config.next).click(function(){
				aktImg.fadeOut(config.browseSpeed);
				if(aktImg.next().length) {
					aktImg = aktImg.next();
					current++;
				} else {
					aktImg = aktImg.parent().children(':first-child');
					current = 1;
				}
				$(config.current).html(current);
				aktImg.fadeIn(config.browseSpeed);
			});

			$(config.prev).click(function(){
				aktImg.fadeOut(config.browseSpeed);
				if(!isInteger(aktImg.prev().length)) {
					aktImg =  $(images[images.length-1]);
					current = images.length;
				} else {
					aktImg = aktImg.prev();
					current--;
				}
				$(config.current).html(current);
				aktImg.fadeIn(config.browseSpeed);
			});			

			$(config.current).html(current);
			$(config.total).html(images.length);			
			
		});
	};
})(jQuery);


