//class_action.js---------------------------------------------------------------------------------------------
//This class maps an eventlist onto content regions
function Action(onExecute){
this.onExecute=onExecute;
}//end function

Action.prototype.init=function Action_init(q_ev,q_count){
this.eventlist=q_ev;
this.initTracks(q_count);
this.nextIndex=0;
}//end function

Action.prototype.initTracks=function Action_initTracks(q_count){
this.trackPrev=new Array();//Current region content
this.trackNext=new Array();//New region content
for(var ix=0;ix<q_count;ix++){
	this.trackPrev[this.trackPrev.length]=-1;
	this.trackNext[this.trackNext.length]=-1;
}//next
}//end function

Action.prototype.setCmd=function Action_setCmd(q_time){
var q_cmdtime=Math.floor(q_time);
if(this.nextIndex<this.eventlist.length){
	var q_nexttime=this.eventlist[this.nextIndex][0];
}else{
	var q_nexttime=null;
}//end if
//q_nexttime is the next time any action will happen
//this.prevTime is the previous time for which the current tracks are valid
if(q_cmdtime==this.prevTime){
//up to date
}else if(q_cmdtime<this.prevTime){
//full update needed
	this.setTracknext(0,q_cmdtime);
	this.executeTracks(this.trackNext.length-1,q_cmdtime);
}else if(q_cmdtime>=q_nexttime){
//sequential update needed
	this.setTracknext(this.nextIndex,q_cmdtime);
	this.executeTracks(this.trackNext.length-1,q_cmdtime);
}//end if
this.prevTime=q_cmdtime;
}//end function

Action.prototype.setNextIndex=function Action_setNextIndex(q_time){
return this.setTracknext(0,q_time);
}//end function

Action.prototype.getNextEvent=function(q_filter){
var iy=this.nextIndex;
if(iy>-1 && iy<this.eventlist.length){
	for(var ix=iy;ix<this.eventlist.length;ix++){
		if(this.eventlist[ix][1].indexOf(q_filter)>-1){
			return this.eventlist[ix];//WildFind(0,this.eventlist[ix][1],"setdoc('*',");
		}//end if
	}//next	
	return "";
}else{
	return "";
}//end if
}//end function

Action.prototype.setTracknext=function Action_setTracknext(startindex,q_cmdtime){
var target;
for(var ix=startindex;ix<this.eventlist.length;ix++){
	if(q_cmdtime>=this.eventlist[ix][0]){
		target=this.getTarget(this.eventlist[ix]);
		this.trackNext[target]=ix;
	}else{
		this.nextIndex=ix;return this.eventlist[ix][0];
		break;
	}//end if
}//next
return -1;
}//end function


Action.prototype.getTarget=function Action_getTarget(q_eventitem){
if(q_eventitem.length==2){
	q_eventitem[2]=getTarget(q_eventitem[1]);
}//end if
return q_eventitem[2];
}//end function

Action.prototype.executeTracks=function Action_executeTracks(q_count,q_cmdtime){

for(var ix=0;ix<=q_count;ix++){
	if(this.trackPrev[ix]==this.trackNext[ix]){
	}else{
		var actionindex=this.trackNext[ix];
		if(actionindex>=0 && actionindex<this.eventlist.length){
			//try{
				if(this.eventlist[actionindex].length>1){
					this.onExecute(this.eventlist[actionindex][1],actionindex,q_cmdtime);
					this.trackPrev[ix]=actionindex;
				}//end if
			//}catch(errObj){
			//logga("Error in executeTracks, actionindex="+actionindex);
			//}//end catch
		}//end if
	}//end if
}//next
}//end function

Action.prototype.execute=function Action_execute(q_cmd){
}//end function

Action.prototype.needSync=function (){
for(var ix=0;ix<this.eventlist.length;ix++){
	if(this.eventlist[ix][0]>1 && (this.eventlist[ix][1].indexOf("setinter(") || this.eventlist[ix][1].indexOf("setdoc(")) ){
		return true;
	}//end if
}//next
return false;
}//end function

Action.prototype.show=function (){
html="";
for(var ix=0;ix<this.trackPrev.length;ix++){
	html=html+ ix+": "+this.trackPrev[ix]+"<br>";
}//next
return html;
}//end function

Action.prototype.showNext=function (){
html="";
for(var ix=0;ix<this.trackNext.length;ix++){
	html=html+ ix+": "+this.trackNext[ix]+"<br>";
}//next
return html;
}//end function

Action.prototype.showTimes=function (){
var q_showtime=q_getShowtime();
var html="";
for(var ix=0;ix<eventlist.length;ix++){
	if(eventlist[ix][0]>q_showtime){
		html=html+ ix+": "+timestring(eventlist[ix][0])+"<br>";
	}//end if
}//next
return html;
}//end function
//end class Action