// Sliding Panel Timer
// Based upon http://labs.adobe.com/technologies/spry/demos/gallery/gallery.js and feedback within the Adobe Spry forum

// Global Variables
var gPanelShowInterval;
if (gPanelShowInterval == undefined)
	gPanelShowInterval = 10000; // msecs between panels
	
var gAutoStartPanelShow;
if (gAutoStartPanelShow == undefined)
	gAutoStartPanelShow = true;
	
var gPanelShowOn = false;
var gPanelShowTimer = null;

if (gAutoStartPanelShow)
	startPanelShow();


// Setup timer event to change to next panel
function setPanelShowTimer() {
	
	killPanelShowTimer(); // Kill any previous timer event
	gPanelShowTimer = setTimeout( function() { gPanelShowTimer = null; advanceToNextPanel(); }, gPanelShowInterval);

}

// Kill any previous timer event 
function killPanelShowTimer() {
	
	if (gPanelShowTimer)
		clearTimeout(gPanelShowTimer);
	gPanelShowTimer = null;

}

// Display next sliding panel on rotational loop
function advanceToNextPanel() {
	
	if (!sp1) return;
	var tPanels = sp1.getContentPanels().length - 1;  // Total Panels
	var cPanel = sp1.getContentPanelIndex(sp1.currentPanel); // Index of currently displayed panel
	
	if (tPanels == cPanel)
		sp1.showFirstPanel();  // Return to the first panel
	else
		sp1.showNextPanel();  // Show the next sliding panel
	setPanelShowTimer();	
}

// Start the timed sliding panel
function startPanelShow() {
	
	gPanelShowOn = true;
	setPanelShowTimer();
}
