// JavaScript Document

var slideInt;

function slide(direction) {
	$.ajax({
		url: '/handlers/get_slides.php',
		dataType: 'html',
		type: 'POST',
		data: {'img':$('.slides img.active').attr('src'), 'dir':direction},
		success: function(data) {
			var $img = $('.slides img[src="' + data + '"]');
			if ($img.length)
			{
					$('.slides img.active').removeClass('active').fadeOut(1500);
					$img.fadeIn(1500).addClass('active');
			}
			else
			{
				$('<img />').appendTo('.slides').attr('src', data).hide()
				.load(function(){
					$(this).fadeIn(1500).addClass('active')
					.siblings('img.active').removeClass('active').fadeOut(1500);
				});
			}
		}
	});	
}

function setSlide() {
	clearInterval(slideInt);
	slideInt = setInterval(function(){
		slide('right');
	}, 5000);
}

$(document).ready(function(){
	if ($('.slides').length)
		setSlide();
	
	$('.arrowLeft').click(function(){
		slide('left');
		setSlide();
		$paginator_cont.children('li.active').prev('li').click();
	});
	$('.arrowRight').click(function(){
		slide('right');
		setSlide();
		$paginator_cont.children('li.active').next('li').click();
	});
	
	$('.slides img').live('click', function(){
		$('.arrowRight').click();
	});
	
	$('.arrowLeft, .arrowRight').mousedown(function() {
		if ($(this).is('.disabled') == false)
		{
			pos = $(this).position();
			$(this).css('left', pos.left - 1);
			$(this).css('top', pos.top + 1);
		}
	});
	
	$('.arrowLeft, .arrowRight').mouseup(function() {
		if ($(this).is('.disabled') == false)
		{
			pos = $(this).position();
			$(this).css('left', pos.left + 1);
			$(this).css('top', pos.top - 1);
		}
	});
	
	var $paginator_cont = $('#collection .paginator .container');
	var $previewer_cont = $('#collection .previewer .container');
	var img_width = $previewer_cont.find('img').width() + parseInt($previewer_cont.find('img').css('margin-right'));
	var imgs_length = $previewer_cont.find('img').length;
	$previewer_cont.width(img_width * imgs_length);
	var speed = 500;
	
	for (var i = 1; i <= imgs_length; i++)
	{
		$paginator_cont.append('<li>' + i + '</li>');
	}
	
	var paginator_cont_width = 0;
	var paginator_width = $paginator_cont.parent('.paginator').width();
	$paginator_cont.children('li').each(function() {
		paginator_cont_width += $(this).outerWidth();
	});
	
	$paginator_cont.children('li').click(function() {
		var id = $previewer_cont.find('img').eq($paginator_cont.children('li').index($(this))).attr('id').replace('col_item_', '');
		var $li = $(this);
		//var ajax_loader_timeout = setTimeout(function(){
			//$('#preload').fadeIn(500);
		//}, 250);
		$.ajax({
			url: '/handlers/get_collection_item.php',
			dataType: 'html',
			type: 'POST',
			data: {
				'id': id
			},
			success: function(html) {
				$('.left_column').empty().append($('.model').html(html).children('img'));
				
				$li.addClass('active').siblings().removeClass('active');
				li_left = $li.position().left;
				paginator_cont_left = parseInt($paginator_cont.css('left'));
				if (122 - li_left < paginator_cont_left)
				{
					if (122 - li_left > paginator_width - paginator_cont_width)
						$paginator_cont.animate({left: 122 - li_left}, speed);
					else
						$paginator_cont.animate({left: paginator_width - paginator_cont_width}, speed);
				}
				if (122 - li_left > paginator_cont_left)
					if (122 - li_left <  - paginator_cont_left)
						$paginator_cont.animate({left: 122 - li_left}, speed);
					else
						$paginator_cont.animate({left: 0}, speed);
				$previewer_cont.animate({left: - img_width * ($li.text() - 1)}, speed);
				// большое изображение изменяется в соответствии с выбранным превью. Реализовано через AJAX
				//$('#collection .left_column img').attr({src: $previewer_cont.find('img').eq($paginator_cont.children('li').index($(this))).attr('src').replace('/thumbs', '')})
				$('.arrowLeft, .arrowRight').removeClass('disabled');
				if ($li.text() == '1')
					$('.arrowLeft').addClass('disabled');
				if ($li.text() == imgs_length)
					$('.arrowRight').addClass('disabled');
				//clearTimeout(ajax_loader_timeout);
				//$('#preload').fadeOut(500);
			}
		});
	});
	
	$paginator_cont.children('li:first').click();
	
	$previewer_cont.find('img').click(function() {
		$paginator_cont.children('li').eq($previewer_cont.find('img').index($(this))).click();
	});
	
	$('#collection .left_column').click(function() {
		$paginator_cont.children('li.active').next('li').click();
	});
});

$(window).load(function(){
	$('#preload').fadeOut(500);
});
