$(document).ready(function() {
	$(".slide").css("position","fixed").css("margin",0);
	$(".text").css("display","none");
	$("body").css("height",5000);
	$(".slide:first-child").addClass("show");
	$("#overlay").hover(function(){
		$(this).css("opacity",1);
	},function(){
		$(this).css("opacity",0);
	});
	$("#overlay #wrap #description-link").click(function(){
		$("#overlay #wrap #description-link").hide();
		$("#overlay #wrap #caption").hide();
		$("#overlay #wrap #description").show();
		$("#overlay #wrap #url-link").hide();
	});
	resize();
	scroll();
	setTimeout(function(){
		$("#notice").fadeIn();
	},3000);
});
$(window).load(function(){
	$("#slideshow").show();
	$(".lazy").each(function(){
		$(this).attr("src",$(this).attr("data-src"));
	});
});
$(window).resize(resize);
$(window).scroll(scroll); 
$(window).focus(function(){
	$("#overlay").css("opacity",0);
});	
	
function scroll(){
	offset = $(window).scrollTop() / ($("body").height() - $(window).height()); 
	if(offset > 0){
		$("#notice").remove();
	}
	totaloffset = $("body").height() - $(window).height();
	total = $(".slide").length - 1;
	pos = parseInt(total * offset);
	offsetpos = $(window).scrollTop();
	startoffset = (totaloffset/total)*parseInt(pos);
	endoffset = (totaloffset/total)*(parseInt(pos)+1);
	
	$("#overlay #wrap #description").hide();
	caption = $("#slide_"+pos).parent().children(".text").children(".caption").html();
	$("#overlay #wrap #caption").html(caption);
	if(caption != ''){
		$("#overlay #wrap #caption").show();
	} else {
		$("#overlay #wrap #caption").hide();
	}
	
	description = $("#slide_"+pos).parent().children(".text").children(".description").html();
	$("#overlay #wrap #description").html(description);
	if(description != ''){
		$("#overlay #wrap #description-link").show();
	} else {
		$("#overlay #wrap #description-link").hide();
	}
	
	url = $("#slide_"+pos).parent().children(".text").children(".url").html();
	$("#overlay #wrap #url-link").html(url);
	if(url != ''){
		$("#overlay #wrap #url-link").show();
	} else {
		$("#overlay #wrap #url-link").hide();
	}
	
	colour = $("#slide_"+pos).attr("data-colour");
	$("body").removeClass().addClass(colour);
	$(".slide:not(#slide_"+pos+")").removeClass("show").addClass("hide");
	$("#slide_"+pos).removeClass("hide").addClass("show");
}
function resize(){
	var winwidth, winheight, conwidth, conheight, imgheight, imgwidth, top, left;
	winwidth = $(window).width();
	winheight = $(window).height();
	conwidth = winwidth - 260;
	if(conwidth < 493){ conwidth = 493; }
	conheight = winheight - 95;
	conratio = conwidth/conheight;
	imgwidth = 1200;
	imgheight = 800;
	imgratio = 1200/800;
	if(conratio < imgratio){
		width = conwidth;
		height = conwidth / imgratio;
	} else {
		height = conheight;
		width = conheight * imgratio;
	}
	if(width > imgwidth || height > imgheight){
		width = imgwidth;
		height = imgheight;
	}

	top = (winheight/2) - (height/2);
	left = (winwidth/2) - (width/2);
	if(left < 130){left = 130; }
	$(".slide").css("top",top+"px").css("left",left+"px").css("height",height+"px").css("width",width+"px");
	$("#overlay").css("top",top+"px").css("left",left+"px");
	$("#overlay").css("height",height+"px").css("width",width+"px");
	$(".slide img").css("width",width+"px");	
}


