
$(document).ready(function()
{
	
	$('.loading').css({opacity: 0.85});

	$('.preview-small').find('.listalka').each(function(){
		$(this).get(0).i = 0;
		$(this).get(0).cur = 0;
		$(this).get(0).quantity = $(this).find('li').length;
		$(this).get(0).w = $($(this).find('li').get(0)).width()+ parseInt($(this).find('li').css('margin-left'))+ parseInt($(this).find('li').css('padding-right'));
		$(this).get(0).animTime = 400;
	})
	
	// галерея, смена влево
	$('.eg-block .left-arrow').click(function(){
		var jul = $(this).parent().find('.listalka');
		var ul = jul.get(0);
		
		if (ul.i == 0)
		{
			var last = jul.find('li:last').clone(true);
			jul.find('li:last').remove();
			jul.find('li:first').before(last);
			jul.css({marginLeft: -ul.w + 'px'});
		}
		else
			ul.i--;

		
		$(this).parent().find('.texts > a').eq(ul.cur).toggleClass("act");					
		ul.cur = (ul.quantity+ul.cur - 1)%ul.quantity;
		var pos = -ul.i * ul.w + 'px';
		jul.animate({marginLeft: pos}, ul.animTime);
		$(this).parent().find('.texts > a').eq(ul.cur).toggleClass("act");
		return false;
		
	});
	
	// галерея, смена вправо
	$('.eg-block .right-arrow').click(function(){
		var jul = $(this).parent().find('.listalka');
		var ul = jul.get(0);
		
		if (ul.i == ul.quantity-5)
		{
			var first = jul.find('li:first').clone(true);
			jul.find('li:last').after(first);
			jul.find('li:first').remove();
			jul.css({marginLeft: (-ul.w)*(ul.quantity-5) + 'px'});
			
		}
		else
			ul.i++;

			$(this).parent().find('.texts > a').eq(ul.cur).toggleClass("act");	 
 			ul.cur = (ul.quantity+ul.cur + 1)%ul.quantity;
			var pos = -ul.i * ul.w + 'px';
			jul.animate({marginLeft: pos}, ul.animTime);
			$(this).parent().find('.texts > a').eq(ul.cur).toggleClass("act");	
			return false;
	});
	
});
