//$('#myDiv').data('currentState', 'off'); - можно воспользоваться такой вещью, для того, чтобы узнавать, загружена картинка или нет.

var img_current;
var img_shift;
var cont_size;
var state;

$(function(){
	img_current = 0;
	img_shift = 0;
 	cont_size = $("div.image_container > div").size()
	state = 0;

	var my_img = new Image(); 

	my_img.src = $("div.image_container > div:eq(0) > a > img").attr("src");
	
	//Проверяем загружена ли картинка
	if (my_img.width != 0)
	{
		//Картинка загружена, можно считать и двигать
		img_shift = ($("div.album_container").width()/2 - $("div.image_container > div:eq(0)").width()/2);
	
		$("div.image_container").css("left", img_shift);
	}
	else
	{
		//Ждем загрузки картинки
		$("div.image_container > div:eq(0) > a > img").load(function() {  
			//Картинка загружена, можно считать и двигать
    		img_shift = ($("div.album_container").width()/2 - $("div.image_container > div:eq(0)").width()/2);
	
			$("div.image_container").animate({
			left: img_shift
			}, "slow");
		});
	}
	
	$("a.prev").bind("click", function(){
		if (img_current < (cont_size - 1) && state == 0)
		{
			//Создаем новую картинку, чтобы проверить ее размеры, чтобы проверить начала ли она загружаться
			var my_img = new Image(); 
			my_img.src = $("div.image_container > div:eq(" + (img_current + 1) + ") > a > img").attr("src");
			
			//Проверяем загружена ли картинка
			if (my_img.width != 0)
			{
				state = 1;
			
				img_shift = img_shift - ($("div.image_container > div:eq(" + img_current + ")").width()/2 + $("div.image_container > div:eq(" + (img_current + 1) + ")").width()/2);
			
				var t = $("div.image_container > div:eq(" + (img_current + 1) + ") > a > img").attr("title");
				var d = $("div.image_container > div:eq(" + (img_current + 1) + ") > a > img").attr("alt");
				
				$("#photo_description").html("<b>" + t + ".</b> " + d);
							
				if (img_current == (cont_size - 2)) $("#gall_right").css("display", "none");
				if (img_current == 0) $("#gall_left").css("display", "block");
			
				$("div.image_container").animate({
				left: img_shift
				}, "slow", "", function(){
					img_current = img_current + 1;
					state = 0;
				});
			}
			else
			{
				state = 1;
				//Ждем загрузки картинки
				$("div.image_container > div:eq(" + (img_current + 1) + ") > a > img").load(function() {  
				
					img_shift = img_shift - ($("div.image_container > div:eq(" + img_current + ")").width()/2 + $("div.image_container > div:eq(" + (img_current + 1) + ")").width()/2);
					
					var t = $("div.image_container > div:eq(" + (img_current + 1) + ") > a > img").attr("title");
					var d = $("div.image_container > div:eq(" + (img_current + 1) + ") > a > img").attr("alt");
				
					$("#photo_description").html("<b>" + t + ".</b> " + d);
					
					if (img_current == (cont_size - 2)) $("#gall_right").css("display", "none");
					if (img_current == 0) $("#gall_left").css("display", "block");
						
					$("div.image_container").animate({
					left: img_shift
					}, "slow", "", function(){
						img_current = img_current + 1;
						state = 0;
					});
				});
			}
		}
		
		return false;
	});
	
	$("a.next").bind("click", function(){
		if (img_current > 0  && state == 0)
		{
			//Создаем новую картинку, чтобы проверить ее размеры, чтобы проверить начала ли она загружаться
			var my_img = new Image();
			my_img.src = $("div.image_container > div:eq(" + (img_current - 1) + ") > a > img").attr("src");
			
			//Проверяем загружена ли картинка
			if (my_img.width != 0)
			{
				state = 1;
				
				img_shift = img_shift + ($("div.image_container > div:eq(" + img_current + ")").width()/2 + $("div.image_container > div:eq(" + (img_current - 1) + ")").width()/2);
				
				var t = $("div.image_container > div:eq(" + (img_current - 1) + ") > a > img").attr("title");
				var d = $("div.image_container > div:eq(" + (img_current - 1) + ") > a > img").attr("alt");
				
				$("#photo_description").html("<b>" + t + ".</b> " + d);
				
				if (img_current == (cont_size - 1)) $("#gall_right").css("display", "block");
				if (img_current == 1) $("#gall_left").css("display", "none");
			
				$("div.image_container").animate({
				left: img_shift
				}, "slow", "", function(){
					img_current = img_current - 1;
					state = 0;
				});
			}
			else
			{
				state = 1;

				$("div.image_container > div:eq(" + (img_current - 1) + ") > a > img").load(function() {  		
					img_shift = img_shift + ($("div.image_container > div:eq(" + img_current + ")").width()/2 + $("div.image_container > div:eq(" + (img_current - 1) + ")").width()/2);
				
					var t = $("div.image_container > div:eq(" + (img_current - 1) + ") > a > img").attr("title");
					var d = $("div.image_container > div:eq(" + (img_current - 1) + ") > a > img").attr("alt");
				
					$("#photo_description").html("<b>" + t + ".</b> " + d);
				
					if (img_current == (cont_size - 1)) $("#gall_right").css("display", "block");
					if (img_current == 1) $("#gall_left").css("display", "none");
				
					$("div.image_container").animate({
					left: img_shift
					}, "slow", "", function(){
						img_current = img_current - 1;
						state = 0;
					});
				});
			}
		}
		
		return false;
	});
});

function renew()
{
	img_current = 0;
	img_shift = 0;
	cont_size = $("div.image_container > div").size()
	state = 0;
	
	//Создаем новую картинку, чтобы проверить ее размеры, чтобы проверить начала ли она загружаться
	var my_img = new Image();
	my_img.src = $("div.image_container > div:eq(0) > a > img").attr("src");
	
	//Проверяем загружена ли картинка
	if (my_img.width != 0)
	{
		img_shift = ($("div.album_container").width()/2 - $("div.image_container > div:eq(0)").width()/2);
		$("div.image_container").css("left", img_shift);
	}
	else
	{
		$("div.image_container > div:eq(0) > a > img").load(function() {
			img_shift = ($("div.album_container").width()/2 - $("div.image_container > div:eq(0)").width()/2);
			$("div.image_container").css("left", img_shift);																					 
		});
	}	
	
	if (cont_size == 1)
	{
		$("#gall_left").css("display", "none");
		$("#gall_right").css("display", "none");
	}
	else
	{
		$("#gall_left").css("display", "none");
		$("#gall_right").css("display", "block");
	}
}

function refill(id)
{
	if (state == 0)
	{
		$.ajax({
			type: "GET",
			url: "inc/ajax.php",
			data: "id=" + id,
			success: function(data){
				if (data != "")
				{
					$("div.image_container").html(data);
					renew();

					var t = $("div.image_container > div:eq(0) > a > img").attr("title");
					var d = $("div.image_container > div:eq(0) > a > img").attr("alt");
					$("#photo_description").html("<b>" + t + ".</b> " + d);
				}
			}
		});
	}
}

function recolor(menu_item)
{
	$('#album_head').html($(menu_item).html());
	$(menu_item).parent().parent().find("li > a").removeClass("grey");
	$(menu_item).addClass("grey");
}
