/* Esta clase contiene el cronómetro
 *
 */

/* Cronómetro
 * Este cronómetro se actualiza cada cierto tiempo (precision) y cada dicho tiempo se aumenta el
 * contador en esa cantidad. Así cada segundo aumenta un segundo, cada milisegundo aumenta un milisegundo.
 * Cabe tener en cuenta que si tiene que aumentar cada ms 1ms, no irá bien, ya que son muchas peticiones.
 *
 * div_cont:	Div padre
 * limnit:		Tiempo límite
 * precision:	Precisión.
 */
function Timer(div_cont, limit, precision)
{
	if(div_cont)
	{
		this.input = document.createElement("span");
		var value = document.createTextNode("00:00:00");

		this.input.appendChild(value);
		div_cont.appendChild(this.input);

		this.limit_ms = Number(limit);

		this.interval = false;
		this.precision_ms = Number(precision);

		this.time = 1;
		this.events = new Array();
		this.events_n = 0;
	}
}

Timer.prototype =
	{
		// Comienza el cronómetro.
		start:	function()
		{
			var self = this;
			this.interval = setInterval(function() { self.update() }, this.precision_ms);

			this.showTime();
		},

		// Para.
		stop: function()
		{
			clearInterval(this.interval);
			this.interval = false;
		},

		// Aumenta el tiempo
		addTime:	function(new_time)
		{
			if(this.limit_ms && this.time + new_time >= this.limit_ms)
				this.time = this.limit_ms;
			else
				this.time += new_time;

			this.showTime();
		},

		// Actualiza el tiempo interno (cada X Tiempo).
		update:	function()
		{
			this.time += this.precision_ms;

			if(this.limit_ms && this.time >= this.limit_ms)
			{
				clearInterval(this.interval);
				this.time = this.limit_ms;
			}

			this.showTime();
			this.checkEvents();
		},

		// Muestra el tiempo
		showTime:	function()
		{
			var time = 0;
			var res_text = "";
			var res_node = false;

			if(this.limit_ms)
				time = (this.limit_ms - this.time);
			else
				time = this.time;


			res_text = String(this.getTime("H", time) + ":" + this.getTime("M", time) + ":" + this.getTime("S", time));

			if(res_text)
				res_node = document.createTextNode(res_text);
			else
				res_node = document.createTextNode("ERROR");

			this.input.replaceChild(res_node, this.input.firstChild);
		},

		// A partir de los ms, obtiene el timepo en el formato deseado.
		getTime:	function(type, time)
		{
			var res = "00";
			var division = 1;
			var mod = -1;

			switch(type)
			{
				case "H":
					division = (1000 * 60 * 60);
					mod = 99;
					break;

				case "M":
					division = (1000 * 60);
					mod = 60;
					break;

				case "S":
					division = (1000);
					mod = 60;
					break;
			}

			res = parseInt(time / division);
			res = res % mod;

			if(res < 10)
				res = String("0" + res);
			else
				res = String(res);

			return res;
		},

		// Compruebas si en el tiempo que se llama hay algún evento que se tiene que ejecutar.
		checkEvents:	function()
		{
			for(var i = 0; i < this.events_n; i++)
			{
				if(this.events[i])
				{
					var ev = this.events[i];

					if(this.time >= ev.time)
					{
						var self = this;
						ev.func(self);

						this.events[i] = false;
					}
				}
			}
		},

		// Añade un evento, una función que se tiene que ejecutar en un cierto tiempo
		attachEvent:	function(func, time)
		{
			var self = this;

			if(time >= this.time)
			{
				this.events[this.events_n] =
				{
					"func":		func,
					"time":		time
				};

				this.events_n ++;
			}
			else
				func(self)
		}
	}