// timer ids
var show_banner_timer = -1;
var rotate_banner_timer = -1;

// banner to display
var current_banner = 1;
var next_banner = -1;

// quantity and width of banners
var banner_count = -1;
var banner_height = 150;

// animation variables
var banner_dx = 8;
var frame_delay = 40;
var rotate_delay = 5000;

function initBanners(count)
{
	banner_count = count;
	
	current_banner = Math.floor(Math.random() * banner_count) + 1;

	// position banners
	for (i = 1; i <= banner_count; i++)
	{
		if (i == current_banner)
			document.getElementById('banner' + i).style.top = '0px';
		else
			document.getElementById('banner' + i).style.top = banner_height + 'px';
		
		document.getElementById('banner' + i).style.display = 'block';
	}

	timerRotateBannerStart();
}

function rotateBanner()
{
	timerShowBannerStart(current_banner % banner_count + 1);
}

function showBanner()
{
	if (typeof showBanner.i == 'undefined') showBanner.i = 0;
	if (typeof showBanner.j == 'undefined') showBanner.j = -banner_height;
	
	showBanner.i += banner_dx;
	showBanner.j += banner_dx;
	
	if (showBanner.i >= banner_height) {
		document.getElementById('banner' + current_banner).style.top = '-' + banner_height + 'px';
		document.getElementById('banner' + next_banner).style.top = '0px';
		
		showBanner.i = 0;
		showBanner.j = -banner_height;
		
		timerShowBannerStop();
		current_banner = next_banner;
		return;
	}
	
	document.getElementById('banner' + current_banner).style.top = showBanner.i + 'px';
	document.getElementById('banner' + next_banner).style.top = showBanner.j + 'px';
}

function timerRotateBannerStart()
{
	if (rotate_banner_timer == -1)
	{
		rotate_banner_timer = window.setInterval(rotateBanner, rotate_delay);
		return true;
	}
	
	return false;
}

function timerRotateBannerStop()
{
	window.clearInterval(rotate_banner_timer);
	rotate_banner_timer = -1;
}

function timerShowBannerStart(num)
{
	if ((show_banner_timer == -1) && (current_banner != num)) {
		next_banner = num;
		show_banner_timer = window.setInterval(showBanner, frame_delay);
		return true;
	}
	return false;
}

function timerShowBannerStop()
{
	window.clearInterval(show_banner_timer);
	show_banner_timer = -1;
}
