Ticker javascript code.
[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.box.style.visibility = "hidden";            
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         this.timout = setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
248     },
249
250     animate: function()
251     {
252        this.box.style.left = (parseInt(this.box.style.left) - this.deltas)+"px";
253        if (parseInt(this.box.style.left) >= this.xend) {
254             this.timout = setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
255         }
256         else {
257             // qui destroy
258             this.box.style.left = this.anc.offsetWidth+"px";
259             this.timout = setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
260         }
261     },
262
263     widthbox_get: function()
264     {
265         return (this.box.offsetWidth);
266     },
267
268     heightbox_get: function()
269     {
270         return (this.box.offsetHeight);
271     },
272
273     widthanc_get: function()
274     {
275         return (this.anc.offsetWidth);
276     },
277
278     heightanc_get: function()
279     {
280         return (this.anc.offsetHeight);
281     }
282 } // train class end
283
284
285
286 function wagon(anc, table, title) {
287     var box;
288     var othis = this;
289     this.anc = anc;
290     
291     box = document.createElement("div");
292     box.className = "wagon";
293     box.anc = this;
294     this.table = table;
295     this.title = title;
296     box.innerHTML = "Tavolo&nbsp;"+table;
297     this.box = box;
298     this.box.setAttribute("title", unescapeHTML(title));
299     
300     addEvent(this.box, "mouseover", function() { this.anc.cb_mouseover(); } );
301     addEvent(this.box, "mouseout",  function() { this.anc.cb_mouseout(); }  );
302     addEvent(this.box, "click",     function() { this.anc.cb_click(); }     );
303
304     this.anc.box.appendChild(box);
305
306     this.w = this.widthbox_get();
307     this.h = this.heightbox_get();
308 }
309
310 wagon.prototype = {
311     prev: null,
312     next: null,
313     table: 55,
314     anc: null,
315     w: 0,
316     h: 0,
317     x: 0,
318     box: null,
319     shut_step: 0, 
320
321     width_get: function()
322     {
323         return (this.w);
324     },
325
326     height_get: function()
327     {
328         return (this.h);
329     },
330
331     widthbox_get: function()
332     {
333         return (this.box.offsetWidth);
334     },
335
336     heightbox_get: function()
337     {
338         return (this.box.offsetHeight);
339     },
340
341     widthnotebox_get: function()
342     {
343         return (this.notebox.offsetWidth);
344     },
345
346     heightnotebox_get: function()
347     {
348         return (this.notebox.offsetHeight);
349     },
350
351     left_set: function(x)
352     {
353         this.box.style.left  = x+"px";
354     },
355
356     cb_click: function()
357     { 
358         act_sitdown(this.table); 
359     },
360
361     cb_mouseover: function()
362     {
363         var notebox, deltax;
364
365         notebox = document.createElement("div");
366         notebox.className = "notebox";
367         notebox.id = "wagon_note";
368
369         notebox.innerHTML = $("table"+this.table).innerHTML;
370         $("room_standup_orig").appendChild(notebox);
371         
372         deltax = 0;
373         deltax = parseInt(getStyle(this.anc.box, "left", "left")) +
374                    parseInt(getStyle(this.box, "left", "left")) +
375                    ((this.box.offsetWidth - notebox.offsetWidth) / 2);
376         
377         notebox.style.left = deltax + "px";
378         notebox.style.top = (-10 -notebox.offsetHeight)+"px";
379         notebox.style.visibility = "visible";
380         notebox.anc = this;
381     
382         this.notebox = notebox;
383
384         return;
385     },
386
387     cb_mouseout: function()
388     {
389         if (this.notebox != null) {
390             $("room_standup_orig").removeChild(this.notebox);
391             this.notebox = null;
392         }
393     }
394
395 } // wagon class end