(function($) {
	
	$.fn.extend({
		
		thumbCarousel: function(options) {
			
			var defaults = {
				item_width: 55,
				place_width: 220,
				get_thumb_filename: function(sName) {
					var sExtension = sName.substr(sName.lastIndexOf('.'));
					return sName.substr(0, sName.lastIndexOf('.')) + '-thumb' + sExtension;
				},
				get_medium_filename: function(sName) {
					var sExtension = sName.substr(sName.lastIndexOf('.'));
					return sName.substr(0, sName.lastIndexOf('.')) + '-medium' + sExtension;
				},
				get_big_filename: function(sName) {
					return sName;
				},
				lightbox_options: {
					imageLoading: '/_images/lightbox/lightbox-ico-loading.gif',
					imageBtnClose: '/_images/lightbox/lightbox-btn-close.gif',
					imageBtnPrev: '/_images/lightbox/lightbox-btn-prev.gif',
					imageBtnNext: '/_images/lightbox/lightbox-btn-next.gif',
					imageBlank: '/_images/lightbox/lightbox-blank.gif',
					txtImage: 'Zdjęcie',
					txtOf: 'z'
				}
			};
			
			options = $.extend(defaults, options);
			
			return this.each(function() {
				
				this.m_oOptions = options;
				this.m_iX = 0;
				this.m_jUl = $(this).find('ul');
				this.m_jContainer = $('<div/>');
				this.m_jMask = $(this).find('.mask');
				this.m_jIndicator = $('<div class="indicator"/>');
				this.m_iItems = $(this).find('li').length;
				this.m_iCurrent = 0;
				this.m_iCurrentScroll = 0;
				this.m_jMainPicture = $(this).find('.main-picture');
				
				var dCarousel = this;
				
				this.ScrollLeft = function() {
					var dCarousel = $(this).closest('.thumbCarousel').get(0);
					var iDesiredX = Math.min(0, dCarousel.m_iX + dCarousel.m_oOptions.item_width);
					dCarousel.m_iX = iDesiredX;
					dCarousel.m_jUl.animate({
						x: iDesiredX
					}, 150);
					return false;
				};
				
				this.ScrollRight = function() {
					var dCarousel = $(this).closest('.thumbCarousel').get(0);
					var iDesiredX = Math.max(- $(dCarousel).width() + dCarousel.m_iItems * dCarousel.m_oOptions.item_width, dCarousel.m_iX - dCarousel.m_oOptions.item_width);
					dCarousel.m_iX = iDesiredX;
					dCarousel.m_jUl.animate({
						x: iDesiredX
					}, 150);
					return false;
				};
				
				this.GoToNext = function() {
					var iDesired = Math.min(dCarousel.m_iItems - 1, dCarousel.m_iCurrent + 1);
					if (iDesired != dCarousel.m_iCurrent) {
						dCarousel.GoTo(iDesired);
					}
				};
				
				this.GoToPrevious = function() {
					var iDesired = Math.max(0, dCarousel.m_iCurrent - 1);
					if (iDesired != dCarousel.m_iCurrent) {
						dCarousel.GoTo(iDesired);
					}
				};
				
				this.GoTo = function(i) {
					var iPlace = dCarousel.m_oOptions.place_width / dCarousel.m_oOptions.item_width;
					var iScroll = dCarousel.m_iCurrentScroll;
					var iDirection = -1;
					if (i >= dCarousel.m_iCurrentScroll + iPlace) {
						iScroll = i - iPlace + 1;
					}
					else if (i < dCarousel.m_iCurrentScroll) {
						iScroll = i;
					}
					dCarousel.m_iCurrentScroll = iScroll;
					dCarousel.m_jContainer.stop(true, false).animate({
						left: - iScroll * dCarousel.m_oOptions.item_width
					}, 150, 'easeOutQuint');
					dCarousel.m_iCurrent = i;
					dCarousel.m_jIndicator.stop(true, false).animate({
						left: i * dCarousel.m_oOptions.item_width
					}, 150, 'easeOutQuint');
					$(dCarousel).find('.next a, .previous a').css({
						opacity: 1,
						cursor: 'pointer'
					});
					if (dCarousel.m_iCurrent == 0) {
						$(dCarousel).find('.previous a').css({
							opacity: .5,
							cursor: 'default'
						});
					}
					if (dCarousel.m_iCurrent == dCarousel.m_iItems - 1) {
						$(dCarousel).find('.next a').css({
							opacity: .5,
							cursor: 'default'
						});
					}
					var sFilename = dCarousel.m_jUl.find('li:eq(' + i + ') a').attr('href');
					var sMediumFilename = dCarousel.m_oOptions.get_medium_filename(sFilename);
					var sBigFilename = dCarousel.m_oOptions.get_big_filename(sFilename);
					var sAlt = dCarousel.m_jUl.find('li:eq(' + i + ') img').attr('alt');
					dCarousel.m_jMainPicture.find('img').stop(true, false).animate({
						opacity: 0
					}, 50, function() {
						$(this).closest('a').attr('href', sBigFilename);
						var jOldImage = $(this);
						var jImage = $('<img/>');
						$(this).after(jImage.css({
							opacity: 0,
							position: 'absolute',
							left: -5000,
							top: -5000
						}));
						var fShow = function() {
							jOldImage.remove();
							jImage.css('position', 'static').animate({
								opacity: 1
							}, 150);
						};
						jImage.load(fShow);
						jImage.attr('src', sMediumFilename).attr('alt', sAlt);
					});
				};
				
				$(this).addClass('thumbCarousel');
				this.m_jMask.append(this.m_jContainer);
				this.m_jContainer.append(this.m_jUl).css({
					position: 'absolute'
				});
				this.m_jContainer.append(this.m_jIndicator);
				$(this).find('li').each(function(i) {
					$(this).css({
						position: 'absolute',
						top: 0,
						left: i * dCarousel.m_oOptions.item_width
					});
				});
				$(this).find('.next a').click(function() {
					dCarousel.GoToNext();
					return false;
				});
				$(this).find('.previous a').click(function() {
					dCarousel.GoToPrevious();
					return false;
				});
				$(this).find('li a').each(function(i) {
					$(this).bind('click', {
						i: i
					}, function(e) {
						dCarousel.GoTo(e.data.i);
						return false;
					});
				});
				this.m_jMainPicture.find('a').lightBox(this.m_oOptions.lightbox_options);
				this.GoTo(0);
				
			});
			
		}
		
	});
	
})(jQuery);