function gallery(rootEl) {
	rootEl = $(rootEl).first();

	var
		VISIBLE_COUNT = 3,
		STEP = 15,
		MAX_HIDE_WIDTH = 750;

	var
		_picturesEl = $('<div class="pictures" />').prependTo(rootEl),
		_curEl = rootEl.find('.links .current'),
		_count = rootEl.find('img').length,
		_curIndex = _count - 1,
		_maxImgWidth = _picturesEl.width(),
		_isAnimate = false,
		_queue = 0,
		_visibleCount = VISIBLE_COUNT;

	init();
	rootEl.find('.links .pseudo').eq(0).click(prev);
	rootEl.find('.links .pseudo').eq(1).click(next);
	$('img', _picturesEl[0]).live('click', next);

	function init() {
		if (_count < _visibleCount) {
			_visibleCount = _count;
		}

		rootEl.height(rootEl.height() + STEP * (_visibleCount - 1));
		rootEl.find('img')
			.appendTo(_picturesEl)
			.each(function(index) {
				if (index < _count - _visibleCount) {
					$(this).css('display', 'none');
				} else {
					var move = STEP * (_count - 1 - index);

					$(this).css({
						display: 'block',
						left: move,
						top: (_visibleCount - 1) * STEP - move,
						width: _maxImgWidth - 2 * move
					});
				}
			});
	}

	function next() {
		if (_isAnimate) {
			_queue = 1;

			return false;
		}

		_isAnimate = true;

		// аЁаОаЗаДаАаЕаМ аКаЛаОаН аИаЗаОаБб�аАаЖаЕаНаИб� аИ аАаНаИаМаИб�б�аЕаМ аПб�аОаЗб�аАб�аНаОб�б�б� аИ аМаАб�б�б�аАаБ.
		var
			curImgEl = rootEl.find('img:last'),
			winHeight = $(window).height(),
			offset = curImgEl.offset(),
			scrollTop = $(window).scrollTop(),
			wrapEl = $('<div />').appendTo('body');

		wrapEl.css({
			height: winHeight - Math.round(offset.top) + scrollTop,
			left: Math.round(offset.left),
			overflow: 'hidden',
			position: 'absolute',
			top: Math.round(offset.top),
			zIndex: 1000,
			width: _maxImgWidth
		});
		curImgEl.clone()
			.appendTo(wrapEl)
			.css('width', '100%');
		curImgEl
			.css('display', 'none')
			.prependTo(_picturesEl);

		// а�аОаКаАаЗб�аВаАаЕаМ аИаЗаОаБб�аАаЖаЕаНаИаЕ б�аВаЕб�б�б�.
		var
			showImgEl = rootEl.find('img:hidden:last'),
			move = STEP * (_visibleCount - 1);

		showImgEl.css({
			display: 'block',
			left: move,
			top: 0,
			width: _maxImgWidth - 2 * move
		});

		var animateEls = showImgEl.nextAll('img');
		var complete = function() {
			wrapEl.remove();
			_isAnimate = false;

			if (0 < _queue) {
				next();
			} else if (0 > _queue) {
				prev();
			}

			_queue = 0;
		};
		var completeCount = animateEls.length + 1;

		wrapEl.animate({
			height: 0,
			left: Math.round(offset.left - MAX_HIDE_WIDTH / 2 + _maxImgWidth / 2),
			top: scrollTop + winHeight,
			opacity: 0,
			width: MAX_HIDE_WIDTH
		}, {
			duration: 500,
			complete: function() {
				if (!--completeCount) {
					complete();
				}
			}
		});

		// а�аНаИаМаИб�б�аЕаМ аИаЗаОаБб�аАаЖаЕаНаИб� аВ б�б�аОаПаКаЕ.
		animateEls.each(function(index) {
			var move = STEP * (_visibleCount - index - 2);

			$(this).animate({
				left: move,
				top: (_visibleCount - 1) * STEP - move,
				width: _maxImgWidth - 2 * move
			}, {
				duration: 500,
				complete: function() {
					if (!--completeCount) {
						complete();
					}
				}
			});
		});

		rootEl.find('.item').eq(_curIndex).removeClass('selected');
		_curIndex = (_curIndex - 1 + _count) % _count;
		rootEl.find('.item').eq(_curIndex).addClass('selected');
		_curEl.text(_count - _curIndex);

		return false;
	}

	function prev() {
		if (_isAnimate) {
			_queue = -1;

			return false;
		}

		_isAnimate = true;

		var
			curImgEl = rootEl.find('img:first'),
			winHeight = $(window).height(),
			offset = rootEl.find('img:last').offset(),
			scrollTop = $(window).scrollTop(),
			wrapEl = $('<div />').appendTo('body');

		wrapEl.css({
			height: 0,
			left: Math.round(offset.left - MAX_HIDE_WIDTH / 2 + _maxImgWidth / 2),
			opacity: 0,
			overflow: 'hidden',
			position: 'absolute',
			top: scrollTop + winHeight,
			zIndex: 1000,
			width: MAX_HIDE_WIDTH
		});
		rootEl.find('img:first').clone()
			.appendTo(wrapEl)
			.css({
				display: 'block',
				width: '100%'
			});

		var animateImgEls = rootEl.find('img:visible:first').nextAll('img');
		var complete = function() {
			rootEl.find('img:hidden:last').next().css('display', 'none');

			rootEl.find('img:first')
				.appendTo(_picturesEl)
				.css({
					display: 'block',
					left: 0,
					top: STEP * (_visibleCount - 1),
					width: _maxImgWidth
				});

			wrapEl.remove();
			_isAnimate = false;

			if (0 < _queue) {
				next();
			} else if (0 > _queue) {
				prev();
			}

			_queue = 0;
		};
		var completeCount = animateImgEls.length + 1;

		wrapEl.animate({
			height: winHeight - Math.round(offset.top) + scrollTop,
			left: Math.round(offset.left),
			top: Math.round(offset.top),
			opacity: 1,
			width: _maxImgWidth
		}, {
			duration: 500,
			complete: function() {
				if (!--completeCount) {
					complete();
				}
			}
		});

		animateImgEls.each(function(index) {
			var move = STEP * (_visibleCount - index - 1);

			$(this).animate({
				left: move,
				top: (_visibleCount - 1) * STEP - move,
				width: _maxImgWidth - 2 * move
			}, {
				duration: 500,
				complete: function() {
					if (!--completeCount) {
						complete();
					}
				}
			});
		});

		rootEl.find('.item').eq(_curIndex).removeClass('selected');
		_curIndex = (_curIndex + 1) % _count;
		rootEl.find('.item').eq(_curIndex).addClass('selected');
		_curEl.text(_count - _curIndex);

		return false;
	}
}

$(function() {
	$('#content .gallery').each(function() {
		gallery(this);
	});
});
