modificata la notify
[brisk.git] / web / commons.js
1 /*
2  *  brisk - commons.js
3  *
4  *  Copyright (C) 2006 matteo.nastasi@milug.org
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details. You should have received a
15  * copy of the GNU General Public License along with this program; if
16  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
17  * Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * $Id$
20  *
21  */
22
23 var PLAYERS_N = 3;
24
25 function $(id) { return document.getElementById(id); }
26
27 /* replacement of setInterval on IE */
28 (function(){
29     /*if not IE, do nothing*/
30     if(!document.uniqueID){return;};
31
32     /*Copy the default setInterval behavior*/
33     var nativeSetInterval = window.setInterval;
34     window.setInterval = function(fn,ms) {              
35         var param = [];
36         if(arguments.length <= 2)       {
37             return nativeSetInterval(fn,ms);
38         }
39         else {
40             for(var i=2;i<arguments.length;i+=1) {
41                 param[i-2] =  arguments[i];
42             }   
43         }
44         
45         if(typeof(fn)=='function') {
46             
47             return (function (fn,ms,param) {
48                 var fo = function () {                                                          
49                     fn.apply(window,param);
50                 };                      
51                 return nativeSetInterval(fo,ms); 
52             })(fn,ms,param);
53         }
54         else if(typeof(fn)=='string')
55         {
56             return  nativeSetInterval(fn,ms);
57         }
58         else
59         {
60             throw Error('setInterval Error\nInvalid function type');
61         };
62     };
63 })()
64
65     // var card_pos = RANGE 0 <= x < cards_ea_n
66
67 function rnd_int(min, max) {
68   return Math.floor(Math.random() * (max - min + 1) + min);
69 }
70
71 function error_images()
72 {
73     alert("GHESEMU!");
74 }
75
76 function abort_images()
77 {
78     alert("ABORTAIMAGES");
79 }
80
81 function unload_images()
82 {
83     alert("ABORTAIMAGES");
84 }
85
86 function reset_images()
87 {
88     alert("ABORTAIMAGES");
89 }
90
91 function update_images()
92 {
93     // $("imgct").innerHTML = "Immagini caricate "+g_preload_imgsz_arr[g_imgct]+"%.";
94     if (g_imgct < g_preload_img_arr.length)
95         setTimeout(preload_images, 100, g_preload_img_arr, g_imgct);
96     g_imgct++;
97     // $("imgct").innerHTML += "U";
98 }
99
100 function preload_images(arr,idx)
101 {
102     var im = new Image;
103     
104     // $("imgct").innerHTML = "Stiamo caricando "+arr[idx]+"%.<br>";
105     im.onload =   update_images;
106     im.onerror =  error_images;
107     im.onabort =  abort_images;
108     im.onunload = unload_images;
109     im.onreset =  reset_images;
110     im.src =      arr[idx];
111     // $("imgct").innerHTML += "P";
112 }
113
114 function safestatus(a)
115 {
116     try{
117         return (a.status);
118     } catch(b)
119         { return (-1); }
120 }
121
122 function createXMLHttpRequest() {
123     try { return new ActiveXObject("Msxml2.XMLHTTP");    } catch(e) {}
124     try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
125     try { return new XMLHttpRequest();                   } catch(e) {}
126     alert("XMLHttpRequest not supported");
127     return null;
128 }
129
130 function send_mesg(mesg)
131 {
132     var xhr_wr = createXMLHttpRequest();
133     // xhr_wr.open('GET', 'index_wr.php?sess='+sess+'&mesg='+encodeURIComponent(mesg), true);
134     xhr_wr.open('GET', 'index_wr.php?sess='+sess+'&mesg='+mesg, true);
135     xhr_wr.onreadystatechange = function() { return; };
136     xhr_wr.send(null);
137
138 }
139
140 /* Stat: CHAT and TABLE */
141
142 function chatt_checksend(obj,e)
143 {
144     var keynum;
145     var keychar;
146     var numcheck;
147
148     if(window.event) { // IE
149         keynum = e.keyCode;
150     }
151     else if(e.which) { // Netscape/Firefox/Opera
152         keynum = e.which;
153     }
154     // alert("OBJ: "+obj);
155     if (keynum == 13 && obj.value != "") { // Enter
156         act_chatt(obj.value);
157         obj.value = "";
158     }
159 }
160 function act_chatt(value)
161 {
162     send_mesg("chatt|"+encodeURIComponent(value));
163     /*
164     obj.disabled = true;
165     obj.value = "";
166     obj.disabled = false;
167     obj.focus();
168     */
169     return false;
170 }
171
172 /* Stat: ROOM */
173 function act_sitdown(table)
174 {
175     send_mesg("sitdown|"+table);
176 }
177
178 function act_wakeup()
179 {
180     send_mesg("wakeup");
181 }
182
183 /* Stat: TABLE  Subst: ASTA */
184 function act_asta(card,pnt)
185 {
186     send_mesg("asta|"+card+"|"+pnt);
187 }
188
189 function act_choose(card)
190 {
191     // alert("sitdown");
192     send_mesg("choose|"+card);
193 }
194
195 /* Stat: TABLE  Subst: GAME */
196 function act_play(card,x,y)
197 {
198     // alert("sitdown");
199     send_mesg("play|"+card+"|"+x+"|"+y);
200 }
201
202 function act_tableinfo()
203 {
204     send_mesg("tableinfo");
205 }
206
207 function act_help()
208 {
209     send_mesg("help");
210 }
211
212 function act_about()
213 {
214     send_mesg("about");
215 }
216
217 function safelogout()
218 {
219     var res;
220
221     res = window.confirm("Sei sicuro di volere abbandonare la partita?");
222     if (res)
223         act_logout();
224 }
225 function act_logout()
226 {
227     send_mesg("logout");
228 }
229
230 function act_reload()
231 {
232     window.onunload = null;
233     document.location.reload();
234 }
235
236 function act_shutdown()
237 {
238     var c = 0;
239
240     send_mesg("shutdown");
241     while (xhr_wr.readyState != 4)
242         c++;
243 }
244
245 function act_preout()
246 {
247     act_logout();
248 }
249
250 function postact_logout()
251 {
252     // alert("postact_logout");
253     try { 
254         xhr_rd.abort();
255     } catch (e) {}
256
257     // eraseCookie("sess");
258     document.location.assign("index.php");
259 }
260
261 /*
262   function slowimg(img,x1,y1,deltat,free,action,srcend)
263   img    - image to move
264   x1,y1  - destination coords
265   deltat - time for each frame (in msec)
266   free   - when the release the local block for other operations (range: 0 - 1)
267   action - function to run when the image is moved
268   srcend - image to switch when the image is moved
269 */
270
271 function sleep(st, delay)
272 {
273     // alert("LOC_NEW PRE: "+st.st_loc_new);
274
275     st.st_loc_new++;
276
277     setTimeout(function(obj){ if (obj.st_loc_new > obj.st_loc) { obj.st_loc++; }},
278                delay, st);
279 }
280
281 function slowimg(img,x1,y1,deltat,free,action,srcend) {
282     this.img = img;
283
284     this.x0  = parseInt(document.defaultView.getComputedStyle(this.img, "").getPropertyValue("left"));
285     // alert("img.x0 = "+this.x0);
286     this.y0  = parseInt(document.defaultView.getComputedStyle(this.img, "").getPropertyValue("top"));
287     this.x1  = x1;
288     this.y1  = y1;
289     this.deltat = deltat;
290     this.free = free;
291     this.action = action;
292     this.srcend = srcend;
293 }
294
295 slowimg.prototype = {
296     img: null, 
297     st: null,
298     x0: 0,
299     y0: 0,
300     x1: 0,
301     y1: 0,
302     dx: 0,
303     dy: 0,
304     free: 0,
305     step_n:    0,
306     step_cur:  0,
307     step_free: 0,
308     time:      0,
309     deltat:   40,
310     tout: 0,
311     action: null,
312     srcend: null,
313     
314     setstart: function(x0,y0)
315     {
316         this.x0 = x0;
317         this.y0 = y0;
318     },
319     
320     setaction: function(act)
321     {
322         this.action = act;
323     },
324     
325     settime: function(time) 
326     {
327         this.time = time;
328         this.step_n = parseInt(time / this.deltat);
329         this.dx = (this.x1 - this.x0) / this.step_n;
330         this.dy = (this.y1 - this.y0) / this.step_n;
331         if (this.step_n * this.deltat == time) {
332             this.step_n--;
333         }
334         this.step_free = parseInt(this.step_n * this.free);
335     },
336     
337     start: function(st)
338     {
339         // $("logz").innerHTML += "               xxxxxxxxxxxxxxxxxxxxxSTART<br>";
340         this.st = st;
341         this.st.st_loc_new++;
342         
343         this.img.style.visibility = "visible";
344         setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
345     },
346     
347     animate: function()
348     {
349         // $("log").innerHTML = "Val " + this.step_cur + " N: " + this.step_n + "<br>";
350         if (this.step_cur == 0) {
351             var date = new Date();
352             // $("logz").innerHTML = "Timestart: " + date + "<br>";
353         }
354         if (this.step_cur <= this.step_n) {
355             this.img.style.left = this.x0 + this.dx * this.step_cur;
356             this.img.style.top  = this.y0 + this.dy * this.step_cur;
357             this.step_cur++;
358             setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
359             if (this.step_cur == this.step_free && this.st != null) {
360                 if (this.st != null && this.st.st_loc < this.st.st_loc_new) {
361                     // alert("QUI1  " + this.step_cur + "  ZZ  "+  this.step_free);
362                     this.st.st_loc++;
363                     this.st = null;
364                 }
365             }
366         }
367         else {
368             this.img.style.left = this.x1;
369             this.img.style.top  = this.y1;
370             // $("logz").innerHTML += "xxxxxxxxxxxxxxxCLEAR<br>";
371             var date = new Date();
372             // $("logz").innerHTML += "Timestop: " + date + "<br>";
373             if (this.st != null && this.st.st_loc < this.st.st_loc_new) {
374                 // alert("QUI2");
375                 this.st.st_loc++;
376                 this.st = null;
377             }
378             if (this.action != null) {
379                 eval(this.action);
380             }
381             if (this.srcend != null) {
382                 this.img.src = this.srcend;
383             }
384         }
385     }
386 }
387
388 var asta_xarr = new Array(0,66,133);
389
390 /* TODO: impostare gli onclick */
391 function dispose_asta(idx, pnt)
392 {
393     var i, btn, pass;
394     
395     var btn;
396     for (i = 0 ; i < 10 ; i++) {
397         btn = $("asta"+i);
398         if (i < idx) {
399             btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+".png";
400             pass = -1;
401         }
402         else {
403             btn.src = "img/asta"+i+(pnt >= 0 ? "" : "_ro")+".png";
404             pass = i;
405         }
406         if (i < 19)
407             btn.style.left = asta_xarr[i % 3];
408         else
409             btn.style.left = asta_xarr[(i+1) % 3];
410         
411         btn.style.top  = parseInt(i / 3) * 50+1;
412         // btn.style.visibility  = "visible";
413         
414         if (pnt >= 0)
415             eval("btn.onclick = function () { act_asta("+pass+",61); }");
416         else
417             btn.onclick = null;
418     }
419     
420     
421     btn = $("astaptdiv");
422     btn.style.left = asta_xarr[i % 3];
423     btn.style.top = parseInt(i / 3) * 50;
424     // btn.style.visibility  = "visible";
425     
426     btn = $("astapt");
427     var rpnt = (pnt < 0 ? -pnt : pnt);
428     btn.value = (rpnt < 61 ? 61 : (rpnt > 120 ? 120 : rpnt));
429     
430     btn = $("astaptsub");
431     btn.style.left = asta_xarr[i % 3];
432     btn.style.top = 25 + parseInt(i / 3) * 50;;
433     btn.src = "img/astaptsub"+(pnt >= 0 ? "" : "_ro")+".png";
434     // btn.style.visibility  = "visible";
435     if (pnt >= 0)
436         btn.onclick = function () { act_asta(9,$("astapt").value); };
437     else
438         btn.onclick = null;
439     
440     i+=1;
441     btn = $("astapasso2");
442     btn.style.left = asta_xarr[i % 3];
443     btn.style.top = parseInt(i / 3) * 50;;
444     btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+".png";
445     // btn.style.visibility  = "visible";
446     if (pnt >= 0)
447         btn.onclick = function () { act_asta(-1,0); };
448     else
449         btn.onclick = null;
450     $("asta").style.visibility = "visible";
451 }
452
453 function hide_asta()
454 {
455     $("asta").style.visibility = "hidden"; 
456 }
457
458
459 function notify(st, text, tout, butt, w, h)
460 {
461     var clo, box;
462     var t = this;
463     
464     this.st = st;
465
466     this.ancestor = document.body;
467     
468     this.st.st_loc_new++;
469
470     clo = document.createElement("input");
471     clo.type = "submit";
472     clo.className = "button";
473     clo.style.bottom = "4px";
474     clo.value = butt;
475     clo.obj = this;
476     clo.onclick = this.input_hide;
477     
478     clodiv = document.createElement("div");
479     clodiv.className = "notify_clo";
480     clodiv.appendChild(clo);
481
482     box = document.createElement("div");
483     box.className = "notify";
484     box.innerHTML = text;
485     box.style.zIndex = 200;
486     box.style.width  = w+"px";
487     box.style.height = h+"px";
488     box.appendChild(clodiv);
489     box.style.visibility = "visible";
490
491     this.notitag = box;
492     
493     this.ancestor.appendChild(box);
494     
495     this.toutid = setTimeout(function(obj){ obj.unblock(); }, tout, this);
496 }
497
498 notify.prototype = {
499     ancestor: null,
500     st: null,
501     notitag: null,
502     toutid: null,
503     
504     unblock: function()
505     {
506         if (this.st.st_loc < this.st.st_loc_new) {
507             this.st.st_loc++;
508         }
509     },
510     
511     hide: function()
512     {
513         clearTimeout(this.toutid);
514         this.ancestor.removeChild(this.notitag);
515         this.unblock();
516     },
517
518     input_hide: function()
519     {
520         clearTimeout(this.obj.toutid);
521         this.obj.ancestor.removeChild(this.obj.notitag);
522         this.obj.unblock();
523     }
524 }
525         
526
527 function $(id) { 
528     return document.getElementById(id); 
529 }
530
531 function globst() {
532     this.st = -1;
533     this.st_loc = -1;
534     this.st_loc_new = -1;
535     this.comms  = new Array;
536 }
537
538
539
540 function remark_step()
541 {
542     var ct = $("remark").l_remct;
543     
544     if (ct != 0) {
545         ct++;
546         if (ct > 2)
547             ct = 1;
548         $("remark").className = "remark"+ct;
549         $("remark").l_remct = ct;
550         setTimeout(remark_step,500);
551     }
552     else
553         $("remark").className = "remark0";
554     
555     return;
556 }
557
558 function remark_on()
559 {
560     if ($("remark").l_remct == 0) {
561         $("remark").l_remct = 1;
562         setTimeout(remark_step,500);
563     }
564 }
565
566 function remark_off()
567 {
568     $("remark").l_remct = 0;
569     $("remark").className = "remark0";
570 }
571
572
573 function choose_seed(card)
574 {
575     var i;
576
577     $("chooseed").style.visibility = "visible";
578     for (i = 0 ; i < 4 ; i++) {
579         $("seed"+i).src = "img/"+i+""+card+".png";
580         seed=$("seed"+i);
581         eval("seed.onclick = function () { act_choose("+i+""+card+"); };");
582     }
583 }
584
585 function set_names(so,ea,ne,nw,we)
586 {
587 //    alert("EA: "+ea);
588     $("name").innerHTML = so; 
589     $("name").title = so; 
590     $("name_ea").innerHTML = ea;
591     $("name_ea").title = ea;
592     $("name_ne").innerHTML = ne;
593     $("name_ne").title = ne;
594     $("name_nw").innerHTML = nw;
595     $("name_nw").title = nw;
596     $("name_we").innerHTML = we;
597     $("name_we").title = we;
598
599     return;
600 }
601
602 var astat_suffix = new Array("","_ea","_ne","_nw","_we");
603
604 function show_astat(zer,uno,due,tre,qua)
605 {
606     var astat = new Array(zer,uno,due,tre,qua);
607
608     for (i = 0 ; i < PLAYERS_N ; i++) {
609         idx = (PLAYERS_N + i - table_pos) % PLAYERS_N;
610
611         if (astat[i] == -2) {
612             $("public"+astat_suffix[idx]).style.visibility = "hidden";
613         }
614         else if (astat[i] == -1) {
615             $("public"+astat_suffix[idx]).style.visibility = "visible";
616             $("pubacard"+astat_suffix[idx]).src = "img/astapasso.png";
617             $("pubapnt"+astat_suffix[idx]).innerHTML = "";
618             $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
619         }
620         else if (astat[i] <= 10) {
621             $("public"+astat_suffix[idx]).style.visibility = "visible";
622             $("pubacard"+astat_suffix[idx]).src = "img/asta"+astat[i]+".png";
623             $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
624         }
625         else if (astat[i] <= 120) {
626             $("public"+astat_suffix[idx]).style.visibility = "visible";
627             $("pubacard"+astat_suffix[idx]).src = "img/asta9.png";
628             $("pubapnt"+astat_suffix[idx]).style.visibility = "inherit"; // XXX VISIBLE
629             $("pubapnt"+astat_suffix[idx]).innerHTML = astat[i];
630         }
631     }
632 }
633
634 var fin = 0;
635
636 function table_init() {
637     var sux = new Array("", "_ea", "_ne", "_nw", "_we");
638
639     remark_off();
640
641     $("asta").style.visibility = "hidden";
642     $("caller").style.visibility = "hidden";
643     show_astat(-2,-2,-2,-2,-2);
644
645     for (i=0 ; i < 8 ; i++) {
646         Drag.init($("card" + i), card_mouseup_cb);
647         for (e = 0 ; e < PLAYERS_N ; e++)
648             $("card"+sux[e]+i).style.visibility = "hidden";
649     }
650     for (i=0 ; i < PLAYERS_N ; i++) {
651         $("takes"+sux[i]).style.visibility = "hidden";
652         }
653
654     for (i = 0 ; i < 8 ; i++) {
655         cards_pos[i] = i;
656         cards_ea_pos[i] = i;
657         cards_ne_pos[i] = i;
658         cards_nw_pos[i] = i;
659         cards_we_pos[i] = i;
660     }
661
662 }
663   
664
665
666 var chatt_lines = new Array();
667 var chatt_lines_n = 0;
668
669 /* PRO CHATT */
670 function chatt_sub(name,str)
671 {
672   // alert("ARRIVA NAME: "+ name + "  STR:"+str);
673   if (chatt_lines_n == 20) {
674     $("txt").innerHTML = "";
675     for (i = 0 ; i < 19 ; i++) {
676       chatt_lines[i] = chatt_lines[i+1];
677       $("txt").innerHTML += chatt_lines[i];
678     }
679     chatt_lines[i] = "<b>"+name+"</b> "+str+ "<br>";
680     $("txt").innerHTML += chatt_lines[i];
681   }
682   else {
683     chatt_lines[chatt_lines_n] = "<b>"+name+"</b> "+str+ "<br>";
684     $("txt").innerHTML += chatt_lines[chatt_lines_n];
685     chatt_lines_n++;
686   }
687   $("txt").innerHTML;
688   $("txt").scrollTop = 10000000;
689 }
690
691 /*
692  *  GESTIONE DEI COOKIES
693  */
694 function createCookie(name,value,hours,path) {
695         if (hours) {
696                 var date = new Date();
697                 date.setTime(date.getTime()+(hours*60*60*1000));
698                 var expires = "; expires="+date.toGMTString();
699         }
700         else var expires = "";
701         document.cookie = name+"="+value+expires+"; path="+path;
702 }
703
704 function readCookie(name) {
705         var nameEQ = name + "=";
706         var ca = document.cookie.split(';');
707         for(var i=0;i < ca.length;i++) {
708                 var c = ca[i];
709                 while (c.charAt(0)==' ') c = c.substring(1,c.length);
710                 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
711         }
712         return null;
713 }
714
715 function eraseCookie(name) {
716         createCookie(name,"",-1);
717 }
718
719 var onunload_times = 0;
720
721
722 function onunload_cb () {
723     var u = 0;
724     if (onunload_times == 0) {
725         var res = window.confirm("    Vuoi veramente abbandonare la briscola ?\n(clicca annulla o cancel se vuoi ricaricare la briscola)");
726         if (res == true) {
727             the_end = true; 
728             act_shutdown();
729             while (1) 
730                 u++;
731         }
732         else {
733             try {
734                 location = self.location;
735             } catch (e) {
736                 alert("Ripristino della briscola fallito, per non perdere la sessione ricaricare la pagina manualmente.");
737             }
738         }
739         onunload_times++;
740     }
741     
742     return(false);
743 }
744
745
746 function room_checkspace(emme,tables,inpe)
747 {
748     nome = "<b>";
749     for (i = 0 ; i < emme ; i++) 
750         nome += "m";
751     nome += "</b>";
752
753     alta = "";
754     for (i = 0 ; i < 5 ; i++) 
755         alta += nome+"<br>";
756
757     for (i = 0 ; i < tables ; i++) {
758         $("table"+i).innerHTML = alta;
759         $("table_act"+i).innerHTML = "<input type=\"button\" class=\"button\" name=\"xhenter"+i+"\"  value=\"Mi siedo.\" onclick=\"act_sitdown(1);\">";
760         }
761
762     stand = "<table class=\"table_standup\"><tbody><tr>";
763     for (i = 0 ; i < inpe ; i++) {
764         stand += "<td class=\"td_standup\">"+nome+"</td>";
765         if ((i+1) % 4 == 0) {
766             stand += "</tr><tr>";
767         }
768     }
769     stand += "</tr>";
770     $("standup").innerHTML = stand;
771
772     $("esco").innerHTML = "<input name=\"logout\" type=\"button\" value=\"Esco.\" onclick=\"window.onunload = null; act_logout();\" type=\"button\">";
773 }