var reelIndex = 0;
var reelCount;
var timeoutID;

function prevClickHandler(e) {
	e.preventDefault();
	
	//console.log("promoBackClick");
	
	if ( reelIndex <= 0 )
	{
		reelIndex = reelCount-1;
	}
	else
	{
		reelIndex--;
	}
	
	swapPromo();
}

function nextClickHandler(e) {
	e.preventDefault();
	
	//console.log("promoNextClick");
	
	nextPromo();
}

function nextPromo() {
	if ( reelIndex >= reelCount-1 )
	{
		reelIndex = 0;
	}
	else
	{
		reelIndex++;
	}
	
	swapPromo();
}

function swapPromo() {
	if ( currentReel != null )
	{
		currentReel.fadeOut( 300, outroComplete );
	}
	
	currentReel = $(".reel").eq( reelIndex );
	
	startTimeout();
}

function outroComplete()
{
	$(this).hide();
	currentReel.fadeIn();
}


function startTimeout()
{
	stopTimeout();
	
	//timeoutID = window.setTimeout( nextPromo, 5*1000 );
}
function stopTimeout()
{
	if( timeoutID )
	{
		window.clearTimeout( timeoutID );
		timeoutID = null;
	}
}


$(function() {
		
	$("#reels-nav .prev").click(prevClickHandler);
	$("#reels-nav .next").click(nextClickHandler);
	
	var reels = $(".reel").hide();
	
	reelCount = reels.length;
	reelIndex = Math.floor( Math.random () * reelCount );
	
	currentReel = reels.eq( reelIndex );
	currentReel.fadeIn();
	//updateButtons();
	if( reelCount > 1 )
	{
		startTimeout();
	}
	
});
