8c057bbdd92b8351edea6baa7db5901c5bdea905
[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: 250,
53     deltas: 10,
54     xend: 0,
55     timout: null,
56     clickable: true,
57
58     show: function()
59     {
60         $('tickbut').style.visibility = "hidden";
61         $('tickbut').style.width = "0px";
62
63         this.clickable = true;
64         this.box.style.visibility = "visible";
65     },
66
67     hide: function()
68     {
69         $('tickbut').style.visibility = "visible";
70         $('tickbut').style.width = "20px";
71
72         this.clickable = false;
73         for (cur = this.first ; cur != null ; cur = cur.next) {
74             if (cur.notebox != null) {
75                 cur.cb_mouseout();
76             }
77         }
78            
79     },
80
81     hide_noauth: function()
82     {
83         for (cur = this.first ; cur != null ; cur = cur.next) {
84             if (cur.table >= g_tables_auth_n) {
85                 cur.hide();
86             }
87         }           
88     },
89
90     show_noauth: function()
91     {
92         for (cur = this.first ; cur != null ; cur = cur.next) {
93             if (cur.table >= g_tables_auth_n) {
94                 cur.show();
95             }
96         }           
97     },
98
99     add: function(table, title)
100     {
101         var last, wag, curx;
102         var dostart = 0;
103
104         for (cur = this.first ; cur != null ; cur = cur.next) {
105             if (cur.table == table)
106                 return;
107         }
108
109         wag = new wagon(this, table, title);
110         if (this.first == null) {
111             this.first = wag;
112             dostart = 1;
113         }
114         else {
115             for (cur = this.first ; cur.next != null ; cur = cur.next);
116             cur.next = wag;
117         }
118
119         this.redraw();
120         this.xend = -this.widthbox_get();
121
122         if (dostart) {
123             this.start();
124         }
125
126     },
127
128     rem: function(table)
129     {
130         var prev = null, remo = null;
131
132         if (this.first == null) {
133             return;
134         }
135    
136         if (this.first.table == table) {
137             remo = this.first;
138         }
139
140         for (cur = this.first ; cur != null ; cur = cur.next) {
141             // recalculate the width and replace wagons
142             if (cur.table == table) {
143                 remo = cur;
144                 break;
145             }
146         }
147
148         if (remo != null) {
149             remo.box.className = "wagon_disable";
150             removeEvent(remo.box, "mouseover", function() { this.anc.cb_mouseover(); } );
151             removeEvent(remo.box, "click",     function() { this.anc.cb_click(); } );
152             setTimeout(function(){ arguments[0][0].shut_wagon(arguments[0]); }, 3000, [ this, remo ]);
153         }
154
155         this.redraw();
156     },
157
158     rem_obj: function(obj)
159     {
160         var prev = null, remo = null;
161
162         if (this.first == null) {
163             return;
164         }
165    
166
167         if (this.first == obj) {
168             remo = this.first;
169             this.first = this.first.next;
170         }
171
172         for (cur = this.first ; cur != null ; cur = cur.next) {
173             // recalculate the width and replace wagons
174             if (cur == obj) {
175                 remo = cur;
176                 
177                 if (prev != null) {
178                    prev.next = cur.next;
179                 }
180                 break;
181             }
182             prev = cur;
183         }
184
185         this.redraw();
186
187         if (this.first == null) {
188             clearTimeout(this.timout);
189             this.timout = null;
190         }
191     },
192
193     stop_move: function()
194     {
195         this.deltas = 0;
196     },
197
198     start_move: function()
199     {
200         this.deltas = 10;
201     },
202
203     shut_wagon: function(args)
204     {
205         var curw;
206
207         obj = arguments[0][0];
208         wag = arguments[0][1];
209
210         if (wag.shut_step == 0) {
211             wag.box.className = "wagon_disable";
212             wag.shut_step = 1;
213         }
214         else {
215             if (wag.shut_step == 1) {
216                 wag.w = wag.widthbox_get();
217                 wag.box.className = "wagon_disable2";
218                 wag.box.style.padding = "0px";
219                 wag.box.style.width =  (wag.w-2)+"px";
220                 wag.box.style.height = (wag.h-2)+"px"; // 2 for border width
221                 wag.table = "";
222                 wag.box.innerHTML = "";
223                 wag.shut_step = 2;
224             }
225             curw = wag.widthbox_get() - 10;
226             wag.w = curw + 2; // 2 for border pixels
227             if (curw <= 0) {
228                 wag.cb_mouseout();
229
230                 obj.box.removeChild(wag.box);
231                 obj.rem_obj(wag);
232
233                 return;
234             }
235             else {
236                 wag.box.style.width = curw+"px";
237                 wag.box.style.padding = "0px";
238             }
239         }
240         this.redraw();
241         setTimeout(function(){ arguments[0][0].shut_wagon(arguments[0]);  }, 250, [ obj, wag ]);
242     },
243
244     redraw: function()
245     {
246         var maxw = 0, maxh = 0, curh;
247
248         for (cur = this.first ; cur != null ; cur = cur.next) {
249             // recalculate the width and replace wagons
250             maxw += 2 + (maxw == 0 ? 0 : 2) + cur.width_get();
251             curh = cur.height_get();
252             maxh = (maxh < curh ? curh : maxh);
253         }
254         maxh += 2;
255         curx = 0;
256         
257         for (cur = this.first ; cur != null ; cur = cur.next) {
258             // recalculate the width and replace wagons
259             cur.left_set(curx);
260             curx += cur.width_get() + 4;
261         }
262
263         this.box.style.width = maxw+"px";
264         this.box.style.height = maxh+"px";
265     },
266
267     resetx: function()
268     {
269         this.deltas = 10;
270         this.clickable = true;
271
272         this.box.style.left = this.anc.offsetWidth+"px";
273     },
274
275
276     start: function()
277     {
278         this.resetx();
279         if (this.timout == null) {
280             this.timout = setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
281         }
282     },
283
284     animate: function()
285     {
286        this.box.style.left = (parseInt(this.box.style.left) - this.deltas)+"px";
287
288 //        if (parseInt(this.box.style.left) >= this.xend) {
289 //             this.timout = setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
290 //         }
291 //         else {
292 //             this.box.style.left = this.anc.offsetWidth+"px";
293 //             this.timout = setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
294 //         }
295        if (parseInt(this.box.style.left) < this.xend) {
296            this.box.style.left = this.anc.offsetWidth+"px";
297        }
298        this.timout = setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
299     },
300
301     widthbox_get: function()
302     {
303         return (this.box.offsetWidth);
304     },
305
306     heightbox_get: function()
307     {
308         return (this.box.offsetHeight);
309     },
310
311     widthanc_get: function()
312     {
313         return (this.anc.offsetWidth);
314     },
315
316     heightanc_get: function()
317     {
318         return (this.anc.offsetHeight);
319     }
320 } // train class end
321
322
323
324 function wagon(anc, table, title) {
325     var box;
326     var othis = this;
327     this.anc = anc;
328     
329     box = document.createElement("div");
330     box.className = "wagon";
331     box.anc = this;
332     this.table = table;
333     this.title = title;
334     box.innerHTML = "Tavolo&nbsp;"+table;
335     this.box = box;
336     this.box.setAttribute("title", unescapeHTML(title));
337     
338     addEvent(this.box, "mouseover", function() { this.anc.cb_mouseover(); } );
339     addEvent(this.box, "mouseout",  function() { this.anc.cb_mouseout(); }  );
340     addEvent(this.box, "click",     function() { this.anc.cb_click(); }     );
341
342     
343     if (g_listen & l_list_isol && table >= g_tables_auth_n) {
344         this.box.style.visibility = "hidden";
345     }
346     this.anc.box.appendChild(box);
347
348     this.w = this.widthbox_get();
349     this.h = this.heightbox_get();
350 }
351
352 wagon.prototype = {
353     prev: null,
354     next: null,
355     table: 55,
356     anc: null,
357     w: 0,
358     h: 0,
359     x: 0,
360     box: null,
361     shut_step: 0, 
362
363     show: function()
364     {
365         this.box.style.visibility = "visible";
366     },
367
368     hide: function()
369     {
370         this.box.style.visibility = "hidden";
371     },
372     
373
374     width_get: function()
375     {
376         return (this.w);
377     },
378
379     height_get: function()
380     {
381         return (this.h);
382     },
383
384     widthbox_get: function()
385     {
386         return (this.box.offsetWidth);
387     },
388
389     heightbox_get: function()
390     {
391         return (this.box.offsetHeight);
392     },
393
394     widthnotebox_get: function()
395     {
396         return (this.notebox.offsetWidth);
397     },
398
399     heightnotebox_get: function()
400     {
401         return (this.notebox.offsetHeight);
402     },
403
404     left_set: function(x)
405     {
406         this.box.style.left  = x+"px";
407     },
408
409     cb_click: function()
410     { 
411         if (this.anc.clickable == true) {
412             act_sitdown(this.table);
413         }
414     },
415
416     cb_mouseover: function()
417     {
418         var notebox, deltax;
419
420         notebox = document.createElement("div");
421         notebox.className = "notebox";
422         notebox.id = "wagon_note";
423
424         notebox.innerHTML = $("table"+this.table).innerHTML;
425         $("room_standup_orig").appendChild(notebox);
426         
427         deltax = 0;
428         deltax = parseInt(getStyle(this.anc.box, "left", "left")) +
429                    parseInt(getStyle(this.box, "left", "left")) +
430                    ((this.box.offsetWidth - notebox.offsetWidth) / 2);
431         
432         notebox.style.left = deltax + "px";
433         notebox.style.top = (-10 -notebox.offsetHeight)+"px";
434         notebox.style.visibility = "visible";
435         notebox.anc = this;
436     
437         this.notebox = notebox;
438
439         return;
440     },
441
442     cb_mouseout: function()
443     {
444         if (this.notebox != null) {
445             $("room_standup_orig").removeChild(this.notebox);
446             this.notebox = null;
447         }
448     }
449
450 } // wagon class end