jQuery.fn.preloadImages = function(callback) {
	var len = this.length;
	var cache = {total: len, loaded: 0, errors: 0, images: [] };

	for(var i=0 ; i<len ; ++i) {
		var img = new Image();
		img.onload = function() {
			if (cache.total == ++cache.loaded) {
				callback(cache);
			}
		}
		img.onerror = function() {
			++cache.errors;
			if (cache.total == ++cache.loaded) {
				callback(cache);
			}
		}
		img.src = this[i].src;
		cache.images[i] = img;
	}
}

jQuery.fn.makeScrollable = function(opts) {
	var root = this;
	var main = $('._main_', root);
	var box = $('._cnt_', main);

	function newpos(pos) {
		main.data("currpos", pos);
		$.cookie('prlistpos', opts.category+":"+pos, { path: '/' });
	}

	function left() {
		var pos = main.data("currpos");
		var list = main.data("positions");

		if (list[pos] > mainWidthH) {
			--pos;

			var pixel = list[pos] - main.scrollLeft();
			main.animate({ "scrollLeft":list[pos]}, {
				duration: 2 * Math.abs(pixel * 2),
				queue: false
			});

			newpos(pos);
		}

		updateButtons();
	}

	function right() {
		var pos = main.data("currpos");
		var list = main.data("positions");
		var boxWidth = main.data("boxwidth");

		if (list[pos] + mainWidth/2 < sumWidth) {
			++pos;

			var pixel = list[pos] - main.scrollLeft();
			main.animate({"scrollLeft":list[pos]}, {
				duration: 2 * Math.abs(pixel * 2),
				queue: false
			});

			newpos(pos);
		}

		updateButtons();
	}

	function updateButtons() {
		var pos = main.data("currpos");
		var list = main.data("positions");
		var boxWidth = main.data("boxwidth");

		if (list[pos] > mainWidthH) {
			$('#scr-l').removeClass('inact');
		} else {
			$('#scr-l').addClass('inact');
		}
		if (list[pos] + mainWidth/2 < sumWidth) {
			$('#scr-r').removeClass('inact');
		} else {
			$('#scr-r').addClass('inact');
		}
	}

	var rootWidth = root.width();
	var mainWidth = main.width();
	var mainWidthH = mainWidth / 2;

	var sumWidth = 0;
	var maxHeight = 0;
	var initPos = 2;
	var positions = [];
	$('._item_', this).each(function(i,e) {
		e = $(e);
		positions[i] = sumWidth + e.width()/2;
		sumWidth += e.width();
		maxHeight = Math.max(maxHeight, e.height());
	});
	main.data("vpwidth", rootWidth);
	main.data("boxwidth", sumWidth + mainWidth);
	main.data("positions", positions);

	// Abstandshalter einbauen, so dass die Elemente zentriert sind
	$("<div>").css({'float':'left', 'width':mainWidth/2, 'height':maxHeight}).prependTo(box);
	$("<div>").css({'float':'left', 'width':mainWidth/2, 'height':maxHeight}).appendTo(box);

	// Default-Startposition abhängig von Anzahl der Elemente
	if (positions.length < 4) {
		initPos = 1;
	}

	// Nachsehen, ob eine Position per Cookie gesetzt werden muss
	var cookie = $.cookie('prlistpos');
	if (cookie) {
		var tmp = cookie.split(':');
		if (tmp[0] == opts.category) {
			initPos = tmp[1];
		} else {
			newpos(initPos);
		}
	}

	// Buttons verlinken (einfacher Klick)
	/*
	$('#scr-l').click(function(){ left(); });
	$('#scr-r').click(function(){ right(); });
	*/

	// Buttons verlinken (bei gedrückter Maustaste)
	$('#scr-l').mousedown(function(){ left(); $('#scr-r').stopTime(); $('#scr-l').everyTime(500, left); });
	$('#scr-l').mouseup(function(){ $('#scr-l').stopTime(); });
	$('#scr-l').mouseleave(function(){ $('#scr-l').stopTime(); });
	$('#scr-r').mousedown(function(){ right(); $('#scr-l').stopTime(); $('#scr-r').everyTime(500, right); });
	$('#scr-r').mouseup(function(){ $('#scr-r').stopTime(); });
	$('#scr-r').mouseleave(function(){ $('#scr-r').stopTime(); });

	// Größe der Box an die Summe aller Content-Elemente anpassen
	box.height(maxHeight);
	box.width(sumWidth + mainWidth);

	// An korrekte Position springen
	main.data("currpos", initPos);
	main.scrollLeft(positions[initPos]);

	updateButtons();
}

