$(document).ready(function(){
	$("#sidebar").animate({ 
	        opacity: 0.0
	      }, 1500 );
	
	var isFadingOut = 1;
	var isScreensaver = 0;
	var timerScreensaver;

	screensaver = function() {
		$("#page").stop();
		$("h1").stop();
		$("body").stop();
		$("#page").animate({ 
			opacity: 0.0
		}, 1000 );
		$("h1").animate({ 
			opacity: 0.0
		}, 1000, "", function(){
			$("body").animate({ 
				backgroundColor: "#000"
			}, 1000 );
		} );
		
		isScreensaver = 1;
		
		timerScreensaver = null;  // this needs to be falsy next mousemove start
	};

	clearTimeout( timerScreensaver );
	timerScreensaver = setTimeout( screensaver, 900000 );
	
	$("body").mousemove(function(){
		var timer,
		onmousestop = function() {
			$("#sidebar").stop();
			$("#sidebar").animate({ 
				opacity: 0.0
			}, 1000 );
			
			isFadingOut = 1;
			
			timer = null;  // this needs to be falsy next mousemove start
		};
		if (timer == null) {
			// code to do on start
			if (isFadingOut == 1) {
				isFadingOut = 0;
				$("#sidebar").stop();
				$("#sidebar").animate({ 
					opacity: 1.0
				}, 1000 );
			}
		}

		if (timerScreensaver == null) {
			// code to do on start
			if (isScreensaver == 1) {
				isScreensaver = 0;
				$("#page").stop();
				$("h1").stop();
				$("body").stop();
				$("body").animate({ 
					backgroundColor: "#fff"
				}, 1000, "", function(){
					$("#page").animate({ 
						opacity: 1.0
					}, 1000 );
					$("h1").animate({ 
						opacity: 1.0
					}, 1000 );
				} );
			}
		}

		// we are still moving, or this is our first time here...
		clearTimeout( timer );
		timer = setTimeout( onmousestop, 10000 );

		clearTimeout( timerScreensaver );
		timerScreensaver = setTimeout( screensaver, 900000 );
	});
});