(function($) {
	$.fn.slideshow = function(options) {
		var options = $.extend({
			timeout: 5000,
			speed: 'slow',
			slide: '.slide',
			controls: '.controls',
			index: '.current',
			prev: '.prev',
			next: '.next',
			play: '.play',
			pause: '.pause'
		}, options || {});
		
		var current_index = this.find(options.index);
		var slides = this.find(options.slide);
		var count = slides.length;
		var pos = 0;
		var prev, current = $(slides.get(pos));
		
		var paused = false;
		var timer = setTimeout(nextSlide, options.timeout);
		
		// Появление/скрытие панели
		var controls = this.find(options.controls);
		this.mouseenter(function() {
			controls.fadeIn('fast');
		});
		this.mouseleave(function() {
			controls.fadeOut('fast');
		});
		
		// Кнопки
		var play = this.find(options.play);
		var pause = this.find(options.pause);
		play.hide().click(function() {
			var timer = setTimeout(nextSlide, options.timeout);
			paused = false;
			pause.show();
			play.hide();
		});
		pause.click(function() {
			paused = true;
			clearTimeout(timer);
			play.show();
			pause.hide();
		});
		this.find(options.prev).click(function() {
			if (!paused) pause.click();
			--pos;
			if (0 > pos) pos = count - 1;
			showSlide();
		});
		this.find(options.next).click(function() {
			if (!paused) pause.click();
			++pos;
			if (count <= pos) pos = 0;
			showSlide();
		});
		
		// Для таймера
		function nextSlide() {
			++pos;
			if (count <= pos) pos = 0;
			showSlide();
			if (!paused) timer = setTimeout(nextSlide, options.timeout);
		}
		
		function showSlide() {
			prev = current;
			current = $(slides.get(pos));
			
			current_index.removeClass("current");
			current_index.eq(pos).addClass("current");
						
			prev.css('z-index', 0).hide();
			current.css('z-index', 1).show();
		}
	};
})(jQuery);
