// Juan Matias de la Camara Beovide
// TJF - Tiger JavaScrip Framework
// Abril 2008

// Objeto para tomar tiempos. Tiene la capacidad de serializarse
// y rearmarse para poder ser almacenado en cookies, etc.

// Modo de instanciar el objeto:
// var o = new TJF_Timer({name : 'nombre'});
//
// Metodos:
//   serialize() : devuelve el objeto serializado
//   unserialize(objeto) : recibe un objeto serializado y lo rearma en el actual
//   getTime() : Devuelve el tiempo en milisegundos.
//   getTimeSec() : Devuelve el tiempo en segundos.
//   getTimeMin() : Devuelve el tiempo en minutos.
//   getTimeHs() : Devuelve el tiempo en horas.
//   start() : Inicia el timer.
//   stop() : Frena el timer.
//   getName() : Devuelve el nombre del timer.

//Variables globales del timer 
var TJFCTES_TIMER = {
	STATUS : {	NOT_STARTED : 0,
				STARTED : 1,
				STOPPED : 2
			 }
};

// Lo creo como clase
var TJF_Timer = Class.create();

// Especifico los defaults
TJF_Timer.DEFAULTS = {
	name : "timer"
};

// Armo la clase con el prototype
TJF_Timer.prototype = {
	
	// Properties
		name : "",
		state : TJFCTES_TIMER.STATUS.NOT_STARTED,
		startTOD : new Date(),
		endTOD : new Date(),
	
	// Methods
		// constructor
		initialize : function (options){
			this.options = Extend(options, TJF_Timer.DEFAULTS );
			this.name = this.options.name;
		},
		
		
		serialize : function (){
			return(this.name+";"+this.state+";"+this.startTOD.getTime()+";"+this.endTOD.getTime()+";");
		},
		unserialize : function (serObject){
			var elements = serObject.split(/;/);
			this.name = elements[0];
			this.state = elements[1];
			this.startTOD = new Date();
			this.startTOD.setTime(elements[2]);
			this.endTOD = new Date();
			this.endTOD.setTime(elements[3]);
			
			return(1);
		},

		getTime : function(){
			if(this.state == TJFCTES_TIMER.STATUS.STOPPED){
				return(this.endTOD.getTime() - this.startTOD.getTime());
			}
		},
		getTimeSec : function(){
			if(this.state == TJFCTES_TIMER.STATUS.STOPPED){
				return(this.getTime() * 1000);
			}
		},
		getTimeMin : function(){
			if(this.state == TJFCTES_TIMER.STATUS.STOPPED){
				return(this.getTime() * 1000 * 60);
			}
		},
		getTimeHs : function(){
			if(this.state == TJFCTES_TIMER.STATUS.STOPPED){
				return(this.getTime() * 1000 * 60 * 60);
			}
		},
		start : function(){
			this.startTOD = new Date();
			this.endTOD = new Date();
			this.state = TJFCTES_TIMER.STATUS.STARTED;
		},
		stop : function(){
			if(this.state == TJFCTES_TIMER.STATUS.STARTED){
				this.endTOD = new Date();
				this.state = TJFCTES_TIMER.STATUS.STOPPED;
			}
		},
		getName : function(){
			return(this.name);
		}
};
