// JavaScript Document
$(document).ready(function(){
	$.fn.alignCenter = function(){
		
		while(($(window).height() - $(this).height()) < 50)
		{
			$(this).height($(this).height() * 0.99);
			$(this).width($(this).width() * 0.99);
		}
		while(($(window).width() - $(this).width()) < 250)
		{
			$(this).height($(this).height() * 0.99);
			$(this).width($(this).width() * 0.99);
		}
		
		var marginLeft = $(window).width()/2 - $(this).width()/2 + 'px';
		var marginTop = $(window).height()/2 - $(this).height()/2 + 'px';
		
		return $(this).css({'margin-left':marginLeft, 'margin-top':marginTop});
	};
});

var index, inc, max_hide, max_img_num, current_img, arr, timeout, pause_interval;
index = 1;
inc = 1;
max_hide = 6;
max_img_num = 13;
pause_interval = 0;
arr = [];

function slide_photo()
{
	if(pause_interval == 0)
	{
		if(inc > 0)
		{		
			arr[index-1] = $("#small_image"+index).width();
			$("#small_image"+index).animate({width: 0, opacity: 0}, 2500, function(){
				if(index == max_hide)
				{
					inc = -1;
				}
				else
				{
					index = index + inc;
				}
			});
		}
		else
		{			
			$("#small_image"+index).animate({width: arr[index-1], opacity: 1}, 2500, function(){
				if(index == 1)
				{
					inc = 1;
				}
				else
				{
					index = index + inc;
				}
			});
		}
	}
}

function small_img_over(img_id)
{
	pause_interval = 1;	
	timeout = window.setTimeout(function()
		{
			$("#medium_image"+img_id).css("top", $("#small_image"+img_id).offset().top - 1);
			$("#medium_image"+img_id).css("left",$("#small_image"+img_id).offset().left - 1); //Для удаления снега удалить переменную left
			$("#medium_image"+img_id).show();
			$("#medium_image"+img_id).animate({top: "-=25", left: "-=25", height: "150"}, 500);
		},
		1500
	);	
}

function small_img_out()
{
	pause_interval = 0;
	window.clearTimeout(timeout);
}

function medium_img_over()
{
	pause_interval = 1;
}

function medium_img_out(img_id)
{
	pause_interval = 0;
	$("#medium_image"+img_id).animate({top: "+=25", left: "+=25", height: "100"}, 500, function(){
		$("#medium_image"+img_id).hide();
	});
}

function arrow_left_over()
{
	pause_interval = 1;
}

function arrow_left_out()
{
	pause_interval = 0;
}

function arrow_right_over()
{
	pause_interval = 1;
}

function arrow_right_out()
{
	pause_interval = 0;
}

function arrow_left_click()
{/*
	arr[index-1] = $("#small_image"+index).width();
	$("#small_image"+index).animate({width: 0, opacity: 0}, 2500, function(){
		if(index == max_hide)
		{
			inc = -1;
		}
		else
		{
			index = index + inc;
		}
	});*/
	//if(inc > 0)
	//{
		/*arr[index-1] = $("#small_image"+index).width();
		$("#small_image"+index).animate({width: 0, opacity: 0}, 2500, function(){
			if(index == max_hide)
			{
				return false;
			}
			else
			{
				index = index + inc;
			}
		});*/
	//}
}

function arrow_right_click()
{/*
	$("#small_image"+index).animate({width: arr[index-1], opacity: 1}, 2500, function(){
		if(index == 1)
		{
			inc = 1;
		}
		else
		{
			index = index + inc;
		}
	});*/
}

function img_to_full(img_id)
{	
	$(document).ready(function(){
		current_img = img_id;
		$("#main_flash_banner").hide();
		$("#dark").height($(document).height()).fadeTo('slow', 0.8);		
		$("#full_image"+img_id).alignCenter().fadeIn(600, function(){ pause_interval = 1; });
		
		var Top = ($(window).height() - $("#arrow_right").height())/2;
		var Left = $(window).width() - $("#arrow_right").width() - 65;
		$("#arrow_right_full").css("top", Top);
		$("#arrow_right_full").css("left", Left);
		$("#arrow_right_full").fadeTo('slow', 1);
		$("#arrow_left_full").css("top", Top);
		$("#arrow_left_full").css("left", 10);
		$("#arrow_left_full").fadeTo('slow', 1);		
	});
}

function full_image_click(img_id)
{
	$(document).ready(function(){
		$("#full_image"+img_id).fadeOut(600, function(){
			$("#arrow_right_full").fadeOut(200, 0);
			$("#arrow_left_full").fadeOut(200, 0);
			$("#dark").fadeTo('slow', 0);
			$("#dark").height(0);
			$("#main_flash_banner").show();
			pause_interval = 0;			
		});
	});
}

var end_animate = 1;

function arrow_right_full_click()
{
	if(end_animate == 1)
	{
		end_animate = 0;
		$("#full_image"+current_img).fadeOut(600, function(){
			current_img = current_img + 1;
			if(current_img >= max_img_num){ current_img = 1; }
			$("#full_image"+current_img).alignCenter().fadeIn(600, function(){				
				end_animate = 1;
			});
		});
	}
}

function arrow_left_full_click()
{
	if(end_animate == 1)
	{
		end_animate = 0;
		$("#full_image"+current_img).fadeOut(600, function(){
			current_img = current_img - 1;
			if(current_img <= 1){ current_img = max_img_num; }
			$("#full_image"+current_img).alignCenter().fadeIn(600, function(){				
				end_animate = 1;
			});
		});
	}
}

window.setInterval(slide_photo, 10000);
