default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / httemplate / elements / qlib / counter.js
1 /**\r
2  * QLIB 1.0 Animated Digital Counter\r
3  * Copyright (C) 2002 2003, Quazzle.com Serge Dolgov\r
4  * This program is free software; you can redistribute it and/or\r
5  * modify it under the terms of the GNU General Public License\r
6  * as published by the Free Software Foundation; either version 2\r
7  * of the License, or (at your option) any later version.\r
8  * http://qlib.quazzle.com\r
9  */\r
10 \r
11 function QCounter_update() {\r
12     with (this) {\r
13         var v = Math.max(value, 0);\r
14         var mod;\r
15         for (var j=0; j<size; j++) {\r
16             mod = Math.floor(v % 10);\r
17             images[j].src = (v >= 1) || (!j) ? res.list[mod].src : res.list[10].src;\r
18             v /= 10;\r
19         }\r
20     }\r
21 }\r
22 \r
23 function QCounter_count(value, step) {\r
24     this._cntt = false;\r
25     this.value += step; \r
26     if ((step * (this.value - value)) >= 0) {\r
27         this.value = value - 0;  // convert to number\r
28     } else {\r
29         this._cntt = setTimeout(this.name + ".count(" + value + "," + step + ")", 50);\r
30     }\r
31     this.update();\r
32 }\r
33          \r
34 function QCounter_set(value) {\r
35     this.setval = value;\r
36     if (value != this.value) {\r
37         if (this._cntt) {\r
38             clearTimeout(this._cntt);\r
39             this._cntt = false;\r
40         }\r
41         var dv = value - this.value;\r
42         if (this.effect == 2) {\r
43             dv = dv / Math.min(10, Math.abs(dv));\r
44         } else if (this.effect == 3) {\r
45             dv = dv / Math.abs(dv);\r
46         }\r
47         this.count(value, dv);\r
48     }\r
49 }\r
50 \r
51 function QCounter(parent, name, res, size, effect) {\r
52     this.init(parent, name);\r
53     if (res) {\r
54         this.res = res;\r
55         this.setval = this.value = 0;\r
56         this.size = size || 4;\r
57         this.effect = effect || 2;\r
58         this._cntt = false;\r
59         this.images = new Array(this.size);\r
60         this.set = QCounter_set;\r
61         this.update = QCounter_update;\r
62         this.count = QCounter_count;\r
63         with (this) {\r
64             document.write('<table class="qcounter" width="' + (res.width * size) + '" height="' + res.height +\r
65                 '" border="0" cellspacing="0" cellpadding="0" unselectable="on"><tr>');\r
66             for (var j=(size - 1); j>=0; j--) {\r
67                 document.write('<td width="' + res.width + '" height="' + res.height +\r
68                     '" unselectable="on"><img name="' + id + j + '" src="' + (j ? res.list[10].src : res.list[0].src) +\r
69                     '" border="0" width="' + res.width + '" height="' + res.height + '"></td>');\r
70                 images[j] = document.images[id + j] || new Image(1, 1);\r
71             }\r
72             document.write('</tr></table>');\r
73         }\r
74     } else {\r
75         this.document.write("invalid resource");\r
76     }\r
77 }\r
78 QCounter.prototype = new QControl();\r
79 QCounter.INSTANT   = 1;\r
80 QCounter.FAST      = 2;\r
81 QCounter.SLOW      = 3;\r