/**
 * @author The One
 * @edited 
 */

$(document).ready(function()
{
	
	$('.loading').css({opacity: 0.85});
	
	// галерея с вертикальной прокруткой
	$('.vertical-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).h = $($(this).find('li').get(0)).height() + 5; 
		$(this).get(0).animTime = 500;
	})

	// галерея с горизонтальной прокруткой
	$('.horizontal-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 = 129;//$($(this).find('li').get(0)).width();
		$(this).get(0).animTime = 300;
	})
	
	// верт. галерея, смена вверх
	$('.move-up').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({marginTop: -ul.h + 'px'});
		}
		else
			ul.i--;

		ul.cur = (ul.quantity+ul.cur - 1)%ul.quantity;
		var pos = -ul.i * ul.h + 'px';
		jul.animate({marginTop: pos}, ul.animTime);
	});
	

	// верт. галерея, смена вниз
	$('.move-down').click(function(){
		var jul = $(this).parent().find('.photos > ul');
		var ul = jul.get(0);
		
		if (ul.i == ul.quantity-5)
		{
			var first = jul.find('li:first').clone(true);
			jul.find('li:first').remove();
			jul.find('li:last').after(first);
			jul.css({marginTop: (-98)*(ul.quantity-6)+ 'px'});
		}
		else
			ul.i++;

 			ul.cur = (ul.quantity+ul.cur + 1)%ul.quantity;
			var pos = -ul.i * ul.h  + 'px';
			jul.animate({marginTop: pos}, ul.animTime);

	});
	
	// горизонт. галерея, смена влево
	$('.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--;
			
		ul.cur = (ul.quantity+ul.cur - 1)%ul.quantity;
		var pos = -ul.i * ul.w + 'px';
		jul.animate({marginLeft: pos}, ul.animTime);
	});
	
	// горизонт. галерея, смена вправо
	$('.move-right').click(function(){
		var jul = $(this).parent().find('.photos > ul');
		var ul = jul.get(0);
		var shift = 0;
		switch( $(this).parent().hasClass("pgallery"))
		{
			case true: 	shift = 3; break;
			case false:	shift = 2; break;
		}

		if (ul.i == ul.quantity-shift)
		{
			var first = jul.find('li:first').clone(true);
			jul.find('li:first').remove();
			jul.find('li:last').after(first);
			jul.css({marginLeft: (-129)*(ul.quantity-3)+ 'px'});
		}
		else
			ul.i++;

 			ul.cur = (ul.quantity+ul.cur + 1)%ul.quantity;
			var pos = -ul.i * ul.w + 'px';
			jul.animate({marginLeft: pos}, ul.animTime);
	});
	
/*	
	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;
	}
	
*/
});





