// Animations for the home screen, including the banner and artwork carousel

// the banner timer
var banner_timer = null;
// the time between the banner changes
var banner_timeout = 6000;

$(function()
{
	$('.banner_item:last').addClass('active');
	$('.banner_item:not(.active)').css({opacity: 0});
	
	banner_timer = setInterval('animate_banner()', banner_timeout);
	
	// add the links wich control the banner selector
	$('#banner_selector a').click(function()
	{
		if($(this).hasClass('active'))
		{
			return false;
		}
		
		// prevent the banner timeout from calling animate_banner()
		clearInterval(banner_timer);
		
		// get the selector (ie. the #anchor)
		var num = $(this).attr('href');
		
		set_banner_image(num);
		
		// resume animation
		banner_timer = setInterval('animate_banner()', banner_timeout);
		
		return false;
	});
	
	// prevent banner animation while mouse is over the banner
	$('#banner').bind('mouseenter', function()
	{
		clearInterval(banner_timer);
	}).bind('mouseleave', function()
	{
		banner_timer = setInterval('animate_banner()', banner_timeout);
	})
});

function animate_banner()
{
	// get the next image, take a random one from the two which aren't selected
	var to_show = $('.banner_item').not('.banner_item.active').eq(Math.floor(Math.random()*2));
	
	set_banner_image(to_show);
}

function set_banner_image(selector)
{
	$('.banner_item').stop(true);
	
	// start hiding
	var hide = $('.banner_item').not(selector)
	
	hide.animate({
		opacity: 0
	}, 1000, function(){ hide.css('display', 'none'); }).removeClass('active');
	// display none is there to prevent the other hidden links from overlapping the ones shown
	
	// show
	$(selector).css('display', 'block').animate({
		opacity: 1
	}, 1000).addClass('active');
	
	// get id, to find #anchor
	var id = $(selector).attr('id');
	
	// adjust links
	$('#banner_selector a[href=#'+id+']').addClass('active');
	$('#banner_selector a').not('#banner_selector a[href=#'+id+']').removeClass('active');
}

