/******************************************************************************************************
*Mod  - Oct 19, 2009:
*		Added the feature that the credit is also faded. The Slideshow Object has two added properties
*		that indicate the start and end opacity value for the credit.
*
*
*Mod - Dec 2, 2009:  All data will be handled in point units
*
*Mod - Aug. 19, 2011: When we process an eventlist.  Events are played in consecutive order.
		However, an event may be played before a previous event has finished. Thus
		it is possible that two or more events are playing at the same time.
		These event may finish in a different order from the one in which
		they were started because they may last for different lengths of time.


		Each event has an onfinish and onplaying routine.

		We make the assumption that one single event itself will not be played again if
		it already is being played. In order to determine that all events are finished,
		we much make a pass through the event list - to see if any of the events are playing.		
*******************************************************************************************************/
var goldenrod
if (!goldenrod) goldenrod = {}





/******************************************************************************************************
*This is a Event Show data class.
*self  = String that is the name of the instance of the object
*eventlist = is an array of occurence Objects 
*i = the index of the current event being processed in eventlist.
*******************************************************************************************************/
goldenrod.Eventshow = function(self, eventlist, eventNum, totalTime, loopNum) {
	this.self = self;
	
	this.eventList = eventlist;
	this.i = eventNum || 0;		//index for pending event
	this.first = eventNum || 0;	//index of first event
	this.totalTime = totalTime || 0;			//Total time that has past since last event started.
	loopNum == undefined ? this.loopNum = -1 : this.loopNum = loopNum;
	this.loopCount = 0;
	this.setTimeOutId = undefined;
	this.setIntervalId = undefined;
	
	this.runSwitch = true;
}

/*----------------Check all events in the list and if any are running return true-------------------*/
goldenrod.Eventshow.prototype.isRunning = function(){
	var returnValue = false;
	for(var i=0; i < this.eventList.length; i++){
		if(this.eventList[i].eventItem.isRunning) {
			returnValue = true;
			break;
		}
	}
	return returnValue;
}

/******************************************************************************************************
*This function actually runs an event show.
*It is given one parameter: an Eventshow Object. 
*Note - totalTime and loopCount must always be reset to 0 if this function is explicitly 
*		invoked more than once.
*
*Count - when count is 0 we do not play the event but it will still hold a place in time in the 
*		Event string - because the timing of subsequent events depends on the event before.
*******************************************************************************************************/
goldenrod.Eventshow.prototype.run = function() {
	var returnvalue = true;
	var currentEvent, time, delay, count;

	if(this.setTimeOutId) {
		clearTimeout(this.setTimeOutId);
		this.setTimeOutId = undefined;
	}

	while( this.runSwitch && ((this.loopNum == -1) || (this.loopCount < this.loopNum)) ) {	
		time = this.eventList[this.i].eventTime;	//time = time to start current event
		
				 
		
		if ((delay = time - this.totalTime) > 0 ) {	//If total time past is less than time to start
			/*-------------totalTime is always zero except when the event show starts--------------*/
			this.totalTime = time; //Update totalTime to what it will be when we return.
			this.setTimeOutId = setTimeout(this.self + ".run()", delay );		//Set delay for this function
			break;	
		}
		else {
			if(this.eventList[this.i].noOverlap && this.isRunning() ){
				/*---------------------------------------------------------------------------
					If event is still running set up to periodically call this routine 
					25 times per second checking to see if the event has finished.
				-----------------------------------------------------------------------------*/
				if(!this.setIntervalId) this.setIntervalId = setInterval(this.self + ".run()", 40 );
				break;
			}
			else {
				if(this.setIntervalId){
					clearInterval(this.setIntervalId);
					this.setIntervalId = undefined;
				}
			}
			this.totalTime = 0;	//Reset Totaltime because we are going to play the pending event.
			currentEvent = this.eventList[this.i].eventItem //Set currentEvent
			count = this.eventList[this.i].eventCount;
			if(count > 0 ) {this.eventList[this.i].eventCount--; }
			this.i++; if(this.i >= this.eventList.length) this.i = 0;
			if(count != 0){
				 currentEvent.Play();	//play it
			}
			if(this.i == this.first ) {
				this.loopCount++;		//increment loop count when we go back to first event.
			}
		}
			
			

	}
	
	return returnvalue
}

goldenrod.Eventshow.prototype.cancelRun = function() {
	this.runSwitch = false;
	if(this.setTimeOutId){
		clearTimeout(this.setTimeOutId);
		this.setTimeOutId = undefined;
	}
	if(this.setIntervalId){
		clearInterval(this.setIntervalId);
		this.setIntervalId = undefined;
	}
}

