default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / httemplate / elements / qlib / sprite.js
1 /**\r
2  * QLIB 1.0 Sprite Object\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 QSprite_load(src) {\r
12     if (src) {\r
13         this.face = new Image(this.cwidth, this.cheight);\r
14         this.face.src = src;\r
15         this.valid = false;\r
16     }\r
17 }\r
18  \r
19 function QSprite_show(show) {\r
20     if (show && !this.valid && this.face.complete) {\r
21         this._img.src = this.face.src;\r
22         this.valid = true;\r
23     }\r
24     this._show(show);\r
25 }\r
26 \r
27 function QSprite_moveTo(x, y) {\r
28     this.stop();\r
29     this._move(x, y);\r
30 }\r
31 \r
32 function QSprite_slideTo(x, y) {\r
33     this.stop();\r
34     if (this.visible) {\r
35         this.doSlide(++this._spro, x, y);\r
36     } else {\r
37         this.moveTo(x, y);\r
38     }\r
39 }\r
40 \r
41 function QSprite_shake() {\r
42     this.stop();\r
43     if (this.visible) {\r
44         this.doShake(++this._spro, 0, this.x, this.y);\r
45     }\r
46 }\r
47 \r
48 function QSprite_stop() {\r
49     this._spro++;\r
50     if (this._sprt) {\r
51         clearTimeout(this._sprt);\r
52         this._sprt = false;\r
53     }\r
54 }\r
55 \r
56 function QSprite_doSlide(id, x, y) {\r
57     if (this._spro == id) {\r
58         this._sprt = false;\r
59         var dx = Math.round(x - this.x);\r
60         var dy = Math.round(y - this.y);\r
61         if (dx || dy) {\r
62             if (dx) dx = dx > 0 ? Math.ceil(dx/4) : Math.floor(dx/4);\r
63             if (dy) dy = dy > 0 ? Math.ceil(dy/4) : Math.floor(dy/4);\r
64             this._move(this.x + dx, this.y + dy);\r
65             this._sprt = setTimeout(this.name + ".doSlide(" + id + "," + x + "," + y + ")", 30);\r
66         } else {\r
67             this._move(x, y);\r
68         }\r
69     }\r
70 }\r
71 \r
72 function QSprite_doShake(id, phase, x, y) {\r
73     if (this._spro == id) {\r
74         this._sprt = false;\r
75         if (phase < 20) {\r
76             var m = 3 * Math.sin(.16 * phase);\r
77             this._move(x + m * Math.sin(phase), y + m * Math.cos(phase));\r
78             this._sprt = setTimeout(this.name + ".doShake(" + id + "," + (++phase) + "," + x + "," + y + ")", 20);\r
79         } else {\r
80             this._move(x, y);\r
81         }\r
82     }\r
83 }\r
84 \r
85 function QSprite_doClick() {\r
86     if (!this._sprt) {\r
87         this.onClick(this.tag);\r
88     }\r
89     return false;\r
90 }\r
91 \r
92 function QSprite(parent, name, x, y, width, height, src, visible, effects, opacity, zindex) {\r
93     this.init(parent, name);\r
94     this.x = x - 0;\r
95     this.y = y - 0;\r
96     this.width = (this.cwidth = width - 0) + 8;\r
97     this.height = (this.cheight = height - 0) + 8;\r
98     var j = QSprite.arguments.length;\r
99     this.visible = (j > 7) ? visible : true;\r
100     this.effects = (j > 8) ? effects : 0;\r
101     this.opacity = (j > 9) ? opacity : 100;\r
102     this.zindex  = (j > 10) ? zindex : null;\r
103     this.valid = !!src;\r
104     this.content = '<a href="#" title="" onclick="return false" onmousedown="return ' + this.name +\r
105         '.doClick()" onmouseover="window.top.status=\'\';return true" hidefocus="true" unselectable="on"><img name="' +\r
106         this.id + '" src="' + (src || '') + '" border="0" width="' + this.cwidth + '" height="' + this.cheight +\r
107         '" alt="" unselectable="on"></a>';\r
108     this.doClick = QSprite_doClick;\r
109     this.doSlide = QSprite_doSlide;\r
110     this.doShake = QSprite_doShake;\r
111     this.onClick = QControl.event;\r
112     this.create();\r
113     this.face = this._img = this.document.images[this.id] || new Image(1, 1);\r
114     this._spro = 0;\r
115     this._sprt = false;\r
116     this._show = this.show;\r
117     this._move = this.moveTo;\r
118     this.load = QSprite_load;\r
119     this.show = QSprite_show;\r
120     this.moveTo = QSprite_moveTo;\r
121     this.slideTo = QSprite_slideTo;\r
122     this.shake = QSprite_shake;\r
123     this.stop = QSprite_stop;\r
124 }\r
125 QSprite.prototype = new QWndCtrl();\r