$(document).ready( function() {

	// Shadows
	$(".body_bkg").each( function() {
	
		$(this).height( $(this).parent().height() );
		$(this).show();
	
	});
	
	$(".adjust_height").each( function() {
		$(this).height( $(this).parent().height() );
	});
	
	// Menu
	$("#menu_items img").each( function() {
	
		// Item?
		var rel = $(this).attr( "rel" );
		if( rel != undefined )
		{
			// Show submenu?
			if( rel != "" )
			{
				$(this).mouseenter( function() {
					$(".submenu").hide();
					$("#sm_"+rel).show();
					
					$(".adjust_height").each( function() {
						$(this).height( $(this).parent().height() );
					});
				});
			}
			else
			{
				$(this).mouseenter( function() {
					$(".submenu").hide();
				});
			}
		}
	
	});
	$(".submenu").mouseleave( function() { $(this).hide(); } );
	
	// Header Images
	headerTimer = setTimeout( "doHeaderTransition()", 3000 );
	
	// Twitter?
	if( $("#twitter").length > 0 )
		twitterTimer = setTimeout( "checkTwitter()", 1000 );
});

var headerTimer;
var twitterTimer;
var twitterCheck=0;
function doHeaderTransition()
{
	$("#header_images div:last").fadeOut( 3000, function() { 
		//alert( "done" );
		$(this).insertBefore( $("#header_images div:first") );
		$(this).show();
		headerTimer = setTimeout( "doHeaderTransition()", 3000 );
	});
}

function checkTwitter()
{
	//alert( $("#content" ).height() );
	twitterCheck++;
	if( $(".twtr-tweet" ).length > 0 )
	{
		$(".twtr-timeline" ).animate( { height: $(".twtr-tweets" ).height() }, 1, function() { $("#body").height( $("#content").height() ); } );
		$(".twtr-reference-tweet").html( ' ' );
		//$(".twtr-timeline").height( $(".twtr-tweets" ).height() );
	}
	else
	{
		if( twitterCheck < 10 )
		{
			$(".twtr-reference-tweet").html( '<div class="pad10 center">loading, please be patient...</div>' );
			$(".twtr-timeline" ).animate( { height: $(".twtr-tweets" ).height() }, 1000 );
			twitterTimer = setTimeout( "checkTwitter()", 1000 );
		}
		else
		{
			$(".twtr-reference-tweet").html( '<div class="pad10 center">... twitter seems to be down ...</div>' );
			$(".twtr-timeline" ).animate( { height: $(".twtr-tweets" ).height() }, 1000 );
		}
	}
}
