﻿var eproList, eproListIndex = 0, eproCurr = null, eproIntervalID, displaySeconds = 7;
$(document).ready(function() {
	// Grab the contents of the list
	eproList = $('#ClientListContainer li');
	if (eproList && eproList.length > 0) {
		// Wire buttons
		$('#ClientContainerPrev').click(ClientListNext)
									.mouseenter(ClientListTimerStop)
									.mouseleave(ClientListTimerStart);
		$('#ClientContainerNext').click(ClientListPrev)
									.mouseenter(ClientListTimerStop)
									.mouseleave(ClientListTimerStart);
		
		// Show first item
		ClientListFadeTo(0);
		
		// Start timer
		ClientListTimerStart();
	}
});

function ClientListTimerStart() {
	eproIntervalID = setInterval(ClientListNext, displaySeconds * 1000);
}

function ClientListTimerStop() {
	if (eproIntervalID) {
		clearInterval(eproIntervalID);
	}
	eproIntervalID = null;
}

function ClientListNext() {
	// Calculate next index
	eproListIndex += 1;
	if (eproListIndex >= eproList.length) {
		eproListIndex = 0;
	}
	
	// Show it!
	ClientListFadeTo(eproListIndex);
}

function ClientListPrev() {
	// Calculate next index
	eproListIndex -= 1;
	if (eproListIndex < 0) {
		eproListIndex = eproList.length - 1;
	}
	
	// Show it!
	ClientListFadeTo(eproListIndex);
}

function ClientListFadeTo(index) {
	// Fade out the existing item
	if (eproCurr != null) {
		eproCurr.fadeOut(600);
	}
	
	// Get the new item && fade it in.
	eproCurr = $(eproList[index]).fadeIn(600);
}
