var nBarQuant = 0;

function bar_SetValue(oBar, sName, nTimeout)
{
	var oCurrDate = new Date();
	var nVal = oBar.nStartVal + (oCurrDate.getTime() - oBar.oStartDate.getTime()) / nTimeout;
	if ((oBar.nStartVal < oBar.nEndVal && nVal < oBar.nEndVal) || 
		(oBar.nStartVal > oBar.nEndVal && nVal > oBar.nEndVal))
	{
		oBar.setValueSimple(nVal);
		oBar.tid = window.setTimeout("bar_SetValue(" + sName + ", '" + sName + "', " + nTimeout + ")", Math.abs(nTimeout) - (new Date().getTime() - oCurrDate.getTime()));
	}
	else
		oBar.setValueSimple(oBar.nEndVal);
}

function Bar(oElement)
{
	this.nNum = nBarQuant;
	nBarQuant++;
	
	this.nWidth = 268;
	
	oElement.style.position = "relative";
	oElement.style.height = 48;
	oElement.innerHTML = 
		'<div id = "bar_max_label' + this.nNum + '" style = "position:absolute;left:1px;top:0px;height:12px;color:#234293;font-size:12px;font-family:Arial;">Max:&nbsp;<b>0</b></div>' +
		'<div style = "position:absolute;left:0px;top:15px;width:267px;height:18px;background:#FFFFFF;border:solid 1px #BFAF70;"></div>' + 
		'<div id = "bar_cur' + this.nNum + '" style = "position:absolute;left:1px;top:16px;width:0px;height:18px;background:#234293;"></div>' +
		'<div id = "bar_max' + this.nNum + '" style = "position:absolute;left:1px;top:16px;width:0px;height:18px;background:#A5A7AE;"></div>' +
		'<img src = "/img/bar/bar.png" style = "position:absolute;left:-1px;top:15px;"/>' +
		'<div id = "bar_cur_label' + this.nNum + '" style = "position:absolute;left:1px;top:36px;height:12px;color:#234293;font-size:12px;font-family:Arial;">Current:&nbsp;<b>0</b></div>';
		
	this.oOpend = document.getElementById("bar_cur" + this.nNum);
	this.oMax = document.getElementById("bar_max" + this.nNum);
	this.oLabelCur = document.getElementById("bar_cur_label" + this.nNum);
	this.oLabelMax = document.getElementById("bar_max_label" + this.nNum);
	
	this.nVal = 0;
	this.nMax = 0;
	this.nTotal = 0;
		
	this.setValueSimple = function(nVal)
	{
		this.nVal = Math.floor(nVal);
		
		if (this.nTotal < this.nVal)
				this.nTotal = Math.ceil(this.nVal / 10) * 10;
		
		if (this.nMax < this.nVal)
			this.nMax = this.nVal;
		
		var nOpenWidth = Math.round(this.nVal / this.nTotal * this.nWidth);
		this.oOpend.style.width = nOpenWidth;
		
		this.oLabelCur.style.left = nOpenWidth;
		this.oLabelCur.innerHTML = "Current:&nbsp;<b>" + this.nVal + "</b>";
		
		this.oMax.style.left = nOpenWidth + 1;
		var nMaxRight = Math.round(this.nMax / this.nTotal * this.nWidth);
		this.oMax.style.width = nMaxRight - nOpenWidth;
		
		this.oLabelMax.style.left = nMaxRight;
		this.oLabelMax.innerHTML = "Max:&nbsp;<b>" + this.nMax + "</b>";
	}
	
	this.nStartVal = 0;
	this.nEndVal = 0;
	this.oStartDate = null;
	this.tid = null;
	
	this.setValue = function(nVal, nActionLengthMS, sThisName)
	{
		if (this.tid != null)
		{
			window.clearTimeout(this.tid);
			this.tid = null;
		}
		
		this.oStartDate = new Date();
		this.nStartVal = this.nVal;
		this.nEndVal = Math.floor(nVal);
			
		if (this.nEndVal != this.nStartVal)
		{	
			if (this.nTotal < this.nEndVal)
				this.nTotal = Math.ceil(this.nEndVal / 10) * 10;
				
			if (this.nMax < this.nEndVal)
			    this.nMax = this.nEndVal;
				
			var nTimeout = Math.floor(nActionLengthMS / (this.nEndVal - this.nStartVal));
			
			if (nTimeout != 0)
				this.tid = window.setTimeout("bar_SetValue(" + sThisName	+	", '"	+	sThisName + "', " + nTimeout + ")", Math.abs(nTimeout));
			else
				this.setValueSimple(this.nEndVal);
		}
	}
}
