
/*
** Respond to DOM load
*/
$(function() {
	// Init masthead carousel if it exists on this page
	if ( $("#masthead.carousel").length > 0 ) {
		mastheadCarouselInit();
	}
});

/*
** Friendly log function
*/
function fLog( message ) {
	// Look for precence of console
	if ( ( typeof( console ) == "object" ) && ( typeof( console.log ) == "function" ) ) {
		console.log( message );
	}
}

/*
** Functions for the masthead carousel
*/
var mastheadCarouselInterval; // defined as setInterval in mastheadCarouselStart()
var mastheadCarouselIntervalTime = 6500;
var mastheadCarouselIntervalSpeed = "slow";
var mastheadCarouselIntervalIndex = 0;

function mastheadCarouselInit() {
	fLog( "mastheadCarousel:init" );
	//	Hide all instances following the first instance
	$("#masthead.carousel .images-container .image:gt(0)").css("display","none");
	$("#masthead.carousel .content-container .content:gt(0)").css("display","none");
	$("#masthead.carousel .btn-next").css("display","block");
	$("#masthead.carousel .btn-previous").css("display","block");
	// Bind mouse events
	$("#masthead.carousel .images-container").bind( "mouseenter", function(){ mastheadCarouselStop() } ).bind( "mouseleave", function(){ mastheadCarouselStart() } );
	$("#masthead.carousel .content-container").bind( "mouseenter", function(){ mastheadCarouselStop() } ).bind( "mouseleave", function(){ mastheadCarouselStart() } );
	$("#masthead.carousel .btn-next").bind( "click", function(){ mastheadCarouselButtonNextCallback() } );
	$("#masthead.carousel .btn-previous").bind( "click", function(){ mastheadCarouselButtonPreviousCallback() } );
	// Start playback
	mastheadCarouselStart()
}

function mastheadCarouselStart( now ) {
	fLog( "mastheadCarousel:start[now:" + now + "]" );
	// Skip delay if "now" is defined
	if ( now ) {
		mastheadCarouselLoadNext();
	}
	// Start playback
	mastheadCarouselInterval = setInterval( mastheadCarouselLoadNext, mastheadCarouselIntervalTime );
}

function mastheadCarouselStop() {
	fLog( "mastheadCarousel:stop" );
	// Stop playback
	clearInterval( mastheadCarouselInterval );
}

function mastheadCarouselLoad( index ) {
	fLog( "mastheadCarousel:load[index:" + index + "]" );
	// Crossfade images
	$("#masthead.carousel .images-container .image:eq(" + mastheadCarouselIntervalIndex + ")").fadeOut( mastheadCarouselIntervalSpeed );
	$("#masthead.carousel .images-container .image:eq(" + index + ")").fadeIn( mastheadCarouselIntervalSpeed );
	// Crossfade content
	$("#masthead.carousel .content-container .content:eq(" + mastheadCarouselIntervalIndex + ")").fadeOut( mastheadCarouselIntervalSpeed );
	$("#masthead.carousel .content-container .content:eq(" + index + ")").fadeIn( mastheadCarouselIntervalSpeed );
	// Save new interval index
	mastheadCarouselIntervalIndex = index;
}

function mastheadCarouselLoadNext() {
	fLog( "mastheadCarousel:loadNext" );
	var index;
	// Loop if on last item
	if ( mastheadCarouselIntervalIndex >= $("#masthead.carousel .content-container .content").length-1 ) {
		index = 0;
	} else {
		index = mastheadCarouselIntervalIndex + 1;
	}
	mastheadCarouselLoad( index );
};

function mastheadCarouselLoadPrevious() {
	fLog( "mastheadCarousel:loadPrevious" );
	var index;
	// Loop if on first item
	if ( mastheadCarouselIntervalIndex == 0 ) {
		index = $("#masthead.carousel .content-container .content").length-1;
	} else {
		index = mastheadCarouselIntervalIndex - 1;
	}
	mastheadCarouselLoad( index );
};

function mastheadCarouselButtonNextCallback() {
	mastheadCarouselStop();
	mastheadCarouselLoadNext();
}

function mastheadCarouselButtonPreviousCallback() {
	mastheadCarouselStop();
	mastheadCarouselLoadPrevious();
}


