var to_id, sub_to_id;

animate_color = function() {
	var color = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
	
	$('body').animate({ backgroundColor: color }, 5000, animate_color);	
};

show_text = function() {
	$('div.content .display').show('slow', function() {
		$('div.content .layer').show();
		$('div.links').show();
	});
};

hide_text = function() {
	$('div.content .display').hide('slow');
	$('div.content .layer').hide();
	to_id = setTimeout(function() {
		$('div.links').hide();
		clearTimeout(to_id);
	}, 5000);
};

show_sub = function(element) {
	var parent = $(element).parent();
	
	if ($('ul.sub', parent).hasClass('hidden')) {
		$('ul.sub', parent).stop(true, true);
		$('div.links ul.sub').hide();
		$('div.links ul.sub').addClass('hidden');
		$('ul.sub', parent).fadeIn('slow');
		$('ul.sub', parent).removeClass('hidden');
		clearTimeout(sub_to_id);
	}
	
	$('ul.sub', parent).mouseout(function() {
		hide_sub(this);
	});
};

hide_sub = function(element) {
	var parent = $(element).parent();

	if (!$('ul.sub', parent).hasClass('hidden')) {	
		clearTimeout(sub_to_id);
		sub_to_id = setTimeout(function() {
			$('ul.sub', parent).hide();
			$('ul.sub', parent).addClass('hidden');
			clearTimeout(sub_to_id);
		}, 3000);
	}
};

$(document).ready(function() {
	$('div.links ul.sub').addClass('hidden');
	$('div.links ul.sub').hide();
	
	$('a.has_sub').mouseover(function() {
		show_sub(this);
	});
	$('a.has_sub').click(function() {
		show_sub(this);
	});
	
	if ($('body').hasClass('home')) {
		$('div.links').hide();
		$('div.content .display').hide();
		$('div.content').mouseover(function() {
			show_text();
		});
		$('div.content').click(function() {
			show_text();
		});
		$('div.content .layer').mouseout(function() {
			hide_text();
		});
	}
	
	animate_color();
	
});