
//// MÈTODES ////

// Codis d'interval inactiu.
var TT_INACTIVE			= -9999;
var TT_INACTIVE_AGAIN	= -9998;


////////////////////////////////////////////////////////////////////////////////////////////////

//// TimeTask ////

// Constructor
function TimeTask(objName, seconds) {
	this.objName	= objName;
	this.idInterval	= TT_INACTIVE;
	this.seconds	= seconds;
}

// Engega l'execució
// Cal que s'assigni una temporització (en segons) i que estigui en estat inactiu.
// En qualsevol cas, engega l'objecte mitjançant el mètode 'run' (cal que existeixi).
// Si s'està tornant a engegar (codi d'interval = 'TT_INACTIVE_AGAIN'), s'executa tb el mètode 'reset' (si existeix).
// Un cop temporitzat, estableix un codi d'interval positiu.
TimeTask.prototype.start = function() {
	var ok = this.seconds>=1 && ( this.idInterval==TT_INACTIVE || this.idInterval==TT_INACTIVE_AGAIN );
	if ( ok ) {
		if ( this.idInterval==TT_INACTIVE_AGAIN ) {
			try { eval( this.objName + ".reset()" ); } catch (e) { }
		}
		var run = this.objName + ".run()";
		eval( this.objName + ".interval = " + this.seconds );
		eval( run );
		this.idInterval = setInterval( run, this.seconds*1000 );
		//alert( "start :" + this.idInterval );
	}
}

// Pausa l'execució
// Cal que NO estigui en estat inactiu, és a dir, que tingui assignat un codi d'interval positiu.
// S'executa el mètode 'pause' (si existeix).
// Estableix com a codi d'interval inactiu 'TT_INACTIVE_AGAIN'.
TimeTask.prototype.stop = function() {
	var ok = this.idInterval!=TT_INACTIVE && this.idInterval!=TT_INACTIVE_AGAIN;
	if ( ok ) {
		try { eval( this.objName + ".pause()" ); } catch (e) { }
		setTimeout( "clearInterval(" + this.idInterval + ")" );
		var aux = this.idInterval;
		this.idInterval = TT_INACTIVE_AGAIN;
		//alert( "stop :" + aux );
	}
}
