var sleep_enabled = 1;
var time_to_sleep = 30;
var time_sleeping = 0;
var is_sleeping = 0;
var sleep_running = 0;
var int_id = 0;

$(function() {

	$.vegas({src:'/images/home/will_battersea.jpg'});
	$.vegas('overlay', {src:'/images/bg_grid_overlay.png'});

	$(document).ready(function() {

		document.sf.q.focus();

		if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)))
			sleep_enabled = 0;
		else if (window.outerWidth && (window.outerWidth <= 480))
			sleep_enabled = 0;
		else
			sleep_enabled = 1;

		if (sleep_enabled) {

			int_id = setInterval("update_sleep_time();", 1000);
			jQuery("#top_nav").css({position: "absolute"});
			jQuery(document).keypress(function() {wake_up();});
			jQuery(this).mousemove(function() {wake_up();});
			sleep_running = 1;

			jQuery(window).focusin(function() {
				if (!sleep_running) {
					int_id = setInterval("update_sleep_time();", 1000);
					jQuery(document).keypress(function() {wake_up();});
					jQuery(this).mousemove(function() {wake_up();});
					sleep_running = 1;
					wake_up();
				}
			});

			jQuery(window).focusout(function() {
				if (sleep_running) {
					clearInterval(int_id);
					jQuery(document).unbind('keypress');
					jQuery(this).unbind('mousemove');
					sleep_running = 0;
					wake_up();
				}
			});
		}
	});
});

function update_sleep_time()
{
	if (++time_sleeping >= time_to_sleep)
		enter_sleep_mode();
}

function enter_sleep_mode()
{
	if (!is_sleeping) {
		jQuery("#top_nav").animate({top: "-56px"}, 1500);
		jQuery("#home_bot_nav").animate({bottom: "-100px"}, 1500);
		is_sleeping = 1;
	}
}

function wake_up()
{
	if (is_sleeping) {
		document.sf.q.focus();
		jQuery("#top_nav").css({top: "0"});
		jQuery("#home_bot_nav").css({bottom: "0"});
		is_sleeping = 0;
	}

	time_sleeping = 0;
}

