(function($) {
	
	$.fn.extend({
		
		newsCarousel: function(options) {
			
			var defaults = {
				item_width: 55,
				place_width: 220,
				auto_rotate_delay: 0,
				get_thumb_filename: function(sName) {
					var sExtension = sName.substr(sName.lastIndexOf('.'));
					return sName.substr(0, sName.lastIndexOf('.')) + '-thumb' + sExtension;
				},
				get_big_filename: function(sName) {
					return sName;
				}
			};
			
			options = $.extend(defaults, options);
			
			return this.each(function() {
				
				this._PrepareThumbs = function() {
					var jThumbs = $('<div class="thumbs"/>');
					jThumbs.append('<p class="previous"><a href="#"><img src="/_images/icons/arrow-orange-left.png" alt="Poprzedni" title="Poprzedni news"/></a></p>');
					jThumbs.append('<p class="next"><a href="#"><img src="/_images/icons/arrow-orange.png" alt="Następny" title="Następny news"/></a></p>');
					this.m_jUl = $('<ul/>');
					var jLis = $(this).find('ul li');
					for (var i = 0; i < jLis.length; i++) {
						var aBg = jLis.eq(i).attr('style').match(/background-image:\s*url\(['"]?([^'")]+)['"]?\)\;?/i, '');
						var sBg = aBg[1];
						sBg = this.m_oOptions.get_thumb_filename(sBg);
						this.m_jUl.append('<li><a href="#"><img src="' + sBg + '" alt=""/></a></li>');
					}
					jThumbs.append($('<div class="mask"/>').append(this.m_jUl));
					$(this).append(jThumbs);
				};
				
				this.m_oOptions = options;
				this.m_jMainUl = $(this).find('ul:first');
				this.m_jUl;
				this._PrepareThumbs();
				
				this.m_jLis = this.m_jMainUl.find('li').remove();
				this.m_jMainUl.append(this.m_jLis.eq(0));
				
				
				this.m_iX = 0;
				this.m_jContainer = $('<div/>');
				this.m_jMask = $(this).find('.mask');
				this.m_jIndicator = $('<div class="indicator"/>');
				this.m_iItems = this.m_jLis.length;
				this.m_iCurrent = 0;
				this.m_iCurrentScroll = 0;
				//this.m_jMainPicture = $(this).find('ul:first');
				
				var dCarousel = this;
				
				this.ScrollLeft = function() {
					var dCarousel = $(this).closest('.newsCarousel').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('.newsCarousel').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.m_iTimeout;
				
				this.AutoRotate = function() {
					var gThis = this;
					window.clearTimeout(this.m_iTimeout);
					if (this.m_oOptions.auto_rotation_delay > 0) {
						this.m_iTimeout = window.setTimeout(function() {
							if (gThis.m_iCurrent >= gThis.m_iItems - 1) {
								gThis.GoTo(0);
							}
							else {
								gThis.GoToNext();
							}
							gThis.AutoRotate();
						}, this.m_oOptions.auto_rotation_delay);
					}
				};
				
				this.AutoRotate();
				
				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 + 3
					}, 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'
						});
					}
					dCarousel.m_jMainUl.animate({
						opacity: 0
					}, 100, function() {
						$(this).empty().append(dCarousel.m_jLis.eq(i)).animate({
							opacity: 1
						}, 100);
					});
				};
				
				$(this).addClass('newsCarousel');
				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).mouseover(function(eEvent) {
					window.clearTimeout(this.m_iTimeout);
				}).mouseout(function(eEvent) {
					this.AutoRotate();
				});
				$(this).find('.thumbs 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('.thumbs 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);