/**
 * Jquery for Header Banner
 *
 * All preset variables are in Seconds.
 * 
 * @author		Dave Newson <dave@4mation.com.au>
 * @version		20100609
 */

 
// Random delay between image swap
var delay_max = 1;
var delay_min = 4;

// How long the fade should take
var fade_duration = 0.5;

var images = [];

$(document).ready( function()
{
	// First visit? We don't want to fade in on every page load.
	if ( document.referrer == '' || document.referrer.indexOf(window.location.hostname) < 0 )
	{
		// Fade-in the logo
		$('#header_banner .logo').hide();
		$('#header_banner .logo').fadeIn( fade_duration * 1000);
	}

	FadeInImages();
	
	delay = delay_max * 1000;
	
	// Delay until next cycle
	setTimeout( function()
	{
		RandomImageSwap( -1 );
	}, delay);
});

/**
 * Fade In Images
 *
 * Fades in a random selection of images
 */
 
function FadeInImages()
{
	$('#header_banner .c div').each( function()
	{
		$(this).css('background-color', '#fff');
	});

	// Get images array
	var available_images = images.slice(0);
	
	$('#header_banner .c').each( function ()
	{                     
		// Pick a random image
		var choice = Rand(available_images.length)
		
		// Set the background image
		$(this).css('background-image', 'url('+available_images[ choice ]+')' );
		
		// Delete option
		available_images.splice( choice, 1 );
	});
	
	// Fade in each image
	$('#header_banner .c div').each( function()
	{
		$(this).fadeOut( fade_duration *1000 );
	});
}


/**
 * Random Image Swap
 *
 * Self-calling function to swap a random image.
 */

function RandomImageSwap( last_chosen )
{
	// Get an array of the avilable boxes
	var options = new Array();
	
	$('#header_banner').children().each( function ( i )
	{
		options[ i ] = $(this);
	});
	
	// Remove last chosen option
	// :BUG: Doing this can cause duplicate image bug.
	//options.splice(last_chosen, 1);
	
	// Pick a random box
	last_chosen = Rand(options.length);
	var box = options[ last_chosen ];
	
	// Get all the images and exclude any that are in use
	var available_images = images.slice(0);
	
	for (i in options)
	{
		for (j in available_images)
		{
			if ( options[i].css('background-image').indexOf( available_images[j] ) >= 0 )
			{
				available_images.splice(j, 1);
				j--;	// Compensates for the actions of Splice.
			}
		}
	}

	var top = $(box).children('div');
	
	// Set top to old image
	$(top).css('background-image', $(box).css('background-image') );
	
	// Set top opacity to 100
	$(top).show();
	
	// Set background to new image
	$(box).css('background-image', 'url('+available_images[ Rand(available_images.length) ]+')' );
	
	delete available_images;
	
	// Fade top image
	$(top).fadeOut(fade_duration * 1000);
	
	// Get time until next swap
	delay = Rand(delay_min, delay_max);
	delay = delay * 1000;
	
	// Delay until next cycle
	setTimeout( function()
	{
		RandomImageSwap( last_chosen );
	}, delay);
	
}

function Rand( arg1, arg2 )
{
	if (arg2)
	{
		return Math.floor(Math.random() * (arg2 - arg1)) + arg1;
	}
	else
	{
		return Math.floor(Math.random() * arg1);
	}
}

