Initial revision
[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  */
20
21 var PLAYERS_N = 3;
22
23 function $(id) { return document.getElementById(id); }
24
25 /* replacement of setInterval on IE */
26 (function(){
27     /*if not IE, do nothing*/
28     if(!document.uniqueID){return;};
29
30     /*Copy the default setInterval behavior*/
31     var nativeSetInterval = window.setInterval;
32     window.setInterval = function(fn,ms) {              
33         var param = [];
34         if(arguments.length <= 2)       {
35             return nativeSetInterval(fn,ms);
36         }
37         else {
38             for(var i=2;i<arguments.length;i+=1) {
39                 param[i-2] =  arguments[i];
40             }   
41         }
42         
43         if(typeof(fn)=='function') {
44             
45             return (function (fn,ms,param) {
46                 var fo = function () {                                                          
47                     fn.apply(window,param);
48                 };                      
49                 return nativeSetInterval(fo,ms); 
50             })(fn,ms,param);
51         }
52         else if(typeof(fn)=='string')
53         {
54             return  nativeSetInterval(fn,ms);
55         }
56         else
57         {
58             throw Error('setInterval Error\nInvalid function type');
59         };
60     };
61 })()
62
63     // var card_pos = RANGE 0 <= x < cards_ea_n
64
65 function rnd_int(min, max) {
66   return Math.floor(Math.random() * (max - min + 1) + min);
67 }
68
69 function error_images()
70 {
71     alert("GHESEMU!");
72 }
73
74 function abort_images()
75 {
76     alert("ABORTAIMAGES");
77 }
78
79 function unload_images()
80 {
81     alert("ABORTAIMAGES");
82 }
83
84 function reset_images()
85 {
86     alert("ABORTAIMAGES");
87 }
88
89 function update_images()
90 {
91     $("imgct").innerHTML = "Immagini caricate "+g_preload_imgsz_arr[g_imgct]+"%.";
92     if (g_imgct < g_preload_img_arr.length)
93         setTimeout(preload_images, 100, g_preload_img_arr, g_imgct);
94     g_imgct++;
95     // $("imgct").innerHTML += "U";
96 }
97
98 function preload_images(arr,idx)
99 {
100     var im = new Image;
101     
102     // $("imgct").innerHTML = "Stiamo caricando "+arr[idx]+"%.<br>";
103     im.onload =   update_images;
104     im.onerror =  error_images;
105     im.onabort =  abort_images;
106     im.onunload = unload_images;
107     im.onreset =  reset_images;
108     im.src =      arr[idx];
109     // $("imgct").innerHTML += "P";
110 }
111
112 function safestatus(a)
113 {
114     try{
115         return (a.status);
116     } catch(b)
117         { return (-1); }
118 }
119
120 function createXMLHttpRequest() {
121     try { return new ActiveXObject("Msxml2.XMLHTTP");    } catch(e) {}
122     try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
123     try { return new XMLHttpRequest();                   } catch(e) {}
124     alert("XMLHttpRequest not supported");
125     return null;
126 }
127
128 function send_mesg(mesg)
129 {
130     var xhr_wr = createXMLHttpRequest();
131     // xhr_wr.open('GET', 'index_wr.php?sess='+sess+'&mesg='+encodeURIComponent(mesg), true);
132     xhr_wr.open('GET', 'index_wr.php?sess='+sess+'&mesg='+mesg, true);
133     xhr_wr.onreadystatechange = function() { return; };
134     xhr_wr.send(null);
135
136 }
137
138 /* Stat: CHAT and TABLE */
139
140 function act_chatt()
141 {
142     var obj = $("txt_in");
143     if (obj.value == "")
144         return (false);
145     send_mesg("chatt|"+encodeURIComponent(obj.value));
146     obj.disabled = true;
147     obj.value = "";
148     obj.disabled = false;
149     obj.focus();
150
151     return false;
152 }
153
154 /* Stat: ROOM */
155 function act_sitdown(table)
156 {
157     send_mesg("sitdown|"+table);
158 }
159
160 function act_wakeup()
161 {
162     send_mesg("wakeup");
163 }
164
165 /* Stat: TABLE  Subst: ASTA */
166 function act_asta(card,pnt)
167 {
168     send_mesg("asta|"+card+"|"+pnt);
169 }
170
171 function act_choose(card)
172 {
173     // alert("sitdown");
174     send_mesg("choose|"+card);
175 }
176
177 /* Stat: TABLE  Subst: GAME */
178 function act_play(card,x,y)
179 {
180     // alert("sitdown");
181     send_mesg("play|"+card+"|"+x+"|"+y);
182 }
183
184 function act_tableinfo()
185 {
186     send_mesg("tableinfo");
187 }
188
189 function act_logout()
190 {
191     send_mesg("logout");
192 }
193
194 function act_preout()
195 {
196     act_logout();
197 }
198
199 function postact_logout()
200 {
201     // alert("postact_logout");
202     try { 
203         xhr_rd.abort();
204     } catch (e) {}
205
206     eraseCookie("sess");
207     document.location.assign("index.php");
208 }
209
210 /*
211   function slowimg(img,x1,y1,deltat,free,action,srcend)
212   img    - image to move
213   x1,y1  - destination coords
214   deltat - time for each frame (in msec)
215   free   - when the release the local block for other operations (range: 0 - 1)
216   action - function to run when the image is moved
217   srcend - image to switch when the image is moved
218 */
219
220 function sleep(st, delay)
221 {
222     // alert("LOC_NEW PRE: "+st.st_loc_new);
223
224     st.st_loc_new++;
225
226     setTimeout(function(obj){ if (obj.st_loc_new > obj.st_loc) { obj.st_loc++; }},
227                delay, st);
228 }
229
230 function slowimg(img,x1,y1,deltat,free,action,srcend) {
231     this.img = img;
232
233     this.x0  = parseInt(window.getComputedStyle(this.img, "").getPropertyValue("left"));
234     // alert("img.x0 = "+this.x0);
235     this.y0  = parseInt(window.getComputedStyle(this.img, "").getPropertyValue("top"));
236     this.x1  = x1;
237     this.y1  = y1;
238     this.deltat = deltat;
239     this.free = free;
240     this.action = action;
241     this.srcend = srcend;
242 }
243
244 slowimg.prototype = {
245     img: null, 
246     st: null,
247     x0: 0,
248     y0: 0,
249     x1: 0,
250     y1: 0,
251     dx: 0,
252     dy: 0,
253     free: 0,
254     step_n:    0,
255     step_cur:  0,
256     step_free: 0,
257     time:      0,
258     deltat:   40,
259     tout: 0,
260     action: null,
261     srcend: null,
262     
263     setstart: function(x0,y0)
264     {
265         this.x0 = x0;
266         this.y0 = y0;
267     },
268     
269     setaction: function(act)
270     {
271         this.action = act;
272     },
273     
274     settime: function(time) 
275     {
276         this.time = time;
277         this.step_n = parseInt(time / this.deltat);
278         this.dx = (this.x1 - this.x0) / this.step_n;
279         this.dy = (this.y1 - this.y0) / this.step_n;
280         if (this.step_n * this.deltat == time) {
281             this.step_n--;
282         }
283         this.step_free = parseInt(this.step_n * this.free);
284     },
285     
286     start: function(st)
287     {
288         // $("logz").innerHTML += "               xxxxxxxxxxxxxxxxxxxxxSTART<br>";
289         this.st = st;
290         this.st.st_loc_new++;
291         
292         this.img.style.visibility = "visible";
293         setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
294     },
295     
296     animate: function()
297     {
298         // $("log").innerHTML = "Val " + this.step_cur + " N: " + this.step_n + "<br>";
299         if (this.step_cur == 0) {
300             var date = new Date();
301             // $("logz").innerHTML = "Timestart: " + date + "<br>";
302         }
303         if (this.step_cur <= this.step_n) {
304             this.img.style.left = this.x0 + this.dx * this.step_cur;
305             this.img.style.top  = this.y0 + this.dy * this.step_cur;
306             this.step_cur++;
307             setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
308             if (this.step_cur == this.step_free && this.st != null) {
309                 if (this.st != null && this.st.st_loc < this.st.st_loc_new) {
310                     // alert("QUI1  " + this.step_cur + "  ZZ  "+  this.step_free);
311                     this.st.st_loc++;
312                     this.st = null;
313                 }
314             }
315         }
316         else {
317             this.img.style.left = this.x1;
318             this.img.style.top  = this.y1;
319             // $("logz").innerHTML += "xxxxxxxxxxxxxxxCLEAR<br>";
320             var date = new Date();
321             // $("logz").innerHTML += "Timestop: " + date + "<br>";
322             if (this.st != null && this.st.st_loc < this.st.st_loc_new) {
323                 // alert("QUI2");
324                 this.st.st_loc++;
325                 this.st = null;
326             }
327             if (this.action != null) {
328                 eval(this.action);
329             }
330             if (this.srcend != null) {
331                 this.img.src = this.srcend;
332             }
333         }
334     }
335 }
336
337 var asta_xarr = new Array(0,66,133);
338
339 /* TODO: impostare gli onclick */
340 function dispose_asta(idx, pnt)
341 {
342     var i, btn, pass;
343     
344     var btn;
345     for (i = 0 ; i < 10 ; i++) {
346         btn = $("asta"+i);
347         if (i < idx) {
348             btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+".png";
349             pass = -1;
350         }
351         else {
352             btn.src = "img/asta"+i+(pnt >= 0 ? "" : "_ro")+".png";
353             pass = i;
354         }
355         if (i < 19)
356             btn.style.left = asta_xarr[i % 3];
357         else
358             btn.style.left = asta_xarr[(i+1) % 3];
359         
360         btn.style.top  = parseInt(i / 3) * 50+1;
361         // btn.style.visibility  = "visible";
362         
363         if (pnt >= 0)
364             eval("btn.onclick = function () { act_asta("+pass+",61); }");
365         else
366             btn.onclick = null;
367     }
368     
369     
370     btn = $("astaptdiv");
371     btn.style.left = asta_xarr[i % 3];
372     btn.style.top = parseInt(i / 3) * 50;
373     // btn.style.visibility  = "visible";
374     
375     btn = $("astapt");
376     var rpnt = (pnt < 0 ? -pnt : pnt);
377     btn.value = (rpnt < 61 ? 61 : (rpnt > 120 ? 120 : rpnt));
378     
379     btn = $("astaptsub");
380     btn.style.left = asta_xarr[i % 3];
381     btn.style.top = 25 + parseInt(i / 3) * 50;;
382     btn.src = "img/astaptsub"+(pnt >= 0 ? "" : "_ro")+".png";
383     // btn.style.visibility  = "visible";
384     if (pnt >= 0)
385         btn.onclick = function () { act_asta(9,$("astapt").value); };
386     else
387         btn.onclick = null;
388     
389     i+=1;
390     btn = $("astapasso2");
391     btn.style.left = asta_xarr[i % 3];
392     btn.style.top = parseInt(i / 3) * 50;;
393     btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+".png";
394     // btn.style.visibility  = "visible";
395     if (pnt >= 0)
396         btn.onclick = function () { act_asta(-1,0); };
397     else
398         btn.onclick = null;
399     $("asta").style.visibility = "visible";
400 }
401
402 function hide_asta()
403 {
404     $("asta").style.visibility = "hidden"; 
405 }
406
407 function notify(st, ancestor, text, tout, butt)
408 {
409     var clo, box;
410     var t = this;
411     
412     this.st = st;
413     this.ancestor = ancestor;
414     
415     this.st.st_loc_new++;
416
417     clo = document.createElement("input");
418     clo.type = "submit";
419     clo.value = butt;
420     clo.obj = this;
421     clo.onclick = this.input_hide;
422
423     box = document.createElement("div");
424     box.className = "notify";
425     box.innerHTML = text;
426     box.style.zIndex = 200;
427     box.appendChild(clo);
428     box.style.visibility = "visible";
429     
430     this.notitag = box;
431     
432     this.ancestor.appendChild(box);
433     
434     this.toutid = setTimeout(function(obj){ obj.unblock(); }, tout, this);
435 }
436
437 notify.prototype = {
438     ancestor: null,
439     st: null,
440     notitag: null,
441     toutid: null,
442     
443     unblock: function()
444     {
445         if (this.st.st_loc < this.st.st_loc_new) {
446             this.st.st_loc++;
447         }
448     },
449     
450     hide: function()
451     {
452         clearTimeout(this.toutid);
453         this.ancestor.removeChild(this.notitag);
454         this.unblock();
455     },
456
457     input_hide: function()
458     {
459         clearTimeout(this.obj.toutid);
460         this.obj.ancestor.removeChild(this.obj.notitag);
461         this.obj.unblock();
462     }
463 }
464         
465
466 function $(id) { 
467     return document.getElementById(id); 
468 }
469
470 function globst() {
471     this.st = -1;
472     this.st_loc = -1;
473     this.st_loc_new = -1;
474     this.comms  = new Array;
475 }
476
477
478
479 function remark_step()
480 {
481     var ct = $("remark").l_remct;
482     
483     if (ct != 0) {
484         ct++;
485         if (ct > 2)
486             ct = 1;
487         $("remark").className = "remark"+ct;
488         $("remark").l_remct = ct;
489         setTimeout(remark_step,500);
490     }
491     else
492         $("remark").className = "remark0";
493     
494     return;
495 }
496
497 function remark_on()
498 {
499     if ($("remark").l_remct == 0) {
500         $("remark").l_remct = 1;
501         setTimeout(remark_step,500);
502     }
503 }
504
505 function remark_off()
506 {
507     $("remark").l_remct = 0;
508     $("remark").className = "remark0";
509 }
510
511
512 function choose_seed(card)
513 {
514     var i;
515
516     $("chooseed").style.visibility = "visible";
517     for (i = 0 ; i < 4 ; i++) {
518         $("seed"+i).src = "img/"+i+""+card+".png";
519         seed=$("seed"+i);
520         eval("seed.onclick = function () { act_choose("+i+""+card+"); };");
521     }
522 }
523
524 function set_names(so,ea,ne,nw,we)
525 {
526 //    alert("EA: "+ea);
527     $("name").innerHTML = so; 
528     $("name_ea").innerHTML = ea;
529     $("name_ne").innerHTML = ne;
530     $("name_nw").innerHTML = nw;
531     $("name_we").innerHTML = we;
532     return;
533 }
534
535 var astat_suffix = new Array("","_ea","_ne","_nw","_we");
536
537 function show_astat(zer,uno,due,tre,qua)
538 {
539     var astat = new Array(zer,uno,due,tre,qua);
540
541     for (i = 0 ; i < PLAYERS_N ; i++) {
542         idx = (PLAYERS_N + i - table_pos) % PLAYERS_N;
543
544         if (astat[i] == -2) {
545             $("public"+astat_suffix[idx]).style.visibility = "hidden";
546         }
547         else if (astat[i] == -1) {
548             $("public"+astat_suffix[idx]).style.visibility = "visible";
549             $("pubacard"+astat_suffix[idx]).src = "img/astapasso.png";
550             $("pubapnt"+astat_suffix[idx]).innerHTML = "";
551             $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
552         }
553         else if (astat[i] <= 10) {
554             $("public"+astat_suffix[idx]).style.visibility = "visible";
555             $("pubacard"+astat_suffix[idx]).src = "img/asta"+astat[i]+".png";
556             $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
557         }
558         else if (astat[i] <= 120) {
559             $("public"+astat_suffix[idx]).style.visibility = "visible";
560             $("pubacard"+astat_suffix[idx]).src = "img/asta9.png";
561             $("pubapnt"+astat_suffix[idx]).style.visibility = "inherit"; // XXX VISIBLE
562             $("pubapnt"+astat_suffix[idx]).innerHTML = astat[i];
563         }
564     }
565 }
566
567 var fin = 0;
568
569 function table_init() {
570     var sux = new Array("", "_ea", "_ne", "_nw", "_we");
571
572     remark_off();
573
574     $("asta").style.visibility = "hidden";
575     $("caller").style.visibility = "hidden";
576     show_astat(-2,-2,-2,-2,-2);
577
578     for (i=0 ; i < 8 ; i++) {
579         Drag.init($("card" + i), card_mouseup_cb);
580         for (e = 0 ; e < PLAYERS_N ; e++)
581             $("card"+sux[e]+i).style.visibility = "hidden";
582     }
583     for (i=0 ; i < PLAYERS_N ; i++) {
584         $("takes"+sux[i]).style.visibility = "hidden";
585         }
586
587     for (i = 0 ; i < 8 ; i++) {
588         cards_pos[i] = i;
589         cards_ea_pos[i] = i;
590         cards_ne_pos[i] = i;
591         cards_nw_pos[i] = i;
592         cards_we_pos[i] = i;
593     }
594
595 }
596   
597
598
599 var chatt_lines = new Array();
600 var chatt_lines_n = 0;
601
602 /* PRO CHATT */
603 function chatt_sub(name,str)
604 {
605   // alert("ARRIVA NAME: "+ name + "  STR:"+str);
606   if (chatt_lines_n == 20) {
607     $("txt").innerHTML = "";
608     for (i = 0 ; i < 19 ; i++) {
609       chatt_lines[i] = chatt_lines[i+1];
610       $("txt").innerHTML += chatt_lines[i];
611     }
612     chatt_lines[i] = "<b>"+name+"</b> "+str+ "<br>";
613     $("txt").innerHTML += chatt_lines[i];
614   }
615   else {
616     chatt_lines[chatt_lines_n] = "<b>"+name+"</b> "+str+ "<br>";
617     $("txt").innerHTML += chatt_lines[chatt_lines_n];
618     chatt_lines_n++;
619   }
620   $("txt").innerHTML;
621   $("txt").scrollTop = 10000000;
622 }
623
624 /*
625  *  GESTIONE DEI COOKIES
626  */
627 function createCookie(name,value,hours,path) {
628         if (hours) {
629                 var date = new Date();
630                 date.setTime(date.getTime()+(hours*60*60*1000));
631                 var expires = "; expires="+date.toGMTString();
632         }
633         else var expires = "";
634         document.cookie = name+"="+value+expires+"; path="+path;
635 }
636
637 function readCookie(name) {
638         var nameEQ = name + "=";
639         var ca = document.cookie.split(';');
640         for(var i=0;i < ca.length;i++) {
641                 var c = ca[i];
642                 while (c.charAt(0)==' ') c = c.substring(1,c.length);
643                 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
644         }
645         return null;
646 }
647
648 function eraseCookie(name) {
649         createCookie(name,"",-1);
650 }
651
652
653 /*
654 window.onload = function() {
655     $("log").innerHTML += "            xxxxxxxxxxxxxxxxxxxxxONLOAD<br>";
656
657     // $("imm2").style.left = 600;
658     // $("imm2").style.top  = 400;
659     var zigu = new slowimg($("imm"),300,100,15,"fin");
660     zigu.settime(1000);
661     zigu.start();
662     //     setTimeout(function() { alert("FIN:" + fin); }, 5000);
663 }
664 */