﻿
function Progress()
{
	var css;
	var divContainer,tblProgress;
	var t = null;
	var count = 0,del = 0,iterations = 0;
	var active = false;

	this.init = function(container,color)
	{
		divContainer = container;
		css = isDefined(color) && color.length > 0 ? color : "Green";
	}
	this.start = function(delay)//use 0 if no delay needed
	{
		if(!isDefined(tblProgress))
		{
			tblProgress = document.createElement("TABLE");
			divContainer.appendChild(tblProgress);
			tblProgress.cellPadding = 0;
			tblProgress.cellSpacing = 2;
			tblProgress.style.border = "0px";
			var tr = tblProgress.insertRow(-1);
			tr.style.height = "8px";
		}
		var c;
		for(var i=0;i<21;i++)
		{
			c = tblProgress.rows[0].insertCell(-1);
			c.style.width = "8px";
		}
		del = delay;
		active = true;
		begin();
	}
	this.stop = function()
	{
		finish();
		active = false;
		count = iterations = 0;
		if(isDefined(tblProgress))
			divContainer.removeChild(tblProgress);
		tblProgress = null;
	}
	var begin = function()
	{
		try{t = window.setInterval(doTheThing,80);}
		catch(e){}
	}
	var finish = function()
	{
		try{window.clearInterval(t);}
		catch(e){}
		t = null;
	}
	var doTheThing = function()
	{
		finish();
		if(!isDefined(tblProgress))return;
		iterations++;
		if(iterations < del)
		{
			begin();
			return;
		}
		if(count == 0 || !active)
			for(var i=0;i<tblProgress.rows[0].cells.length;i++)
				tblProgress.rows[0].cells[i].style.backgroundColor = "";
		if(active)
		{
			tblProgress.rows[0].cells[count].style.backgroundColor = css;
			count++;
			if(count > tblProgress.rows[0].cells.length-1)count = 0;
			begin();
		}
	}
}