/**
 * @author The One
 * @edited for karapyzik by Molester
 */

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

	$('.photo-listalka').find('.photos > ul').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();
		$(this).get(0).animTime = 400;
	})
	
	// галерея, смена влево
	$('.move-left').click(function(){
		var jul = $(this).parent().find('.photos > ul');
		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");
		
	});
	
	// галерея, смена вправо
	$('.move-right').click(function(){
		var jul = $(this).parent().find('.photos > ul');
		var ul = jul.get(0);
		
		if (ul.i == ul.quantity-1)
		{
			var first = jul.find('li:first').clone(true);
			jul.find('li:first').remove();
			jul.find('li:last').after(first);
			jul.css({marginLeft: (-226)*(ul.quantity-2) + '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");	

	});
	
	
/*	
	var imagelist = [];
	
	imagelist.length = $('.listalka img').length;
	
	for (var j = 0; j < imagelist.length; j++) {
		imagelist[j] = new Image();
		imagelist[j].onload = function () {
			$('.listalka img').eq(this.seq).parents('li').eq(0).find('.loading').fadeOut(500);
		}
		imagelist[j].src = $('.listalka img').eq(j).attr("src"); 
		imagelist[j].seq = j;
	}
	
*/
});






