$.globals = {};
function disableEvents()
{
	$("#thumbnails img:not(.no-popup)").unbind("click");
	$("#container").addClass("pointer").bind("click",hidePopup);
	$(window).data("position",$(window).scrollTop());
	$(window).bind("scroll",hideWithScroll);
}
function enableEvents()
{
	$("#thumbnails img:not(.no-popup)").bind("click",showPopup);
	$("#container").addClass("pointer").unbind("click");
	$(window).unbind("scroll");
}
function showPopup()
{
	disableEvents();
	$("#popup img").remove();
	var obj = this;
	var popup_src = $(obj).attr("src").split(".jpg").join("_hi.jpg");
	var initX = $(obj).offset().left;
	var initY = $(obj).offset().top;
	var initWidth = $(obj).width();
	var initHeight = $(obj).height();
	var endX = ($(window).width()-$("#popup").width())/2;
	var endY = ($(window).height()-$("#popup").height())/2 + $(window).scrollTop();
	var endWidth = $("#popup").width();
	var endHeight = $("#popup").height();
	$("#popup").css("opacity",.5);
	$("#zoom").hide();
	$("#oscurece").width($(document).width());
	$("#oscurece").height($(document).height());
	$("#loading").css({
		top:($(window).height()/2)-($("#loading").height()/2) + $(window).scrollTop(),
		left:($(window).width()/2)-($("#loading").width()/2),
	});
	$("#oscurece").fadeIn("normal",function(){
		$("#popup").width(initWidth);
		$("#popup").height(initHeight);
		$("#popup").show();
		$("#popup").css('top',initY);
		$("#popup").css('left',initX);
		$("#popup").animate({
	    top:endY+'px', 
	    left:endX+'px', 
	    width:endWidth+"px", 
	    height:endHeight+"px"
		}, 150, function(){		
			$("#popup,#oscurece").addClass("pointer").bind("click",hidePopup);
			if($(obj).data("loaded")==1)
			{
				$("#popup").append($("<img id=\"hi-res\" />").attr("src",popup_src).hide());
				$("#popup img").fadeIn("slow");
				$("#popup").css("opacity",1);				
			}
			else
			{
				$("#loading").show();
				$("#popup").append($("<img />").hide());
				$("#popup img").attr("src",popup_src).load(function(){
					$("#loading").fadeOut("fast",function(){
						$(obj).data("loaded",1);
						$("#popup img").fadeIn("slow");
						$("#popup").css("opacity",1);
					})
				});
			}
		});
	});	
}
function hideWithScroll()
{
	if($(window).scrollTop()>=($(window).data("position")+300) || $(window).scrollTop()<=($(window).data("position")-300))
	{
		hidePopup();
	}
}
function hidePopup()
{
	$("#loading").hide();
	$("#preloader,#popup").hide(0,function(){
		$('#oscurece').fadeOut('normal',function(){
			enableEvents();
		});
	}).removeClass("pointer");
}
function addPadding()
{
	var padding = Math.floor(($(document).width() - (Math.floor($(document).width()/280)*280)) / 2);
	$("#thumbnails").css({
		paddingLeft : padding+"px"
	});
}
function preloadImages(){
	$("#thumbnails img").each(function(){
		
	});
}
$(document).ready(function(){
	enableEvents();
	addPadding();
	$(window).resize(addPadding);
	preloadImages();
});
