/**
 * @author The One
 */

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

	$('.right-col').each(function(){
		$(this).get(0).i = 0;
		$(this).get(0).cur = 0;
	})
	
	$('.move').css({opacity: 0});
	
	$('.right-col').each(function()
	{
		$(this).get(0).quantity = $(this).find('.listalka>li').length;
	});
	
	var quantity = $('.listalka>li').length;
	
	$('.gallery').each(function()
	{
		var current = $(this).parents('.right-col').get(0).i;
		
		$(this).parents('.right-col').find('.listalka-text:eq(' + current + ')').css({display: 'block'});
	}) 

	$('.listalka').each(function()
	{
		var current = $(this).parents('.right-col').eq(0);
		var quant = current.get(0).quantity;
		if (quant <= 1) {
			current.find('.move-left').css({opacity: 1}).get(0).className = 'move-l';
			current.find('.move-right').css({opacity: 1}).get(0).className = 'move-r';
		}
		$(this).css({width: quant*300 + 'px'});
	}) 

	
	$('.move-left').click(function(){
		var current = $(this).parents('.right-col').eq(0);
		if (current.get(0).i == 0) {
			var last = current.find('.listalka li:last').clone(true);
			current.find('.listalka li:last').replaceWith('');
			current.find('.listalka li:first').before(last);
			current.find('.listalka').css({
				marginLeft: '-299px'
			});
			current.get(0).i++;
		} 
		current.find('.listalka-text').fadeOut(150);
		current.get(0).i--;
		current.get(0).cur--;
		current.get(0).cur = current.get(0).quantity+current.get(0).cur;
		current.get(0).cur = current.get(0).cur%current.get(0).quantity;
		var step = -current.get(0).i*299 + 'px';
		current.find('.listalka').animate({marginLeft: step}, 300);	
		current.find('.listalka-text').eq(current.get(0).cur).fadeIn(150);
	});
	
	$('.move-right').click(function(){
		var current = $(this).parents('.right-col').eq(0);
		if (current.get(0).i == current.get(0).quantity-1) {
			var first = current.find('.listalka li:first').clone(true);
			current.find('.listalka li:first').replaceWith('');
			current.find('.listalka li:last').after(first);
			current.find('.listalka').css({
				marginLeft: (-299)*(current.get(0).quantity-2) + 'px'
			});
			current.get(0).i--;
		}
		current.find('.listalka-text').fadeOut(150);
		current.get(0).i++;
		current.get(0).cur++;
		current.get(0).cur = current.get(0).cur%current.get(0).quantity;
		var step = -current.get(0).i*299 + 'px';
		current.find('.listalka').animate({marginLeft: step}, 300);	
		current.find('.listalka-text').eq(current.get(0).cur).fadeIn(150);
	});
	
	
	$('.listalka img').each(function(){
		var this_pic = this;
		var this_url = this.src;
		$(this).attr('src','');
		this.onload = function(){
			$(this_pic).parents('li').eq(0).find('.loading').fadeOut(500);
		}
		$(this).attr('src', this_url);
	});
	
	$('.move').bind('mouseenter', function()
	{
		$(this).animate({opacity: 1}, 100);
	});

	$('.move').bind('mouseleave', function()
	{
		$(this).animate({opacity: 0}, 100);
	});
	
});