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