;(function ($) {
	$.fn.slideshow = function (options) {
		options = $.extend({}, $.fn.slideshow.defaults, options);

		return this.each(function () {
			var container = $(this);
			var slides = options.slideSelector ? $(options.slideSelector, this) : container.children();

			if (container.css('position') == 'static') {
				container.css('position', 'relative');
			}

			container.css('height', Math.max.apply(null, slides.map(function () { return $(this).outerHeight(); }).toArray()));

			slides.css({
				position: 'absolute', left: 0, top: 0,
				zIndex: function (i) {
					return slides.length - i;
				}
			});

			slides.slice(1).hide();

			var currentSlide = 0, auto = false, paused = false, timer;

			function show(index) {
				index %= slides.length;

				if (index == currentSlide) {
					return;
				}

				var current = slides.eq(currentSlide),
					next = slides.eq(index),
					transition = options.transition
					transitionOut = transition.currentSlide,
					transitionIn = transition.nextSlide;
					
				container.triggerHandler('beforeChange', [index, current, next]);

				currentSlide = index;
				
				current.stop(true, true).addClass('transition-out').css(transitionOut.from || {}).animate(transitionOut.to || {}, options.speed, function () {
					if (typeof transitionOut.onComplete === 'function') { transitionOut.onComplete.call(this); }
					if ($.browser.msie) { this.style.removeAttribute('filter'); }
					current.removeClass('transition-out');
				});
				
				next.stop(true, true).addClass('transition-in').css(transitionIn.from || {}).animate(transitionIn.to || {}, options.speed, function () {
					if (typeof transitionIn.onComplete === 'function') { transitionIn.onComplete.call(this); }
					if ($.browser.msie) { this.style.removeAttribute('filter'); }
					next.removeClass('transition-in');

					container.triggerHandler('afterChange', [index, current, next]);
				});

				if (auto) {
					start();
				}
			}

			function prev() {
				show(currentSlide - 1);
			}

			function next() {
				show(currentSlide + 1);
			}

			function start() {
				stop();
				auto = true;
				timer = setTimeout(next, options.timeout);
			}

			function stop() {
				auto = false;
				clearTimeout(timer);
			}

			container.bind({
				show: function (e, index) {
					show(index);
				},
				prev: prev,
				next: next,
				start: start,
				stop: stop
			});
			
			if (options.pager) {
				var pager = $(options.pager);
				
				for (var i = 0, l = slides.length; i < l; i++) {
					pager.append('<a href="#">' + (i + 1) + '</a>');
				}
				
				pager.delegate('a', 'click', function (e) {
					e.preventDefault();
					this.blur();
					
					var index = $(this).index();
					
					setTimeout(function () {
						show(index);
					}, 0);
				});
				
				container.bind('beforeChange', function (e, index) {
					pager.find('a.active').removeClass('active');
					pager.find('a:eq(' + index + ')').addClass('active');
				}).triggerHandler('beforeChange', 0);
			}

			if (options.pauseOnHover) {
				container.hover(
					function () {
						if (auto) {
							stop();
							paused = true;
						}
					},
					function () {
						if (paused) {
							start();
							paused = false;
						}
					}
				);
			}

			if (options.autoPlay) {
				start();
			}
		});
	};

	$.fn.slideshow.defaults = {
		pager: null,
		speed: 750,
		slideSelector: null,
		timeout: 4000,
		pauseOnHover: false,
		autoPlay: false,
		
		transition: {
			currentSlide: {
				from: { zIndex: 1, opacity: 1 },
				to: { opacity: 0 },
				onComplete: function () {
					$(this).css({ display: 'none', opacity: '' });
				}
			},
			nextSlide: {
				from: { display: 'block', zIndex: 0 }
			}
		}
	};

})(jQuery);

