/**
 * QLIB 1.0 Active Counter Control
 * Serge Dolgov (C) 2002
 * studio.quazzle.com
 */

function QCounter_update() {
    with (this) {
        var v = Math.max(value, 0);
        var mod;
        for (var j=0; j<size; j++) {
            mod = Math.floor(v % 10);
            document.images["$c_" + name + j].src = (v >= 1) || (!j) ? res.list[mod].src : res.list[10].src;
            v /= 10;
        }
    }
}

function QCounter_count(value, step) {
    this._TID = null;
    this.value += step; 
    if ((step * (this.value - value)) >= 0) {
        this.value = value - 0;  // convert to number
    } else {
        this._TID = setTimeout(this.name + ".count(" + value + "," + step + ")", 50);
    }
    this.update();
}
         
function QCounter_set(value) {
    if (value != this.value) {
        if (this._TID) {
            clearTimeout(this._TID);
            this._TID = null;
        }
        var dv = value - this.value;
        if (this.effects == 1) {
            dv = dv / Math.min(10, Math.abs(dv));
        } else if (this.effects == 2) {
            dv = dv / Math.abs(dv);
        }
        this.count(value, dv);
    }
}

function QCounter(parent, name, res, size) {
    this.init(parent, name);
    if (res) {
        this.res = res;
        this.value = 0;
        this.size = size || 4;
        this.effects = 1;
        this.set = QCounter_set;
        this.update = QCounter_update;
        this.count = QCounter_count;
        with (res) {
            var out = '<table class="counter" border="0" width="' + (width * this.size) + '" height="' + height + '" cellspacing="0" cellpadding="0"><tr>\n';
            for (var j=(this.size - 1); j>=0; j--) {
                out += '<td width="' + width + '" height="' + height + '">';
                out += '<img name="$c_' + this.name + j + '" src="' + (j ? list[10].src : list[0].src) + '" border="0" width="' + width + '" height="' + height + '"></td>\n';
            }
            document.writeln(out + '</tr></table>');
        }
    } else {
        document.write("invalid resource");
    }
}
QCounter.prototype = new QControl();

QCounter.NONE    = 0;   // counter effects
QCounter.DEFAULT = 1;
QCounter.SLOW    = 2;
