timout management to avoid double animate running on explorer
[brisk.git] / web / ticker.js
1 /*
2  *  brisk - ticker.js
3  *
4  *  Copyright (C) 2006-2008 Matteo Nastasi
5  *                          mailto: nastasi@alternativeoutput.it 
6  *                                  matteo.nastasi@milug.org
7  *                          web: http://www.alternativeoutput.it
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * General Public License for more details. You should have received a
18  * copy of the GNU General Public License along with this program; if
19  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
20  * Suite 330, Boston, MA 02111-1307, USA.
21  *
22  * $Id$
23  *
24  */
25
26 function train(anc) {
27     var box;
28     this.anc = anc;
29        
30     box = document.createElement("div");
31     box.className = "train";
32     box.id = "train";
33     box.style.left = "0px";
34     box.style.width = "0px";
35     box.style.height = "0px";
36     box.anc = this;
37     
38     addEvent(box, "mouseover", function() { this.anc.stop_move(); } );
39     addEvent(box, "mouseout", function() { this.anc.start_move(); } );
40
41     this.box = box;
42     this.anc.appendChild(box);
43
44 }
45
46 train.prototype = {
47     anc: null,
48     first: null,
49     box: null,
50     notebox: null,
51     width: 0,
52     deltat: 100,
53     deltas: 5,
54     xend: 0,
55     timout: null,
56
57     show: function()
58     {
59         this.box.style.visibility = "visible";
60     },
61
62     hide: function()
63     {
64         this.box.style.visibility = "hidden";
65         for (cur = this.first ; cur != null ; cur = cur.next) {
66             if (cur.notebox != null) {
67                 cur.cb_mouseout();
68             }
69         }
70            
71     },
72
73     add: function(table, title)
74     {
75         var last, wag, curx;
76         var dostart = 0;
77
78         for (cur = this.first ; cur != null ; cur = cur.next) {
79             if (cur.table == table)
80                 return;
81         }
82
83         wag = new wagon(this, table, title);
84         if (this.first == null) {
85             this.first = wag;
86             dostart = 1;
87         }
88         else {
89             for (cur = this.first ; cur.next != null ; cur = cur.next);
90             cur.next = wag;
91         }
92
93         this.redraw();
94         this.xend = -this.widthbox_get();
95
96         if (dostart) {
97             this.start();
98         }
99     },
100
101     rem: function(table)
102     {
103         var prev = null, remo = null;
104
105         if (this.first == null) {
106             return;
107         }
108    
109         if (this.first.table == table) {
110             remo = this.first;
111         }
112
113         for (cur = this.first ; cur != null ; cur = cur.next) {
114             // recalculate the width and replace wagons
115             if (cur.table == table) {
116                 remo = cur;
117                 break;
118             }
119         }
120
121         if (remo != null) {
122             remo.box.className = "wagon_disable";
123             removeEvent(remo.box, "mouseover", function() { this.anc.cb_mouseover(); } );
124             removeEvent(remo.box, "click",     function() { this.anc.cb_click(); } );
125             setTimeout(function(){ arguments[0][0].shut_wagon(arguments[0]); }, 3000, [ this, remo ]);
126         }
127
128         this.redraw();
129     },
130
131     rem_obj: function(obj)
132     {
133         var prev = null, remo = null;
134
135         if (this.first == null) {
136             return;
137         }
138    
139
140         if (this.first == obj) {
141             remo = this.first;
142             this.first = this.first.next;
143         }
144
145         for (cur = this.first ; cur != null ; cur = cur.next) {
146             // recalculate the width and replace wagons
147             if (cur == obj) {
148                 remo = cur;
149                 
150                 if (prev != null) {
151                    prev.next = cur.next;
152                 }
153                 break;
154             }
155             prev = cur;
156         }
157
158         this.redraw();
159
160         if (this.first == null) {
161             clearTimeout(this.timout);
162             this.timout = null;
163         }
164     },
165
166     stop_move: function()
167     {
168         this.deltas = 0;
169     },
170
171     start_move: function()
172     {
173         this.deltas = 5;
174     },
175
176     shut_wagon: function(args)
177     {
178         var curw;
179
180         obj = arguments[0][0];
181         wag = arguments[0][1];
182
183         if (wag.shut_step == 0) {
184             wag.box.className = "wagon_disable";
185             wag.shut_step = 1;
186         }
187         else {
188             if (wag.shut_step == 1) {
189                 wag.w = wag.widthbox_get();
190                 wag.box.className = "wagon_disable2";
191                 wag.box.style.padding = "0px";
192                 wag.box.style.width =  (wag.w-2)+"px";
193                 wag.box.style.height = (wag.h-2)+"px"; // 2 for border width
194                 wag.table = "";
195                 wag.box.innerHTML = "";
196                 wag.shut_step = 2;
197             }
198             curw = wag.widthbox_get() - 4;
199             wag.w = curw + 2; // 2 for border pixels
200             if (curw <= 0) {
201                 obj.box.removeChild(wag.box);
202                 obj.rem_obj(wag);
203
204                 return;
205             }
206             else {
207                 wag.box.style.width = curw+"px";
208                 wag.box.style.padding = "0px";
209             }
210         }
211         this.redraw();
212         setTimeout(function(){ arguments[0][0].shut_wagon(arguments[0]);  }, 100, [ obj, wag ]);
213     },
214
215     redraw: function()
216     {
217         var maxw = 0, maxh = 0, curh;
218
219         for (cur = this.first ; cur != null ; cur = cur.next) {
220             // recalculate the width and replace wagons
221             maxw += 2 + (maxw == 0 ? 0 : 2) + cur.width_get();
222             curh = cur.height_get();
223             maxh = (maxh < curh ? curh : maxh);
224         }
225         maxh += 2;
226         curx = 0;
227         
228         for (cur = this.first ; cur != null ; cur = cur.next) {
229             // recalculate the width and replace wagons
230             cur.left_set(curx);
231             curx += cur.width_get() + 4;
232         }
233
234         this.box.style.width = maxw+"px";
235         this.box.style.height = maxh+"px";
236     },
237
238     resetx: function()
239     {
240         this.box.style.left = this.anc.offsetWidth+"px";
241     },
242
243
244     start: function()
245     {
246         this.resetx();
247         if (this.timout == null) {
248             this.timout = setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
249         }
250     },
251
252     animate: function()
253     {
254        this.box.style.left = (parseInt(this.box.style.left) - this.deltas)+"px";
255
256 //        if (parseInt(this.box.style.left) >= this.xend) {
257 //             this.timout = setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
258 //         }
259 //         else {
260 //             this.box.style.left = this.anc.offsetWidth+"px";
261 //             this.timout = setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
262 //         }
263        if (parseInt(this.box.style.left) < this.xend) {
264            this.box.style.left = this.anc.offsetWidth+"px";
265        }
266        this.timout = setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
267     },
268
269     widthbox_get: function()
270     {
271         return (this.box.offsetWidth);
272     },
273
274     heightbox_get: function()
275     {
276         return (this.box.offsetHeight);
277     },
278
279     widthanc_get: function()
280     {
281         return (this.anc.offsetWidth);
282     },
283
284     heightanc_get: function()
285     {
286         return (this.anc.offsetHeight);
287     }
288 } // train class end
289
290
291
292 function wagon(anc, table, title) {
293     var box;
294     var othis = this;
295     this.anc = anc;
296     
297     box = document.createElement("div");
298     box.className = "wagon";
299     box.anc = this;
300     this.table = table;
301     this.title = title;
302     box.innerHTML = "Tavolo&nbsp;"+table;
303     this.box = box;
304     this.box.setAttribute("title", unescapeHTML(title));
305     
306     addEvent(this.box, "mouseover", function() { this.anc.cb_mouseover(); } );
307     addEvent(this.box, "mouseout",  function() { this.anc.cb_mouseout(); }  );
308     addEvent(this.box, "click",     function() { this.anc.cb_click(); }     );
309
310     this.anc.box.appendChild(box);
311
312     this.w = this.widthbox_get();
313     this.h = this.heightbox_get();
314 }
315
316 wagon.prototype = {
317     prev: null,
318     next: null,
319     table: 55,
320     anc: null,
321     w: 0,
322     h: 0,
323     x: 0,
324     box: null,
325     shut_step: 0, 
326
327     width_get: function()
328     {
329         return (this.w);
330     },
331
332     height_get: function()
333     {
334         return (this.h);
335     },
336
337     widthbox_get: function()
338     {
339         return (this.box.offsetWidth);
340     },
341
342     heightbox_get: function()
343     {
344         return (this.box.offsetHeight);
345     },
346
347     widthnotebox_get: function()
348     {
349         return (this.notebox.offsetWidth);
350     },
351
352     heightnotebox_get: function()
353     {
354         return (this.notebox.offsetHeight);
355     },
356
357     left_set: function(x)
358     {
359         this.box.style.left  = x+"px";
360     },
361
362     cb_click: function()
363     { 
364         act_sitdown(this.table); 
365     },
366
367     cb_mouseover: function()
368     {
369         var notebox, deltax;
370
371         notebox = document.createElement("div");
372         notebox.className = "notebox";
373         notebox.id = "wagon_note";
374
375         notebox.innerHTML = $("table"+this.table).innerHTML;
376         $("room_standup_orig").appendChild(notebox);
377         
378         deltax = 0;
379         deltax = parseInt(getStyle(this.anc.box, "left", "left")) +
380                    parseInt(getStyle(this.box, "left", "left")) +
381                    ((this.box.offsetWidth - notebox.offsetWidth) / 2);
382         
383         notebox.style.left = deltax + "px";
384         notebox.style.top = (-10 -notebox.offsetHeight)+"px";
385         notebox.style.visibility = "visible";
386         notebox.anc = this;
387     
388         this.notebox = notebox;
389
390         return;
391     },
392
393     cb_mouseout: function()
394     {
395         if (this.notebox != null) {
396             $("room_standup_orig").removeChild(this.notebox);
397             this.notebox = null;
398         }
399     }
400
401 } // wagon class end