$(function(){
	//hide all bloys
	$('#bloy_content').children().hide();
	
	//set up links (for nojs degrade)
	$('.fixhash')
		.each(function(i,el){
			$(el).attr('href', el.href.replace('#', '#_'));
		});

	//set up bbq cache
	var cache = {
		'': $('#home')
	};
	
	//bbq hashchange event
	$(window).bind('hashchange', function(e){
		//get the hash and fix it to match div ids (for nojs degrade)
		var url = $.param.fragment().replace('_','');
		
		//clean up
		$('#bloy_menu a.current').removeClass('current');
		$('#bloy_content').children(':visible').hide();
		
		//set right menu link class
		url && $( '#bloy_menu a[href*="#_' + url + '"]' ).addClass( 'current' );
		
		//go back to the top of the page
		$(window).scrollTop(0);

		//figure out what to show
		if(cache[url])
			cache[url].show();
		else {
			cache[url] = $('#'+url).show();
		}
		
		if($.url.param('play_video') === '')
		{
			var vidx = $('.video_headline:visible').position().top;
			$(window).scrollTop(vidx);
		}
		
		//stop any videos that may be playing
		stopAllVideos();
	});
	
	//trigger hashchange ondomready
	$(window).trigger('hashchange');
	
	function stopAllVideos()
	{
		$('.video object, .video embed')
			.each(function(i,el){
				var p = document.getElementById(el.id);
				if(p.sendEvent)
					p.sendEvent('STOP');
			});
	}
});
