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