$(function() {
	carousel();
	stylize();
	selectColors();
	primaryNav();
	gallery();
	catInset();
});

function carousel() {
	if ($('.carousel .carousel-scroller').length) {
		var paused = false;

		$('.carousel .carousel-scroller').jCarouselLite({
			btnNext: '.carousel .i-carousel-next',
			btnPrev: '.carousel .i-carousel-prev',
			mouseWheel: false,
			visible: 1,
			speed: 1540,
			circular: true,
			auto: 70,
			interval: 0,
			pause: true,
			btnGo: ['.carousel-control .1', '.carousel-control .2', '.carousel-control .3']
		});

		$('.carousel .player').click(function() {
			if (!paused) {
				$('.carousel .carousel-scroller').trigger('pauseCarousel');
				paused = true;
				$(this).addClass('play');
			} else {
				$('.carousel .carousel-scroller').trigger('resumeCarousel');
				paused = false;
				$(this).removeClass('play');
			}
		})
	}
}

function stylize() {
	$('.category-nav1 td:odd').addClass('alt');
	$('.search-results tr:even').addClass('alt');
}

function selectColors() {
	var big = $('.select-colors-inset dt img');
	var previews = $('.select-colors-inset a');

	previews.click(function() {
		big.hide(0);
		big.attr('src', $(this).attr('href')).load(function() {
			$(this).fadeIn(350)
		});
		previews.removeClass('active');
		$(this).addClass('active');
		return false;
	});
}

function primaryNav() {
	var nav = $('.primary-nav');
	var delayHide = 300; //Задержка скрытия подменю (мс)
	var delayShow = 0;
	var delayShowMenu = 200; //Фильтрация быстрого наведения на главное меню (мс)
	var timerHide = 0;
	var timerShow = 0;
	var container = $('.container').offset().left + $('.container').outerWidth();

	nav.find('> li').each(function() {
		var item = $(this);

		if (item.find('.submenu').length)
			item.addClass('has-submenu');

		$(this).hover(
			function() {
				stopSuppress();
				if ($('.submenu').is(':visible')) delayShow = delayShowMenu;
				timerShow = setTimeout(function() {
					nav.find('.submenu').hide(0);
					item.find('.submenu').find('tr:last').addClass('last').end().show(0).css({
						'marginLeft': function() {
							if (($(this).offset().left + $(this).outerWidth()) > (container)) {
								return container - ($(this).offset().left + $(this).outerWidth()) - 20
							}
						}
					});
				}, delayShow);
			},
			function() {
				stopShow();
				timerHide = setTimeout(function() {
					item.find('.submenu').hide(0).removeAttr('style');
				}, delayHide);
				delayShow = 0;
			}
		);
	});

	nav.find('.submenu').hover(
		function() {stopSuppress();$(this).closest('li').find('> a').addClass('hover')},
		function() {$(this).closest('li').find('> a').removeClass('hover')}
	);

	function stopSuppress() {
		if (timerHide)
			clearTimeout(timerHide);
			timerHide = 0;
	}

	function stopShow() {
		if (timerShow)
			clearTimeout(timerShow);
			timerShow = 0;
	}
}

function gallery() {
	if ($("a[rel^='prettyPhoto']").length) {
		$("a[rel^='prettyPhoto']").prettyPhoto({
			theme: 'light_square',
			overlay_gallery: false
		})
	}
}

function catInset() {
	$('.categories-inset > a').not('.more').not('.categories-inset1 > a').mouseover(
		function() {
			if ($(this).next().is('div.hover')) {
				$(this).next().show(0).css({
					'top': $(this).position().top,
					'left': $(this).position().left
				})
			}
		}
	);
	$('.categories-inset .hover').hover(
		function() {
			$(this).show(0)
		},
		function() {
			$(this).hide(0).removeAttr('style');
		}
	)
}
