if (typeof jQuery != "function") {
	alert("The jQuery-Library is not loaded which is requried for the lbr_slidebox-Extension");
} else { // else jQuery <!--
	
	if (typeof $.fn.lbrslidebox != "function") { // is lbrslidebox allready defined? <!--
		(function($) {
			$.fn.extend({	
			
			lbrslidebox: function(options) { // lbrslidebox <!--
					/**
					Default-Werte
					*/
					var defaults = {
						'boxH': 400,
						'boxW': 300,
						'animationTime': 200,
						'rotationTime': 1000,
						'animation': "fade",
						'rotation': 1
					};
					
					var obj, currentObj, nextObj, prevBtnObj, nextBtnObj, playPauseBtnObj;
					var conf =  $.extend(defaults, options);
					var current = 0; // current position in items-array
					var next = 0; // next position in items-array
					var items = new Array();
					var loadedImages = new Array();
					var rotationBreak = false;
					
					var log = function (o) {
						//console.log(o);
					}
					
					var _init = function () {
						// get the items as array
						obj.find("li.tx_lbrslidebox_image div.item").each(function () {
								items.push($(this));
						});
						log(items);
						if (!items.length) return;
						
						// hide the list
						obj.find("ul.tx_lbrslidebox_images").hide();
						
						// create the current- and the next-container
						currentObj = $("<div>");
						obj.append(currentObj);
						
						nextObj = $("<div>");
						obj.append(nextObj);
						
						// set css
						currentObj.css({ "z-index": 1, 'position': 'absolute', 'top': 0, 'left': 0 });
						nextObj.css({ "z-index": 2, 'position': 'absolute', 'top': 0, 'left': 0 });
						
						// load the first image into the next-container (the image will be the one in the current-container on animation)
						nextObj.html(items[current].clone());
						
						rotationBreak = (conf.rotation) ? false : true;
						
						// run the loop
						_start();
					};
					
					var _start = function () {
						if (conf.rotation) setTimeout(function () { _rotate() }, conf.rotationTime);
						_initNavigation();
						_handlePlayPauseClass();
					};
					
					var _initNavigation = function () {
						prevBtnObj = obj.find(".tx_lbrslidebox_previous");
						if (prevBtnObj.length) {
							prevBtnObj.unbind("click").click(function () {
									_prevClicked();
							});
						}
						
						nextBtnObj = obj.find(".tx_lbrslidebox_next");
						if (nextBtnObj.length) {
							nextBtnObj.unbind("click").click(function () {
									_nextClicked();
							});
						}
						
						playPauseBtnObj = obj.find(".tx_lbrslidebox_playPause");
						if (playPauseBtnObj.length) {
							playPauseBtnObj.unbind("click").click(function () {
									_playPauseClicked();
							});
						}
					};
					
					var _unbindNavigation = function () {
						if (prevBtnObj.length) prevBtnObj.unbind("click");
						if (nextBtnObj.length) nextBtnObj.unbind("click");
						if (playPauseBtnObj.length) playPauseBtnObj.unbind("click");
					};
					
					var _prevClicked = function () {
						rotationBreak = true;
						next = (current-1 < 0) ? items.length-1 : current-1;
						_animate();
						_handlePlayPauseClass();
					};
					
					var _nextClicked = function () {
						rotationBreak = true;
						next = (current+1 >= items.length) ? 0 : current+1;
						_animate();
						_handlePlayPauseClass();
					};
					
					var _playPauseClicked = function () {
						if (rotationBreak) {
							rotationBreak = false;
							conf.rotation = 1;
							_rotate();
						} else {
							rotationBreak = true;
						}
						_handlePlayPauseClass();
					};
					
					var _handlePlayPauseClass = function () {
						if (rotationBreak) {
							playPauseBtnObj.removeClass("pause").addClass("play");
						} else {
							playPauseBtnObj.removeClass("play").addClass("pause");
						}
					}
					
					var _rotate = function () {
						if (!rotationBreak) {
							next = (current+1 >= items.length) ? 0 : current+1;
							
							// wait till image is loaded
							loadedImages[next] = new Image();
							loadedImages[next].src = items[next].find("img").attr("src");
							loadedImages[next].onerror = function () { alert("ERROR: Could not load image '"+items[next].find("img").attr("src")+"'. Maybe the image is damaged?"); };
							_imageIsLoaded(loadedImages[next]);
						}
					};
					
					var _imageIsLoaded = function (imageObj) {
						if (!imageObj.complete) {
							setTimeout(function () { _imageIsLoaded(imageObj); }, 100);
							return;
						}
						
						_animate();
					};
					
					
					var _animate = function () {
						_unbindNavigation();
						switch (conf.animation) {
							case "fade":
								_animateFade();
								break;
							case "slideLeftToRight":
								_animateSlideLeftToRight();
								break;
							case "slideRightToLeft":
								_animateSlideRightToLeft();
								break;
							default:
								_animateDefault();
								break;
						}
					};
					
					var _animateDefault = function () {
						currentObj.html(items[current].clone());
						nextObj.html(items[next].clone());
						current = next;
						_start();
					};
					
					var _animateFade = function () {
						currentObj.html(items[current].clone());
						nextObj.css({ 'opacity': 0 }).html(items[next].clone()).animate({ 'opacity': 1 }, conf.animationTime, function () { current = next; _start(); });
					};
					
					var _animateSlideLeftToRight = function () {
						currentObj.html(items[current].clone());
						nextObj.css({ 'left': (conf.width*-1) }).html(items[next].clone()).animate({ 'left': 0 }, conf.animationTime, function () { current = next; _start(); });
					};
					
					var _animateSlideRightToLeft = function () {
						currentObj.html(items[current].clone());
						nextObj.css({ 'left': conf.width }).html(items[next].clone()).animate({ 'left': 0 }, conf.animationTime, function () { current = next; _start(); });
					};
					
					return this.each(function() { // loop <!--
						obj = $(this);
						_init();
					}); // loop -->
					
				} // lbrslidebox -->
				
			});
		})(jQuery);
		
	} // is lbrslidebox allready defined? -->

} // else jQuery -->


