// JavaScript Document

var intervalID = -1;
var nCycles = 0;
var cycleLimit = 10;	// Keeps from cycling too long, esp. on buggy Firefox 
var cycleBase = null;
var cycleLast = null;
var cycleLasti = -1;
var cycleParent = null;

function cancelCycle() {
	if( intervalID >= 0 ) {
		window.clearInterval( intervalID );
		intervalID = -1;
	}
	if( cycleBase != null ) {
		if( cycleLast != null ) {
			//cycleLast.style.display = "none";
			cycleLast.style.visibility = "hidden";
			cycleLast = null;
			cycleLasti = -1;
		}
		el = document.getElementById( cycleBase+'1' );
		if( el != null )
			el.style.visibility = "visible";
		cycleBase = null;
		cycleParent = null;
		nCycles = 0;
	}
}

function containsChild(container, child) {
  var isParent = false;
  while(child != null)  {
		// Do not consider self to be contained.
		var parent = child.parentNode;
    if ((isParent = (container == parent)))
      break;
    child = parent;
  }
  return isParent;
}

function endCycle() {
	if((cycleParent == null ) || (event.toElement == null )
		 || (!containsChild( cycleParent, event.toElement ) && ( cycleParent != event.toElement ))) {
  	//alert( "End Cycle: Event.toEl: " + ((event.toElement==null)?"null":(event.toElement.tagName+'.'+event.toElement.className)) );
		cancelCycle();
	}
}

function beginCycle( idbase, parent ) {
	if( idbase == cycleBase )
		return;
	//alert( "beginCycle: " + idbase + " running: " + cycleBase );
	cancelCycle();
	cycleLast = document.getElementById( idbase+'1' );
	cycleNext = document.getElementById( idbase+'2' );
	if(( cycleLast != null ) && ( cycleNext != null )) {
		cycleBase = idbase;
		cycleLasti = 1;
		intervalID = window.setInterval( nextCycle, 50 );
		if( parent != null )
			cycleParent = parent;
		else
			cycleParent = cycleLast.parentNode;
	}
}

function nextCycle() {
	if(( cycleLasti <= 0 ) || ( cycleBase == null ) || ( cycleLast == null )) {
		cancelCycle();
		return;
	}
	cycleLasti++;
	cycleNext = document.getElementById( cycleBase+cycleLasti );
	if( cycleNext == null ) {
		cycleLasti=1;
		if( ++nCycles < cycleLimit )
			cycleNext = document.getElementById( cycleBase+cycleLasti );
		if( cycleNext == null ) {
			cancelCycle();
			return;
		}
	}
	window.clearInterval( intervalID );
	intervalID = window.setInterval( nextCycle, 500 );
	cycleNext.style.visibility = 'visible';
	cycleLast.style.visibility = 'hidden';
	cycleLast = cycleNext;
}