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