var rotateEvery = 10000;
var rotateTimer;
var buttonLock = false;
var currentHero = 0;
/*var heroes = new Array(
	"rotating_hero_slbpak",
	"rotating_hero_mlb",
	"rotating_hero_vbb",
	"rotating_hero_fios",
	"rotating_hero_hsi"
	);
*/
var heroes = [];
var timerFadeDuration = 4000;
var clickFadeDuration = 500;

window.addEvent('domready', function() {
	
	["rotating_hero_slbpak",
	"rotating_hero_mlb",
	"rotating_hero_vbb",
	"rotating_hero_hsi",
	"rotating_hero_wifi",
	"rotating_hero_vcc"].each(function(h) {
		heroes.push($(h));	
	});
	
	$('rotating_hero_next').addEvent("click", function() {
		if(!buttonLock) {
			rotateHero(1, clickFadeDuration);
		}
	});
	$('rotating_hero_prev').addEvent("click", function() {
		if(!buttonLock) {
			rotateHero(-1, clickFadeDuration);
		}
	});
	
	setTimer();
});

function rotateHero(howMany, dur) {
	clearTimeout(rotateTimer);
	buttonLock = true;
	
	var oldHero = currentHero;
	var newHero = currentHero + howMany;
	if(newHero > heroes.length-1) newHero = 0;
	if(newHero < 0) newHero = heroes.length-1;
	heroes[newHero].setStyle('visibility','hidden');
	heroes[newHero].removeClass("hidden");
	var fadeOut = new Fx.Tween(heroes[currentHero], {
		duration:dur,
		transition:Fx.Transitions.Sine.easeInOut,
		onComplete:function() {
			heroes[oldHero].addClass("hidden");
		}
	});
	var fadeIn = new Fx.Tween(heroes[newHero], {
		duration:dur,
		transition:Fx.Transitions.Sine.easeInOut,
		onComplete:function() {
			//alert("Changing currentHero value to "+newHero);
			currentHero = newHero;
			// Open the lock
			buttonLock = false;
			// Reset the timer
			setTimer();
		}
	});
	
	fadeOut.start("opacity",[.98, .1]);
	fadeIn.start("opacity",[.1, .97]);
}

function setTimer() {
	rotateTimer = setTimeout(function() { rotateHero(1, timerFadeDuration); }, rotateEvery);
}