/*****************************************************************************/
/*                                2010 Shio2e                                */
/*                          JAVASCRIPT CUENTA ATRAS                          */
/*****************************************************************************/

// Class: cuentaAtras
var cuentaAtras = Class.create();
cuentaAtras.prototype = {
	initialize: function (variable, tiempo, destino_d, destino_h, destino_m, destino_s) {
		this.variable 		= variable;
		this.tiempo 		= tiempo;
		this.destino_d 		= destino_d;
		this.destino_h 		= destino_h;
		this.destino_m 		= destino_m;
		this.destino_s 		= destino_s;
		this.temporizador 	= null;
	},

	calcular: function () {
		var dias, horas, minutos, segundos;

		segundos = this.tiempo % 60;
		minutos = (this.tiempo % 3600 - segundos) / 60;
		horas = (this.tiempo % (24 * 3600) - minutos * 60 - segundos) / 3600;
		dias = (this.tiempo - this.tiempo % (24 * 3600)) / (24 * 3600);

		return { dias: dias, horas: horas, minutos: minutos, segundos: segundos };
	},

	genVisible: function (entrada) {
		$(this.destino_d).update(entrada.dias);
		$(this.destino_h).update((entrada.horas < 10 ? '0' : '')+entrada.horas);
		$(this.destino_m).update((entrada.minutos < 10 ? '0' : '')+entrada.minutos);
		$(this.destino_s).update((entrada.segundos < 10 ? '0' : '')+entrada.segundos);
	},

	start: function () {
		this.genVisible(this.calcular());
		this.temporizador = setInterval(this.variable+'.transicion()',1000);
	},

	stop: function () {
		clearInterval(this.temporizador);
	},

	transicion: function () {
		if (this.tiempo > 0) {
			this.genVisible(this.calcular());
			this.tiempo--;
		} else {
			this.stop();
		}
	}
};
