// Do the Cufon replacing first
Cufon.replace(
	'#content H1',
	{
		fontFamily: 'synchro',
		hover: true
	}
);

$(function(){

	// Keeps the Channels menu item selected even while child UL is being hovered
	$('#channels')
	.hover(
		function(){
			$(this).addClass('hovered');
		},
		function(){
			$(this).removeClass('hovered');
		}
	);
	
	// Go to Channels = do nothing
	$('#gtc')
	.click(
		function(){
			return false;
		}
	);
	
	// Recycle bg image
	function recycleBg()
	{
		var $recycle = $('#recycle A');
		var random = Math.floor( Math.random() * _bgs.length );
		var filepath = 'images/backgrounds/' + _bgs[ random ];
		
		$recycle.html( 'Recycling...' );
		
		// Preload the image
		var img = new Image();
		// once it's loaded, execute code
		$(img)
		.load( function(){
			$('#bg').empty().append('<img />').find('img').attr({
				width: $('#bg').width(),
				height: $('#bg').height(),
				src: filepath
			});	
			$recycle.html( 'Recycle' );
		})
		.error( function(){
			$recycle.html( 'Oops. Try again?' );
		})
		.attr( 'src', filepath );
	}
	
	// Resize bg div and image
	function resizeBg()
	{
		var ww = $(window).width();
		var wh = $(window).height();
		$('#bg').css({
			width: ww,
			height: wh
		}).find('img').attr({
			width: ww,
			height: wh
		});
	}
	
	resizeBg();
	recycleBg();
	
	$(window).bind('resize', function(){
		resizeBg();
	});
	
	// Recycle = change background
	$('#recycle A')
	.click(
		function(){
		
			recycleBg();
			
			return false;
		}
	);
	
	
	// Open link with class 'external' in new window 
	$('a.external').click( function(){
		window.open( this.href );
		return false;
	});	
});