$(document).ready( function() {

	// Featured Box
	fbTimer = setTimeout( transitionFeaturedBox, 4000 );

	$("#featurecontrols").find("span").click( function() {
		if( !$(this).hasClass( "current" ) && !cAnimating ) {
			clearTimeout( fbTimer );
			cAnimating = true;
			
			// Update Image
			$("#f"+ $(this).attr("rel")).insertBefore( ".featureditem:last" );
			$(".featureditem:last").fadeOut( 666, function() {
			
				$(this).insertBefore( ".featureditem:first" );
				$(this).show();
				cAnimating = false;
			});
			
			//$("#f"+ $(this).attr("rel")).insertAfter( ".featureditem:last" );
			
			// Update Controls
			$("#featurecontrols").find("span").removeClass( "current" );
			$(this).addClass( "current" );
		}
	});
});

var fbTimer;
var cAnimating = false;
function transitionFeaturedBox()
{
	$(".featureditem:last").each( function() {
	
		// Opacity
		var elem = $(this);
		elem.fadeOut( 666, function() {
			
			$(this).insertBefore( ".featureditem:first" );
			$(this).show();
			
			// Update Controller
			var n = $("#featurecontrols").find( ".current" ).next();
			
			if( n.attr( "rel" ) == undefined )
				n = $("#featurecontrols").find("span:first");
				
			$("#featurecontrols").find( "span" ).removeClass( "current" );
			n.addClass( "current" );
			
			fbTimer = setTimeout( transitionFeaturedBox, 4000 );
		});
	});
}
