var imgWidth = 12;
var imgHeight = 18;
var bimgWidth = 3;
var bimgHeight = 22;
var imgPath = '/images/chet/';
var dgImg = [];
for (var i = 0; i < 10; i++) {
    dgImg[i] = new Image(imgWidth, imgHeight);
    dgImg[i].src = imgPath + 'dg' + i + '.gif';
}
var dgStart = new Image(bimgWidth, bimgHeight); 
dgStart.src = imgPath + 'dgs.gif';
var dgEnd = new Image(bimgWidth, bimgHeight);
dgEnd.src = imgPath + 'dge.gif';

var Counters = [];
Counter = function (container, dgCount, callback) {
    this.container = container;
    this.callback = callback;
    this.prefix = Math.floor (Math.random (1, 1000));    
    this.table = null;
    this.tbody = null;
    this.row = null;
    this.dgCount = dgCount;
    this.dg = [];
    this.createTable (dgCount);
    this.value = '0';
    this.index = Counters.length;
    Counters[this.index] = this;
    this.output ('0');
}
Counter.prototype.output = function (value) {
    var index;
    for(var i = 0; i < this.dgCount; i++) {
        index = Number (this.value.substr(i, 1));
        
        if (index != this.dg[i].index) {
            this.dg[i].src = dgImg[index].src;
            this.dg[i].index = index;
        }
    }
}
Counter.prototype.createTable = function  () {
    var table = Counter.createElement("table", this.container);
	this.table = table;
	table.cellSpacing = 0;
	table.cellPadding = 0;
	var tbody = Counter.createElement("tbody", table);
	this.tbody = tbody;
	var row = Counter.createElement("tr", tbody);
	this.row = row;
	var cell = null;
	var img = null;
	cell = Counter.createElement("td", row);
	img = Counter.createElement("img", cell);
	img.style.width = bimgWidth;
	img.style.height = bimgHeight;
	img.src = dgStart.src;
	for (var i = 0; i < this.dgCount; i++) {
	    cell = Counter.createElement("td", row);
	    this.dg[i] = Counter.createElement("img", cell);
	    this.dg[i].style.width = imgWidth;
	    this.dg[i].style.height = imgHeight;
	}
	cell = Counter.createElement("td", row);
	img = Counter.createElement("img", cell);
	img.style.width = bimgWidth;
	img.style.height = bimgHeight;
	img.src = dgEnd.src;
}


Counter.process = function () {
    for (var i = 0; i < Counters.length; i++) {
        if (Counters[i].callback != null) {
            var value = Counter.formatDigits (Counters[i].callback (), Counters[i].dgCount);
            if (Counters[i].value != value) {
                Counters[i].value = value;
                Counters[i].output (); 
            }
        }
    }
    setTimeout("Counter.process();", 1000);
}
Counter.formatDigits = function (num, dgCount) {
    var toRet = String (num);
    if (toRet.length > dgCount) {
        toRet = toRet.substr (toRet.length - dgCount, dgCount);
    } else if (toRet.length < dgCount) {
        for (var i = toRet.length; i < dgCount; i++) {
            toRet =   '0' + toRet;
        }
    }
    return toRet;    
}
Counter.createElement = function (type, parent) {
    var el = null;
	if (document.createElementNS) {
		// use the XHTML namespace; IE won't normally get here unless
		// _they_ "fix" the DOM2 implementation.
		el = document.createElementNS("http://www.w3.org/1999/xhtml", type);
	} else {
		el = document.createElement(type);
	}
	if (typeof parent != "undefined") {
		parent.appendChild(el);
	}
	return el;   
}