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