function bg_fadein(opac){
	if(opac < 100){
		var h = opac+5;
		document.getElementById('div_background_hq').style.opacity = h/100;
		window.setTimeout("bg_fadein('" + h + "');",1);
	} 
}

$(function () {
	
	// Preload hover images
	$('.nav a').each(function () {
		$(this).data('image', $(this).css('background-image'));
		$(this).data('hoverImage', $(this).css('background-image').replace(/_([^\.]+?)\.gif/, '_$1_active.gif'));
		$('<div/>', {css: {backgroundImage: $(this).data('hoverImage')}}).appendTo($('body'));
	});
	
	// Activate navigation
	$('.mainnav a').click(function () {
		$('.subnav, .subsubnav').hide();
	});
	$('.subnav a').click(function () {
		$('.subsubnav').hide();
	});
	$('.mainnav a, .subnav a').click(function () {
		$('#nav_'+$(this).attr('class')).show();
	});
	$('.nav a').hover(function () {
		$(this).css('background-image', $(this).data('hoverImage'));
	}, function () {
		$(this).css('background-image', $(this).data('image'));
	});
	
	
	// Use .breadcrumb tag to determine which submenus will be visible
	var breadcrumb = $('.breadcrumb').text().split(' > ');
	var subnavTitle = breadcrumb[1] || null;
	var subsubnavTitle = breadcrumb[2] || null;
	
	$('.subnav').each(function () {
		if ( ! subnavTitle) return;
		var encodedTitle = subnavTitle.toLowerCase().replace(/\s+/, '-');
		if ($(this).attr('id').match(new RegExp(encodedTitle, 'i'))) {
			$(this).show();
		}
	});
	$('.subsubnav').each(function () {
		if ( ! subsubnavTitle) return;
		var encodedTitle = subsubnavTitle.toLowerCase().replace(/\s+/, '-');
		if ($(this).attr('id').match(new RegExp(encodedTitle, 'i'))) {
			$(this).show();
		}
	});
});

