//////////////////////////////////////////////////////////////////////////////////////////////
// Banner Fader v1.0
// Andrew Pettican (May 2009)
//////////////////////////////////////////////////////////////////////////////////////////////

/**
 * Constants/Globals
 */
var bf_fade_duration = 3000;	// The amount of time a fade should last
var bg_pause_duration = 3000;	// The amount of time to pause the banner at full opacity
var bf_banner_index = 0;		// The current banner we are displaying
var bf_banner_home_array = Array();	// The array of banner images to cycle over
var bf_banner_nav_array = Array();
var bf_fade_timer = null;


/**
 * Initialise
 */
$(document).ready(function()
{
	// Get the id's of all .banner_home_img objects and dump em into bf_banner_home_array
	var banner_imgs = $(".banner_home_img");
	if(banner_imgs.length > 0)
	{
		for(i=0; i<banner_imgs.length; i++)
		{
			bf_banner_home_array[i] = banner_imgs[i].id;
			
			// Hide all banners except for the first one
			if(i>0) {
				$("#"+bf_banner_home_array[i]).css("opacity", "0");
			}
		}
	}
	
	// Get the id's of all .banner_nav_img objects and dump em into bf_banner_nav_array
	var banner_nav_imgs = $(".banner_nav_img");
	if(banner_nav_imgs.length > 0)
	{
		for(i=0; i<banner_nav_imgs.length; i++)
		{
			bf_banner_nav_array[i] = banner_nav_imgs[i].id;
			
			// Hide all banners except for the first one
			if(i>0) {
				$("#"+bf_banner_nav_array[i]).css("opacity", "0");
			}
		}
	}
	
	// Begin cycling over the banners
	if(bf_banner_nav_array.length > 0 && bf_banner_nav_array.length == bf_banner_home_array.length) {
		bf_fade_timer = setTimeout( function(){ cycle_banners(bf_banner_index+1); bf_fade_timer = null; }, bg_pause_duration );
	}
});


/**
 * Cycle the banner images
 */
function cycle_banners(bf_banner_index)
{
	// Determine which banner we are fading in/out
	var fade_in_banner_index = bf_banner_index;
	var fade_out_banner_index = bf_banner_index - 1;
	
	// Fading in the first banner of the array?
	if(fade_in_banner_index < 0 || fade_in_banner_index > bf_banner_home_array.length-1) {
		fade_in_banner_index = 0;
	}
	
	// Fading out the last banner of the array?
	if(fade_out_banner_index < 0) {
		fade_out_banner_index = bf_banner_home_array.length-1;
	}
	
	// Check that this is NOT the first banner we are cycling to (i.e. bf_banner_index is above 0)
	var fade_out_banner = (bf_banner_index >= 0);
	if(bf_banner_index >= 0)
	{
		// Fade out
		$("#"+bf_banner_nav_array[fade_out_banner_index]).animate({opacity:0}, bf_fade_duration);
		$("#"+bf_banner_home_array[fade_out_banner_index]).animate({opacity:0}, bf_fade_duration);
	}
	
	// Fade in
	$("#"+bf_banner_nav_array[fade_in_banner_index]).animate({opacity:1}, bf_fade_duration);
	$("#"+bf_banner_home_array[fade_in_banner_index]).animate({opacity:1}, bf_fade_duration, function() {
		// Fade complete
		bf_fade_timer = setTimeout( function(){ cycle_banners(fade_in_banner_index+1); bf_fade_timer = null; }, bg_pause_duration );
		
		// Set the background colour of the splash container after the first image has loaded 
		// (not gonna bother with this for now)
		/*
		if(bf_banner_index < 0) {
			$("#splash").css("background-color", "#7F7F7F");
		}
		*/
	});
}
