/* DB_Interval extends the setInterval method of Javascript */
DB_Interval = function (func, interval, max) 
{
	/* Log Start */
	log('DB_Interval(func='+ func+',interval='+ interval+', max='+max);
	/* Log End */
	this.count = 0;
	this.max = max || 0;
	this.interval = interval; /* # in ms */
	this.interval_func;
	this.func = func;

	var tb = DB_Thunder.Base;

	this.start = function ()
	{
		if (!this.interval_func)
		{
			var _func = tb.bind(this.next, this);
			this.interval_func = window.setInterval(_func, this.interval);
		}
	}
	this.stop = function ()
	{
		if (this.interval_func)
		{
			window.clearInterval(this.interval_func);
			this.interval_func = null;
		}
	}
	this.toggle = function ()
	{
		console.log("ah");
		if (this.interval_func)
		{
			console.log('stop');
			this.stop();
		}
		else
		{
			this.start();
		}
	}
	this.next = function()
	{
		this.count += 1;
		if (this.max && this.count > this.max)
		{
			this.stop();
		}
		else 
		{
			this.func();
		}
	},
	this.reset = function()
	{
		this.stop();
		this.start();
	}

	// Setup the function that will be called by setInterval
	var _func = tb.bind(this.next, this);
}

