/**
<style>
 .wrapper {
	overflow: hidden;
	position: relative;
	width: 600px;
	height: 100px;
	}
 .wrapper table {
	position: absolute;
	}
.prev, .next {
	outline: none;
	}
</style>
<script>
new $.slider({
	selectors: {
		container: '#slider .wrapper table',
		prevLink: '#slider .prev',
		nextLink: '#slider .next',
		blocks: 'td'
	},
	//loop: false,
	//vertical: true,
	//step: 2
});
</script>
*/
(function($){

	$.slider = function(options) {
		this.options = $.extend(true, {}, $.slider.defaults, options);
		$($.proxy(this.realInit, this));
	};
	
	$.slider.defaults = {
		selectors: {
			container: '',
			prevLink: '',
			nextLink: '',
			blocks: '> div'
		},
		loop: true,
		vertical: false,
		step: 1
	};
	
	$.slider.prototype = {
		realInit: function() {
			this.currentPage = 1;
		
			var sizeType = this.options.vertical ? 'height' : 'width';
			this.container = $(this.options.selectors.container);
			if (!this.container) {
				return false;
			}
			
			var blocks = this.container.find(this.options.selectors.blocks);
			this.blockSize = blocks.eq(0)[{width: 'outerWidth', height: 'outerHeight'}[sizeType]](true);
			this.totalPages = Math.ceil(blocks.length/this.options.step);
			this.container[sizeType](this.blockSize*blocks.length);

			var this_ = this;
			this.prevLink = $(this.options.selectors.prevLink);
			this.prevLink.click(function(){ this_.prev(this); return false; });
			this.nextLink = $(this.options.selectors.nextLink);
			this.nextLink.click(function(){ this_.next(this); return false; });
			
			this.updateLinks();
			
			this.container.data('slider', this); // Сохраняем ссылку на будущее
		},
		
		prev: function(elem) {
			this.changePage(elem, -this.options.step);
		},
		
		next: function(elem) {
			this.changePage(elem, this.options.step);
		},

		rewind: function() {
			this.changePage();
		},
	
		changePage: function(link, direction) {
			if (link && $(link).hasClass('disabled')) {
				return;
			}
            
			var newValue = '0';
			if (direction !== undefined) {
				var shift = this.blockSize;
				if ((this.currentPage == 1 && direction < 0) || (this.currentPage == this.totalPages && direction > 0)) {
					direction = -direction;
					shift *= this.totalPages-1;
					this.currentPage = direction < 0 ? 1 : this.totalPages;
				}
				else {
					this.currentPage += direction < 0 ? -1 : 1;
				}
				newValue = (direction < 0 ? '+' : '-') + '=' + shift*Math.abs(direction) + 'px';
			}
			else {
				this.currentPage = 1;
			}

			var params = {};
			params[this.options.vertical ? 'top' : 'left'] = newValue; 
			
			this.container.animate(params);
			this.updateLinks();
		},
		
		updateLinks: function() {
            if (this.options.loop) return;
			this.prevLink.toggleClass('disabled', this.currentPage == 1);
			this.nextLink.toggleClass('disabled', this.currentPage == this.totalPages);
		}
	};
	
})(jQuery);


