$(function() {
	
		// News teaser
	/* setTimeout(function() {
		$("#news-teaser").fadeIn(300);
	}, 1000); */
	
	$(document).mouseover( function() {
		setTimeout(function() {
			if($("#news-teaser").css("display") != "block") {
				$("#news-teaser").fadeIn(500);
			}
		}, 100);
	});
	
		// Contact menu drop-down
	$("#menu-contact").hover(
		function() {
			$("#menu-contact").addClass("over");
			$("#contact").fadeIn(100);
		},
		function() {
			$("#menu-contact").removeClass("over");
			$("#contact").fadeOut(100);
		}
	);
		// Project sections thumbnails overlay
	$("#overlay-close").click(
		function() {
			$("#overlay").fadeOut(200);
			$(document).unbind("click");
			return false;
		}
	);
	$("#overlay-navigation div").hover(
		function() {
			$(this).addClass("over");
		},
		function() {
			$(this).removeClass("over");
		}
	);
	
	var over_overlay = false;
	function overlaySet(el) {
		$("#screenshot img").hide();
		$("#overlay-section-project-title").text($("#project-title").text());
		$("#overlay-section-details h3").text($(el).parent().prev().text());
		$("#overlay-section-description p").text($(el).parent().next().children("p").text());
		var url = $(el).parent().next().children("a").attr("href");
		if(url) {
			$("#overlay-section-url").attr("href", url);
			$("#screenshot-url").attr("href", url);
			$("#overlay-section-url").show();
		}
		else {
			$("#overlay-section-url").hide();
		}
		$("#screenshot img").attr("src", $(el).parent().next().children("img").attr("src"));
		
		if($(el).parent().parent().next().hasClass("project-section")) {
			if($("#overlay-navigation-next").css("display") == "none") {
				$("#overlay-navigation-next").show();
			}
			$("#overlay-navigation-next a").text($(el).parent().parent().next().children("h3").text()+" »");
			$("#overlay-navigation-next").unbind("click");
			$("#overlay-navigation-next").click(function(){
				return overlaySet($(el).parent().parent().next().children("div").children("a"));
			});
		}
		else {
			$("#overlay-navigation-next").hide();
		}
		if($(el).parent().parent().prev().hasClass("project-section")) {
			if($("#overlay-navigation-previous").css("display") == "none") {
				$("#overlay-navigation-previous").show();
			}
			$("#overlay-navigation-previous a").text("« "+$(el).parent().parent().prev().children("h3").text());
			$("#overlay-navigation-previous").unbind("click");
			$("#overlay-navigation-previous").click(function(){
				return overlaySet($(el).parent().parent().prev().children("div").children("a"));
			});
		}
		else {
			$("#overlay-navigation-previous").hide();
		}
		$("#overlay").fadeIn(200,function(){
			$("#screenshot img").show();
			if($("#screenshot img").height() < 336 ) {
				$("#screenshot").css("height", $("#screenshot img").height() );
				$("#screenshot").css("width", "460px" );
				$("#browser").css("height", $("#screenshot img").height() );
				$("#browser").css("width", "460px" );
				$("#browser").css("margin-left", "20px");
			}
			else {
				$("#screenshot").css("width", "480px" );
				$("#screenshot").css("height", "336px" );
				$("#browser").css("width", "480px" );
				$("#browser").css("height", "342px" );
				$("#browser").css("margin-left", "0");
			}
			
		});
		$("#overlay").hover(
			function() {
				over_overlay = true;
			},
			function() {
				over_overlay = false;
			}
		);
		$(document).click(function(){
			if($("#overlay").css("display") != "none" && over_overlay == false ) {
				$("#overlay").fadeOut(200);
			}
		});
		$(document).keyup( function(e) {
			var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
			if( key == 27 ){
				$("#overlay").fadeOut(200);
			}
		});
		return false;
	}
	$("#project-sections .project-section-thumbnail a").click(
		function() {
			return overlaySet(this);
		}
	);	
});