var banner_current = "0";
var ctrl_html = '<ul id="rotating_banner_ctrl">';
var banner_rotating = false;
var rotateTimer;

rotating_banners.each(function(banner, rbi){
	var banner_html = '<div id="banner_'+rbi+'" class="banner_item"><a href="'+banner.link+'" title="'+banner.text+'"><img src="'+banner.src+'" alt="'+banner.text+'" /></a></div>';	
	new Insertion.Top('rotating_banner', banner_html);
	$('banner_'+rbi).hide();
	
	var rbi_friendly = parseInt(rbi) + 1;
	
	ctrl_html = ctrl_html + '<li id="ctrl_item_'+rbi+'" onmouseover="banner_switch('+rbi+'); resetTimer(); return false;">' + rbi_friendly + '</li>';
	
});

ctrl_html = ctrl_html + '</ul>';
new Insertion.Bottom('rotating_banner', ctrl_html);

$('banner_0').show();
$('ctrl_item_0').addClassName('current');

autoRotate();

function banner_switch(to){
	if (banner_current == to || banner_rotating == true) return;
	var banner_current_old = banner_current;
	banner_current = to;
	
	$('banner_'+banner_current_old).hide();
//	Effect.Appear('banner_'+to, {duration: 0.3, beforeStart: function(){banner_rotating = true}, afterFinish: function(){banner_rotating = false} });
	
//	$('banner_'+banner_current_old).style.filter="blendTrans(duration=2)";
//	$('banner_'+banner_current_old).filters.blendTrans.apply();
//	$('banner_'+banner_current_old).filters.blendTrans.play();
//	$('banner_'+banner_current_old).hide();
//	document.images.myimg.src=newImage.src;
	
	$('banner_'+to).show();
	
	$('ctrl_item_'+banner_current_old).removeClassName('current');
	$('ctrl_item_'+to).addClassName('current');
}

function rotate(){
	var banner_i;
	banner_i = parseInt(banner_current);
	if (banner_i + 1 < rotating_banners.length) banner_next = banner_i + 1;
	else banner_next = 0;
	
	return banner_switch(banner_next);
}


function autoRotate(){
	rotateTimer = setTimeout(function(){rotate(); autoRotate();}, 2000);
}

function resetTimer(){
	clearTimeout(rotateTimer);
	autoRotate();
}
