function updateOrientation() {
	
	var width  = $(window).width();
	var height = $(window).height();
	var height2 = $(document).height();
	
	var fakX = width / 1600;
	var fakY = height2 / 1131;

	if( fakX > fakY ) {
		$("#bg").height( 1131 * fakX );
		$("#bg").width( width );
	} else {
		$("#bg").height( height2 );
		$("#bg").width( 1600 * fakY );
	}

	var navHeight = $("#navigation").outerHeight();
	var blueContainerHeight = $("#content").outerHeight();
	
	positionFromTop = ( blueContainerHeight + navHeight > height - 100 );
	
	if( $("#blueContainer").html() ) {
		$("#blueContainer").height( blueContainerHeight );
		if(positionFromTop) {
			$("#blueContainer").css( 'bottom', '' );
			$("#blueContainer").css( 'top', navHeight+100+"px" );
		} else {
			$("#blueContainer").css( 'bottom', '0px' );
			$("#blueContainer").css( 'top', '' );
		}
		if( $("#subNav").html() ) {
			$("#subNav").css( 'top', ( $("#blueContainer").offset().top - $("#subNav").outerHeight() ) );
		}
	}
	if( $("#blueContainerOpaque").html() ) {
		$("#blueContainerOpaque").height( blueContainerHeight );
		if(positionFromTop) {
			$("#blueContainerOpaque").css( 'bottom', '' );
			$("#blueContainerOpaque").css( 'top', navHeight+100+"px" );
		} else {
			$("#blueContainerOpaque").css( 'bottom', '0px' );
			$("#blueContainerOpaque").css( 'top', '' );
		}
		if( $("#subNav").html() ) {
			$("#subNav").css( 'top', ( $("#blueContainerOpaque").offset().top - $("#subNav").outerHeight ) );
		}
	}

	if( $("body").has( $("#blueImage") ) ) {
		$("#blueImage").width( $("#bg").width() );
		$("#blueImage").height( $("#bg").height() );
		if(positionFromTop) {
			$("#blueImage").css( 'top', -$('#content').offset().top )
		} else {
			$("#blueImage").css( 'top', -(height-blueContainerHeight) )
		}
	}
	
	$("#bgContainer").height( height );
	
/*
 	$(document).ready( function () {
		setTimeout(function(){window.scrollTo(0, 1);}, 1000);
	});
*/	
}

function navSetup() {
	$('#navigation a').each(function() {
			if(!$(this).hasClass('selected')) {
				$(this).hover(function() {
					$(this).toggleClass('selected');
				}, function() {
					$(this).toggleClass('selected');
				});
			}
		});
}

$(window).load( function () {
		setTimeout(function(){ updateOrientation(); }, 300);
		setTimeout(function(){
			updateOrientation();
			$("#bg").animate( {opacity:1}, 500 );
			$("#blueImage").animate( {opacity:1}, 500 );
		}, 500);
	} );
$(window).resize( updateOrientation );
