(function($) {
	$.fn.lrworld_Slideshow = function(settings) {
		this.each(function() {
			//lrworld_slideshow(this, settings);
			$.data(this, 'lrworld_slideshow', new lrworld_slideshow(this, settings));
		});
	};

	/**
	 * PlugIn to disable a timer.
	 * @return {jQuery}
	 */
	$.fn.lrworld_slideshow_disableTimer = function()
	{
		return this.each(function() {
			var slideshowObject = $.data(this, 'lrworld_slideshow');
			if (slideshowObject !== 'undefined')
			{
				slideshowObject.timerEnabled = false;
				slideshowObject.stopTimer();
			}
		});
	};

	var lrworld_slideshow = function($base, localSettings)
	{
		var teaserSlideTimer = false;
		var slides = null;
		var currentSlide = null;
		var isAnimating = null;
		var loopCounter = null;
		var lockSlideshow = null;

		var settings = $.extend({
			timeoutSlideshow: 5000,
			firstSlideIndex: 0,
			loops: 1
		}, localSettings || {})

		currentSlide = settings.firstSlideIndex;
		slides = $($base).children('div.slide');

		if (slides.size() <= 1)
			return false;

		(function init()
		{
			$($base).children('.teaserNav').show();
			slides.not(':first').hide();

			isAnimating = 0;
			lockSlideshow = false;

			indexCurrentSlide = 0;
			totalSlides = slides.size();

			teaserSlideTimer = window.setTimeout(function() {
				doTeaserSlide(0);
			}, settings.timeoutSlideshow);

			$('#teaserNavLeft').click(doBackward);
			$('#teaserNavRight').click(doForward);

			$($base).mouseenter(stopSlideshow);
			$($base).mouseleave(startSlideshow);
		})();

		function doForward(e)
		{
			e.preventDefault();
			if (isAnimating != 1 )
			{
				stopSlideshow();

				if (indexCurrentSlide < totalSlides - 1)
				{
					indexCurrentSlide++
				}
				else
				{
					indexCurrentSlide = 0;
				}
				switchSlide();
			}
		};

		function doBackward(e)
		{
			e.preventDefault();
			if (isAnimating != 1 )
			{
				stopSlideshow();

				if (indexCurrentSlide > 0)
					indexCurrentSlide--;
				else
					indexCurrentSlide = totalSlides -1;

				switchSlide();
			}
		};

		function stopSlideshow(e)
		{
			if (teaserSlideTimer != 'stopped')
			{
				clearTimeout(teaserSlideTimer);
				teaserSlideTimer = 'stopped';
			}
			if(isAnimating == 1)
				lockSlideshow = true;
		};

		function startSlideshow(e)
		{
			if (teaserSlideTimer == 'stopped' && isAnimating == 0)
			{
				clearTimeout(teaserSlideTimer);

				teaserSlideTimer = window.setTimeout(function() {
					doTeaserSlide(indexCurrentSlide);
				}, settings.timeoutSlideshow);

			}
		};

		function switchSlide()
		{
			slides.hide();
			slides.eq(indexCurrentSlide).css('zIndex', 10).css('opacity', 1).show();
		};

		function doTeaserSlide(currentSlide)
		{
			if (lockSlideshow)
			{
				lockSlideshow = false;
				return;
			}
			isAnimating = 1;
			lastCurrentSlide = indexCurrentSlide;
			if (indexCurrentSlide < totalSlides - 1)
				indexCurrentSlide++;
			else
				indexCurrentSlide = 0;

			slides.eq(indexCurrentSlide).show().css('zIndex', 10).css('opacity', 1);

			slides.eq(currentSlide).css('zIndex', 100).animate({
				opacity : 0
			}, {
				duration: 1000,
				complete: function() {
					isAnimating = 0;
					slides.eq(lastCurrentSlide).hide();
					teaserSlideTimer = window.setTimeout(
						$.proxy(function() {
							if (settings.loops > 0)
							{
								if (currentSlide == (totalSlides - 1))
									loopCounter++;
							}
							doTeaserSlide(indexCurrentSlide);
						}, $($base)),
						settings.timeoutSlideshow
					);
				}
			});
		};
	};
})(jQuery);
