/**
 * @requires jQuery
 * @requires jTweener
 */

function productsFader(rootEl) {
	rootEl = $(rootEl);

	var HALF_TIME = 0.5;

	var
		_changeLinkEl = rootEl.find('h2 .pseudo'),
		_allLinkEl = rootEl.find('.show .all'),
		_shortLinkEl = rootEl.find('.show .short'),
		_containerEl = rootEl.find('.section_container'),
		_sectionsEls = _containerEl.find('.section'),
		_faderEl = rootEl.find('.section_fader'),
		_selected = 0,
		_tweenNs = 'productsFader' + Math.random(),
		_isAnimate = false;

	if (1 >= _sectionsEls.length) {
		_changeLinkEl.remove();

		return;
	}

	_changeLinkEl.one('click', onChange);
	_shortLinkEl.click(onShort);
	_allLinkEl.click(onAll);

	function onChange() {
		if (_isAnimate) {
			return false;
		}

		var
			newSelected = _selected + 1 < _sectionsEls.length ? _selected + 1 : 0,
			curHeight = _sectionsEls.eq(_selected).height(),
			newHeight = _sectionsEls.eq(newSelected).height();

		_containerEl.css({
			height: curHeight,
			overflow: 'hidden'
		});
		_faderEl.css({
			height: curHeight > newHeight ? curHeight : newHeight,
			opacity: 0
		});

		jTweener.addNSAction({
			onComplete: function() {
				_selected = newSelected;
				_changeLinkEl.one('click', onChange);
				jTweener.removeNSActions(_tweenNs);
				updateIe6()
			}
		}, _tweenNs);

		$t(_faderEl, {
			opacity: 1,
			time: HALF_TIME,
			transition: 'easeNone',
			onComplete: function() {
				_sectionsEls.eq(_selected).removeClass('selected_section');
				_sectionsEls.eq(newSelected).addClass('selected_section');

				$t(_faderEl, {
					opacity: 0,
					time: HALF_TIME,
					transition: 'easeNone',
					'namespace': _tweenNs,
					onComplete: function() {
						_faderEl.css('height', 0);
					}
				}).tween();
			}
		}).tween();

		$t(_containerEl, {
			height: newHeight,
			time: 2 * HALF_TIME,
			'namespace': _tweenNs,
			onComplete: function() {
				_containerEl.css({
					height: ''
				});
			}
		}).tween();

		return false;
	}

	function onShort() {
		if (_isAnimate) {
			return false;
		}

		_isAnimate = true;

		_changeLinkEl.css('visibility', '');
		_allLinkEl.removeClass('hidden');
		_shortLinkEl.addClass('hidden');

		var
			els = _sectionsEls.not('.selected_section'),
			count = els.length;

		els.slideUp(HALF_TIME * 1000, function() {
			if (!--count) {
				_sectionsEls.not('.selected_section').css({
					display: '',
					left: '',
					position: '',
					top: '',
					visibility: ''
				});
				updateIe6()
				_isAnimate = false;
			}
		});

		return false;
	}

	function onAll() {
		if (_isAnimate) {
			return false;
		}

		_isAnimate = true;

		_changeLinkEl.css('visibility', 'hidden');
		_allLinkEl.addClass('hidden');
		_shortLinkEl.removeClass('hidden');

		var
			els = _sectionsEls.not('.selected_section'),
			count = els.length;

		els
			.css({
				display: 'none',
				left: 0,
				position: 'relative',
				top: 0,
				visibility: 'visible'
			})
			.slideDown(HALF_TIME * 1000, function() {
				if (!--count) {
					updateIe6()
					_isAnimate = false;
				}
			});

		return false;
	}

	function updateIe6() {
		if ($c.browser.msie && 6 >= parseInt($c.browser.version)) {
			$('#layout').css('margin-top', 1);
			$('#layout').css('margin-top', '');
		}
	}
}

$(function() {
	productsFader('#content .products_list');
});
