$(document).ready(function()
{
	document.onmousedown=function(){return false;}
	document.onselectstart=function(){return false;} 

	speed=1000;
	display=false; 
	var load=true;
	left_width=$("#left_content").width();
		

	
	var bottom_foto='<table id="bottom_foto_table" border=0" cellpadding="0" cellspacing="0">';
		bottom_foto+='<tr>';
			for (i=1; i<=total_foto; ++i)
			bottom_foto+='<td><img src="../img/'+dir_small_foto+'/'+i+'.jpg" class="small_foto_array"></td>';
		bottom_foto+='</tr>';
		bottom_foto+='</table>';
	
	var top=$(window).height()/2-65;
	$("#content_big_foto").css({backgroundImage:"url('../img/bg_loader.gif')", backgroundPosition: "50% "+top});
	
	$("#left, #right").css({opacity:0.6, top:5, backgroundImage:"url('../img/but_next1.gif')", backgroundRepeat:"no-repeat", backgroundPosition: "50% 50%"});
	$("#left").css("backgroundImage", "url('../img/but_back1.gif')");
	
	$("#bottom_foto").html(bottom_foto);
	
	if (details.length<=22 && $("#bottom_foto").width()>1280) $("#left, #right").css("opacity",0);
	else $("#left, #right").css("opacity",0.6);	
	
	$(document).ready(function()
	{
		$(".small_foto_array").load(function()
		{
			$(this).fadeIn(speed, function()
			{
				
				hash_image=location.hash.substring(1);
				if (!hash_image) hash_image=1;
					
				$("#content_big_foto").html('<img id="big_image" src="../img/'+dir_big_foto+'/'+hash_image+'.jpg" style="display:none;" width="100%">');
				
				$("#big_image").bind("load resize", function()
				{
					if (load===true)
					{
						$(this).fadeIn(speed, function()
						{
							$("#collection_page").fadeIn(speed*2).html(collection);
							$("#details").html(details[hash_image]);
							display=true;
							$("#big_image").draggable('destroy');
							$("#big_image").draggable({axis:"y", containment:[0, $(window).height()-$("#big_image").height(), 0, 0]});
							load=false;
						});
					}
				});
			});
		});	
	});
	
	
	$("#content_big_foto").click(function()
	{
		$("#bottom_scale").css("display", "none");
		
		if (display===true)
		{
			
			$("#bottom_foto").animate({scrollLeft:0}, speed); 
			$("#left_content").animate({width:"0px"}, speed, function() 
			{
				$(this).hide(); 
				$("#img_show_left_content").show();
								
				$("#bottom_content").animate({height:"0px"}, speed, function()
				{
					display=false;
					$(this).hide(); 
					$("#img_show_footer_content").show();
					
					$("#big_image").draggable('destroy');
					$("#big_image").draggable({axis:"y", containment:[0, $(window).height()-$("#big_image").height(), 0, 0]});
				}); 
			});
		}
		
		
		
		
		$("#content_big_foto").click(function()
		{
			if (display===false)
			{
				
				$("#bottom_foto").animate({scrollLeft:0}, speed); 
				$("#left_content").animate({width:"230px"}, speed, function() 
				{
					$("#img_show_left_content").hide();
									
					$("#bottom_content").animate({height:"65px"}, speed, function()
					{
						display=true;
						$("#img_show_footer_content").hide();
						$("#big_image").draggable('destroy');
						$("#big_image").draggable({axis:"y", containment:[0, $(window).height()-$("#big_image").height(), 0, 0]});
					}); 
				});
			}
		});			
			
		
		
	});

	
	
	$("#right").mouseover(function()
	{ 
		$("#bottom_scale").hide();
		
		if(display===true)
		$("#bottom_foto").animate({scrollLeft:$("#bottom_foto_table").width()-$("#content_big_foto").width()}, speed*5);
	});
	
	$("#left").mouseover(function()
	{ 
		$("#bottom_scale").hide();
		
		if(display===true)
		$("#bottom_foto").animate({scrollLeft:"0"}, speed*5);
	});
	
	
	$("#right, #left").mouseout(function()
	{
		$("#bottom_foto").stop();
	});
		
	
	$("#img_show_left_content").click(function()
	{
		$("#big_image").animate({top:0}, speed, function() 
		{
			$("#left_content").animate({width:"230px"}, speed, function()
			{
				$("#img_show_left_content").hide();
				display=true;
				
				$("#big_image").draggable('destroy');
				$("#big_image").draggable({axis:"y", containment:[0, $(window).height()-$("#big_image").height(), 0, 0]});
			});
		});
	});		
	
	
	$("#img_show_footer_content").click(function()
	{
		$("#bottom_content").animate({height:"65px"}, speed, function()
		{
			$("#img_show_footer_content").hide();
			display=true;
		});
	});	
	
	
	$(".small_foto_array").mouseover(function()
	{
		if (display===true)
		{
			$(this).attr("src", function(index, src)
			{
				var left=$(this).offset().left;
				
				
				if (left > $("#right").offset().left-110) left=$("#right").offset().left-100;
				else if (left < $("#left").offset().left+20) left=$("#left").offset().left+10;				
				else left=left-20;
				
				$("#bottom_scale").attr({src:src}).css({left:left, display:"block"});
			});
		}
	});
	
	
	$("#bottom_scale").click(function()
	{
		$(this).attr("src", function(index, src)
		{
			var reg=/\d/g;
			var hash=src.match(reg);
			
			location.hash="";
			for (var i=0; i<hash.length; ++i)
			location.hash+=hash[i];
						
			$("#big_image").fadeOut(speed, function()
			{
				$(this).attr({src:src.replace("small", "big")}).css("top", "0").load(function()
				{
					
					$("#details").html(details[location.hash.substring(1)]);
										
					$(this).draggable('destroy');
					$(this).fadeIn(speed).draggable({axis:"y", containment:[0, $(window).height()-$(this).height(), 0, 0]});
				});
			});
		});
	});
	
	$("#bottom_scale").mouseout(function()
	{
		$(this).css("display", "none");
	});
	
	$(window).resize(function()
	{
		$("#bottom_scale").css("display", "none");
		$("#big_image").animate({top: 0}, speed, function()
		{
			$("#big_image").draggable('destroy');
			$("#big_image").draggable({axis:"y", containment:[0, $(window).height()-$("#big_image").height(), 0, 0]});			
		});
		
		if (details.length<=22 && $("#bottom_foto").width()>1280) $("#left, #right").css("opacity",0);
		else $("#left, #right").css("opacity",0.6);	
	});	
});
