//Top Story content cycling
// globalize variables
var contentCycleInterval;

// Content Cycling Variables
var cycleDelay = 3000;			// timeInMilliseconds
var currentContentPiece = 1;	// defaults to first one
var maxNumContentPieces;	// max number of content/stories

// Preload Images
// auto-cycle controls
previousImage = new Image(18,18);	previousImage.src = "./images/controlsPrev.gif";
nextImage = new Image(18,18);		nextImage.src = "./images/controlsNext.gif";
playImage = new Image(18,18);		playImage.src = "./images/controlsPlay.gif";
pauseImage = new Image(18,18);		pauseImage.src = "./images/controlsPause.gif";

var moduleName = "topStories";
var contentCycleInterval;
var pauseValue = 0;
var initialPauseValue = 0;

// Navigation Settings
// nav delay
var navDelay = 500;


// Content Cycling Functions
function adjacentContent(nearContentLoc)
{ /* return values for content ID for prev/next */
	if ( nearContentLoc == "prev" )
	{ // previous content
		decrementContentPieces();
	} else if ( nearContentLoc == "next" )
	{ // next content
		incrementContentPieces();
	} // end if
	return currentContentPiece;
} // end function
/*
function autoCycleContent(currentContentPiece)
{ 
	// call auto-cycle function with delay
	contentCycleInterval = window.setInterval('cycleContent(moduleName, currentContentPiece)', cycleDelay);
} // end function
*/
function autoCycleContent(currentContentPiece)
{ /* calls the auto cycle function with the delay */
	// call auto-cycle function with delay
	if (initialPauseValue == 0)
	{ // default to auto cycle
		contentCycleInterval = window.setInterval('cycleContent(moduleName, currentContentPiece)', cycleDelay);
	} else
	{ // defalut to pause
		pauseCycleContent('topStories', 'perm');
		initialPauseValue = 0;
	} // end if
} // end function

function cycleContent(moduleName, currentContentPiece)
{ /* auto-cycles the content */
	// increment content piece
	currentContentPiece = incrementContentPieces();
	// hide all content
	for( i = 1; i <= maxNumContentPieces; i++ ) {
		document.getElementById("story" + i).style.display = 'none';
	} // end for
	// display current content
	document.getElementById("story" + currentContentPiece).style.display = 'block';
	decorateControls();
} // end function

function pauseCycleContent(moduleName, pauseType)
{ /* toggles pausing the auto-cyle */
	if ( pauseType == "temp" )
	{ // specific content selected
		window.clearInterval(contentCycleInterval);
		pauseValue = 1;
		document.getElementById("pauseImage").src = pauseImage.src;
	} else
	{ // pause button selected
		if ( pauseValue == 0 )
		{ // not currently paused
			// pause auto-cycle
			window.clearInterval(contentCycleInterval);
			pauseValue = 1;
			document.getElementById("pauseImage").src = playImage.src;
		} else
		{ // currently paused
			// resume auto-cycle
			autoCycleContent(moduleName, currentContentPiece);
			document.getElementById("pauseImage").src = pauseImage.src;
			pauseValue = 0;
		} // end if
	} // end if
} // end function

function selectContent(moduleName, storyID)
{ /* displays the content selected and hides the rest */
	if (( storyID == "prev" ) || ( storyID == "next" ))
	{ // previous or next is selected
		storyID =  adjacentContent(storyID);
	} // end if
	for( i = 1; i <= maxNumContentPieces; i++ )
	{ // hide all content
		document.getElementById("story" + i).style.display = 'none';
	} // end for
	// display selected content
	document.getElementById("story" + storyID).style.display = 'block';
	// pause auto cycle
	pauseCycleContent(moduleName, "temp");
	document.getElementById("pauseImage").src = playImage.src;
	currentContentPiece = storyID;
	decorateControls();
} // end function

function decorateControls()
{ /* displays the control numbers active/passive */
	for( i = 1; i <= maxNumContentPieces; i++ )
	{ // make all control numbers passive display
		document.getElementById("controls" + i).className = 'passiveControls';
	} // end for
	// make current control number active
	document.getElementById("controls" + currentContentPiece).className = 'activeControls';
} // end function

function incrementContentPieces()
{ /* increments the content pieces */
	currentContentPiece++;
	if ( currentContentPiece > maxNumContentPieces )
	{ // check to see if increments past max and reset
		currentContentPiece = 1;
	} // end if
	return currentContentPiece;
} // end function

function decrementContentPieces()
{ /* decrements the content pieces */
	currentContentPiece--;
	if ( currentContentPiece < 1 )
	{ // check to see if increments past min and reset
		currentContentPiece = maxNumContentPieces;
	} // end if
	return currentContentPiece;
} // end function
		

