var nav_current = parent_nav;
$(function() {
	// From window.resize for quicker display.
	$('#emblem').offset({top: $(window).height() / 2 - 222});
	$('#nav_ul').offset({left: $(window).width() / 2 - 300});

	// navigation
	nav_sub_fadein();

	var timeout = null;
	$('#nav_ul a').mouseover(function() {
		clearTimeout(timeout);
		if(nav_current == this.id.substr(3)) return;
		nav_sub_fadeout();
		nav_current = this.id.substr(3);
		$('#nav_ul li.current').toggleClass('current');
		$(this).parent().toggleClass('current');
		nav_sub_fadein();
	}).mouseout(function() {
		timeout = window.setTimeout('nav_reset()', 5000);
	});

	// sub navigation
	$('div.nav_div').mouseover(function() {
		clearTimeout(timeout);
	}).mouseout(function() {
		timeout = window.setTimeout('nav_reset()', 5000);
	});

	// class overlay pictures
	$('img.overlay').mouseover(function() {
		$(this).fadeTo(300, 1);
	}).mouseout(function() {
		$(this).fadeTo(300, 0);
	});

	// gallery
	$('#gallery img').click(function() {
		var img = new Image();
		$(img).load(function() {
			this.removeAttribute('width'); /* IE8 Fix */
			this.removeAttribute('height');
			$('#image').hide().html(this).fadeIn()
			setTimeout('image_offset()', 10);
		}).attr('src', $(this).attr('src').replace('thumbs/', ''));
	});

	// gallery popup
	$('#image').click(function() {
		$('#image').fadeOut();
	});
});

function nav_sub_fadeout() {
	$('#nav'+ nav_current + '_sub').toggleClass('nav_adiv').fadeOut(300);
}
function nav_sub_fadein() {
	$('#nav'+ nav_current +'_sub').fadeTo(300, 1).toggleClass('nav_adiv');
}

function nav_reset() {
	if(nav_current == parent_nav) return;
	nav_sub_fadeout();
	nav_current = parent_nav;
	$('#nav_ul li.current').toggleClass('current');
	$('#nav'+ parent_nav).parent().toggleClass('current');
	nav_sub_fadein();
}

// webkit jquery workaround: cache hits don't load image dimensions
function image_offset() {
	var img = $('#image img');
	$(img).css({
		'max-height': $(window).height() - 150,
		'max-width': $(window).width() - 20
	});
	$('#image').offset({
		top: $(window).height() / 2 + $(window).scrollTop() - (img.height() / 2),
		left: $(window).width() / 2 + $(window).scrollLeft() - (img.width() / 2)
	});
}

$(window).resize(function() {
	$('#emblem').offset({top: $(window).height() / 2 - 222});
	$('#nav_ul').offset({left: $(window).width() / 2 - 300});
	$('img.overlay').each(function() {
		$(this).offset($(this).prev().offset());
	});
	image_offset();
});

$(window).load(function() {
	$(window).resize();
});

