/*
Rotate content automatically or with controls
written by jkr at Propeller Media Works, 2007-07-31
inspiration from code of uknown source
*/
loadRotatorControls=function(){
	// --------------------------------------------------------------------

	// change the pagination controls to activate current
	updatePaginationControls = function(contentNumber) {
		// inactivate all first
		for(i=1;i<=numQueuedContentDivs;i++) {
			paginationControl=document.getElementById(rotatorPaginationLinksRoot+i);
			paginationControl.className = "";
		} // end for
		// now re-activate current
		//if (contentNumber<1 || contentNumber>4) {alert(contentNumber);}
		currentPaginationControl=document.getElementById(rotatorPaginationLinksRoot+contentNumber);
		currentPaginationControl.className = "current";
	} // end function
	// --------------------------------------------------------------------
	
	// change the play control text
	updatePlayControl = function() {
		if (true == rotatorIsRotating) {
			rotatorPlayPauseControlId.innerHTML = 'pause';
		} else {
			rotatorPlayPauseControlId.innerHTML = 'play';
		} // end else
	} // end function
	// --------------------------------------------------------------------

	// swap content of the container div
	swapRotatorContent = function(contentNumber) {
		currentContentNumber = contentNumber;
		updatePaginationControls(contentNumber);
		rotatorContentContainer.innerHTML=queuedContentDivs[currentContentNumber].innerHTML;
	} // end swapcontent
	// --------------------------------------------------------------------

	// get the next content number
	getNextContentNumber = function() {
		//alert(currentContentNumber);
		if(currentContentNumber>=numQueuedContentDivs) {
			return 1; // if we would pass the end of the array, return to start (1-based
		} // end if too high
		else {
			return currentContentNumber + 1; // simply increment
		} // end else
	} // end function
	// --------------------------------------------------------------------
	
	// get previous content number
	getPreviousContentNumber = function() {
		if(currentContentNumber<=1) {
			return numQueuedContentDivs; // go to the last element
		} //end if too low
		else {
			return currentContentNumber - 1; // simply subtract
		} // end else
	} // end function
	// --------------------------------------------------------------------
	
	// rotate content automatically
	autoRotate = function(){
		newContentNumber = getNextContentNumber();
		swapRotatorContent(newContentNumber);
	} // end function
	// --------------------------------------------------------------------

	// kick off autorotation
	startRotation = function(timeBetweenSwaps) {
		rotateScheduler = setInterval('autoRotate()', timeBetweenSwaps); 
		rotatorIsRotating = true;
		updatePlayControl();
	}
	// --------------------------------------------------------------------

	// stop autorotation
	stopRotation = function(){
		if (null != rotateScheduler) {
			clearInterval(rotateScheduler);
		}
		rotatorIsRotating = false;
		updatePlayControl();
	} // end function
	// --------------------------------------------------------------------
	
	// switch playing -- if playing, stop.  else start
	toggleRotation = function() {
		if (true == rotatorIsRotating) {
			stopRotation();
		} else {
			startRotation(rotationTimeDelay);
		} // end if
	} // end function
	// --------------------------------------------------------------------
	
	// goto a particular content item and stop there
	gotoAndStop = function(contentNumber) {
		stopRotation();
		swapRotatorContent(contentNumber);
	} // end function
	// --------------------------------------------------------------------

	// goto a particular content item and stop there
	gotoAndPlay = function(contentNumber) {
		stopRotation();
		swapRotatorContent(contentNumber);
		startRotation(rotationTimeDelay);
	} // end function
	// --------------------------------------------------------------------
	
	// get the divs representing the queued content
	getQueuedContentDivs = function(queuedContainerNodeId, classNameToMatch) {
		// get children divs of the hidden content div
		// get an easy handle on the parent element
		queuedContainerChildren = document.getElementById(queuedContainerNodeId).childNodes;
		childrenArray = []; j = 0;
		for (i=0; i<queuedContainerChildren.length; i++) {
			classRegex = new RegExp('/\b' + classNameToMatch + '\b/');
			if (1 == queuedContainerChildren[i].nodeType) {
				// element nodes
				j++;
				childrenArray[j]=queuedContainerChildren[i];
			} // end if is text node
		} // end for
		return childrenArray;
	}  // end function
	// --------------------------------------------------------------------
	/*
	// gets all <div> elements
	allDivs=document.getElementsByTagName('div');
	queuedContentDivs=[];
	// makes array of <div> elements with specified class name as source content
	// NOTICE -- THIS ARRAY WILL START AT *1*, NOT ZERO
	j = 1; // counter for content divs so that we don't get a zero-based array
	for(i=0;i<allDivs.length;i++) {
		if(/\bqueued_rotator_content_item\b/.test(allDivs[i].className)){
			queuedContentDivs[j]=allDivs[i];
			j++;
		} // end if class matches
	} // end for all divs
	*/
	queuedContentDivs = getQueuedContentDivs('queued_rotator_content', 'rotator_content_item'); // div containing hidden divs
	numQueuedContentDivs = queuedContentDivs.length - 1; // account for the fact that it is not zero-based
	// get the element that we swap content into
	var rotatorContentContainer=document.getElementById('rotator_content');
	// play/pause element
	var rotatorPlayPauseControlId = document.getElementById('rotator_play_pause_control');
	var rotatorPaginationLinksRoot = "rotator_pagination_link_";
	var rotateScheduler = null;
	var rotatorIsRotating = false;
	var currentContentNumber=0; // start display at first item
	var rotationTimeDelay = 5*1000; // time in milliseconds
	// load the first content
	swapRotatorContent(1);
	if (numQueuedContentDivs > 1) { // only rotate if more than one item
		// set rotation spinning
		startRotation(rotationTimeDelay);
	} else {
		stopRotation(); // it's already stopped, but set the button condition correctly
	} // end if
} // end loadRotatorControls