/**
 * QLIB 1.0 Animated Digital Counter
 * Copyright (C) 2002 2003, Quazzle.com Serge Dolgov
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * http://qlib.quazzle.com
 */
 
function QImageList(width, height) {
    var len = QImageList.arguments.length - 2;
    if (len > 0) {
        this.list = new Array(len);
        this.length = len;
        this.width = width;
        this.height = height;
        var im;
        for (var j=0; j<len; j++) {
            im = new Image(width, height);
            im.src = QImageList.arguments[j + 2];
            this.list[j] = im;
        }
    }
}

function QControl_init(parent, name) {
    this.parent = parent || self;
    this.window = (parent && parent.window) || self;
    this.document = (parent && parent.document) || self.document;
    this.name = (parent && parent.name) ? (parent.name + "." + name) : ("self." + name);
    this.id = "Q";
    var h = this.hash(this.name);
    for (var j=0; j<8; j++) {
        this.id += QControl.HEXTABLE.charAt(h & 15);
        h >>>= 4;
    }
}

function QControl_hash(str) {
    var h = 0;
    if (str) {
        for (var j=str.length-1; j>=0; j--) {
            h ^= QControl.ANTABLE.indexOf(str.charAt(j)) + 1;
            for (var i=0; i<3; i++) {
                var m = (h = h<<7 | h>>>25) & 150994944;
                h ^= m ? (m == 150994944 ? 1 : 0) : 1;
            }
        }
    }
    return h;
}

function QControl_nop() {
}

function QControl() {
    this.init = QControl_init;
    this.hash = QControl_hash;
    this.window = self;
    this.document = self.document;
    this.tag = null;
}
QControl.ANTABLE  = "w5Q2KkFts3deLIPg8Nynu_JAUBZ9YxmH1XW47oDpa6lcjMRfi0CrhbGSOTvqzEV";
QControl.HEXTABLE = "0123456789ABCDEF";
QControl.nop = QControl_nop;
QControl.event = QControl_nop;

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);
            images[j].src = (v >= 1) || (!j) ? res.list[mod].src : res.list[10].src;
            v /= 10;
        }
    }
}

function QCounter_count(value, step) {
    this._cntt = false;
    this.value += step; 
    if ((step * (this.value - value)) >= 0) {
        this.value = value - 0;  // convert to number
    } else {
        this._cntt = setTimeout(this.name + ".count(" + value + "," + step + ")", 50);
    }
    this.update();
}
         
function QCounter_set(value) {
    this.setval = value;
    if (value != this.value) {
        if (this._cntt) {
            clearTimeout(this._cntt);
            this._cntt = false;
        }
        var dv = value - this.value;
        if (this.effect == 2) {
            dv = dv / Math.min(10, Math.abs(dv));
        } else if (this.effect == 3) {
            dv = dv / Math.abs(dv);
        }
        this.count(value, dv);
    }
}

function QCounter(parent, name, res, size, effect) {
    this.init(parent, name);
    if (res) {
        this.res = res;
        this.setval = this.value = 0;
        this.size = size || 4;
        this.effect = effect || 2;
        this._cntt = false;
        this.images = new Array(this.size);
        this.set = QCounter_set;
        this.update = QCounter_update;
        this.count = QCounter_count;
        with (this) {
            document.write('<table class="qcounter" width="' + (res.width * size) + '" height="' + res.height +
                '" border="0" cellspacing="0" cellpadding="0" unselectable="on"><tr>');
            for (var j=(size - 1); j>=0; j--) {
                document.write('<td width="' + res.width + '" height="' + res.height +
                    '" unselectable="on"><img name="' + id + j + '" src="' + (j ? res.list[10].src : res.list[0].src) +
                    '" border="0" width="' + res.width + '" height="' + res.height + '"></td>');
                images[j] = document.images[id + j] || new Image(1, 1);
            }
            document.write('</tr></table>');
        }
    } else {
        this.document.write("invalid resource");
    }
}
QCounter.prototype = new QControl();
QCounter.INSTANT   = 1;
QCounter.FAST      = 2;
QCounter.SLOW      = 3;
