$(document).ready(function(){
	
	adjustSiteLayout();
	
	$("#imageLoader").click(function(evt){evt.stopPropagation();}).draggable();
	$("#copyright").show();
	
	$("#lights").click(function(){
		if ($("#imageLoader").attr("class") == "open") closeImage();
	})
	
})

$(window).resize(function(){
	adjustSiteLayout();
})

function adjustSiteLayout() {
	
	var minHeight = 600;
	var windowHeight = $(window).height();
	var windowWidth = $(window).width();
	var documentHeight = $(document).height();
	
	$("#imageLoader").css({top: windowHeight / 2 - $("#imageLoader").outerHeight() / 2, left: windowWidth / 2 - $("#imageLoader").outerWidth() / 2});
	$("#loading").css({top: windowHeight / 2 - $("#loading").outerHeight() / 2, left: windowWidth / 2 - $("#loading").outerWidth() / 2});
	
	if (windowHeight > minHeight) {
		$("#mainContent").height(windowHeight);
		$("#copyright").css({ top: windowHeight - 40});
	} else {
		$("#mainContent").height(minHeight);
		$("#copyright").css({ top: minHeight - 40});
	}
}

function callImage(url) {
	
	$("#lights").css({display: "block"}).animate({opacity: 0.8},400);
	
	$("#imageLoader").fadeIn(400, function(){ $("#loading").fadeIn(400); }).load("/includes/scripts/imageLoader.php?url="+url, function(){
		$("#mainImage").load(function(){
			$("#imageLoader").animate({
				height: $(this).height(),
				top:  $(window).height() / 2 - $(this).outerHeight() / 2,
			}, 400, function(){
				$("#imageLoader").animate({
					width: $("#mainImage").width(),
					left:  $(window).width() / 2 - $("#mainImage").outerWidth() / 2,
				}, 400, function(){
					$("#loading").fadeOut(400, function(){ $("#mainImage").fadeIn(400) });
					$("#imageLoader").attr("class", "open");
				})
			})
		})
		
	})
}

function closeImage() {
	
	$("#lights").animate({opacity: 0}, 400, function(){$(this).css({display: "none"})} );
	$("#imageLoader").fadeOut(400, function(){
		$("#mainImage").hide();
		$(this).css({
			width: 100,
			height: 100,
			top: $(window).height() / 2 - 60,
			left: $(window).width() / 2 - 60
		}).attr("class","closed");
	})
}
