/*
ESmith 2007-02-17
Modified content_rotator.js
by rewriting swapRotatorContent() to hide/show div visibility rather than swap innerHTML.
Queued divs are initially hidden and absolutely positioned in the same place. 
*/

/*
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
    	// ESmith 2008-02-16 Modified to swap visibility, not the content
	swapRotatorContent = function(contentNumber) {
	    	oldNumber = currentContentNumber;
		currentContentNumber = contentNumber;
	    
		updatePaginationControls(contentNumber);

	    	newContent = queuedContentDivs[currentContentNumber];

	    	newContent.style.visibility = 'visible';

		if (oldNumber != contentNumber && oldNumber != 0) {
			oldContent = queuedContentDivs[oldNumber];
			oldContent.style.visibility = 'hidden';

		}
		    
	} // 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(){
		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
	*/

    // ESmith 2007-02-24
    // Hack error check - don't bother if this div doesn't exist
    if ( !document.getElementById('queued_rotator_content')) {
	return;
    }

	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 rotatorIsRotating = false;
	var currentContentNumber=0; // start display at first item
	var rotationTimeDelay = 5*1000;


	// load the first content
	swapRotatorContent(1);
	// set rotation spinning
	startRotation(rotationTimeDelay);


} // end loadRotatorControls