4 * Copyright (C) 2006 matteo.nastasi@milug.org
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.
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.
24 var EXIT_BAN_TIME = 900;
26 function $(id) { return document.getElementById(id); }
28 function getStyle(x,IEstyleProp, MozStyleProp)
31 var y = x.currentStyle[IEstyleProp];
32 } else if (window.getComputedStyle) {
33 var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(MozStyleProp);
38 /* replacement of setInterval on IE */
40 /*if not IE, do nothing*/
41 if(!document.uniqueID){return;};
43 /*Copy the default setInterval behavior*/
44 var nativeSetInterval = window.setInterval;
45 window.setInterval = function(fn,ms) {
47 if(arguments.length <= 2) {
48 return nativeSetInterval(fn,ms);
51 for(var i=2;i<arguments.length;i+=1) {
52 param[i-2] = arguments[i];
56 if(typeof(fn)=='function') {
58 return (function (fn,ms,param) {
59 var fo = function () {
60 fn.apply(window,param);
62 return nativeSetInterval(fo,ms);
65 else if(typeof(fn)=='string')
67 return nativeSetInterval(fn,ms);
71 throw Error('setInterval Error\nInvalid function type');
75 /*Copy the default setTimeout behavior*/
76 var nativeSetTimeout = window.setTimeout;
77 window.setTimeout = function(fn,ms) {
79 if(arguments.length <= 2) {
80 return nativeSetTimeout(fn,ms);
83 for(var i=2;i<arguments.length;i+=1) {
84 param[i-2] = arguments[i];
88 if(typeof(fn)=='function') {
90 return (function (fn,ms,param) {
91 var fo = function () {
92 fn.apply(window,param);
94 return nativeSetTimeout(fo,ms);
97 else if(typeof(fn)=='string')
99 return nativeSetTimeout(fn,ms);
103 throw Error('setTimeout Error\nInvalid function type');
109 // var card_pos = RANGE 0 <= x < cards_ea_n
111 function rnd_int(min, max) {
112 return Math.floor(Math.random() * (max - min + 1) + min);
115 function error_images()
120 function abort_images()
122 alert("ABORTAIMAGES");
125 function unload_images()
127 alert("ABORTAIMAGES");
130 function reset_images()
132 alert("ABORTAIMAGES");
135 function update_images()
137 $("imgct").innerHTML = "Immagini caricate "+g_preload_imgsz_arr[g_imgct]+"%.";
138 if (g_imgct < g_preload_img_arr.length)
139 setTimeout(preload_images, 100, g_preload_img_arr, g_imgct);
141 // $("imgct").innerHTML += "U";
144 function preload_images(arr,idx)
148 // $("imgct").innerHTML = "Stiamo caricando "+arr[idx]+"%.<br>";
149 im.onload = update_images;
150 im.onerror = error_images;
151 im.onabort = abort_images;
152 im.onunload = unload_images;
153 im.onreset = reset_images;
155 // $("imgct").innerHTML += "P";
158 function safestatus(a)
166 function createXMLHttpRequest() {
167 try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {}
168 try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
169 try { return new XMLHttpRequest(); } catch(e) {}
170 alert("XMLHttpRequest not supported");
174 function send_mesg(mesg)
176 var xhr_wr = createXMLHttpRequest();
177 // xhr_wr.open('GET', 'index_wr.php?sess='+sess+'&mesg='+encodeURIComponent(mesg), true);
178 xhr_wr.open('GET', 'index_wr.php?sess='+sess+'&mesg='+mesg, true);
179 xhr_wr.onreadystatechange = function() { return; };
184 /* Stat: CHAT and TABLE */
186 function chatt_checksend(obj,e)
192 if(window.event) { // IE
195 else if(e.which) { // Netscape/Firefox/Opera
198 // alert("OBJ: "+obj);
199 if (keynum == 13 && obj.value != "") { // Enter
200 act_chatt(obj.value);
204 function act_chatt(value)
206 send_mesg("chatt|"+encodeURIComponent(value));
210 obj.disabled = false;
217 function act_sitdown(table)
219 send_mesg("sitdown|"+table);
222 function act_wakeup()
227 /* Stat: TABLE Subst: ASTA */
228 function act_asta(card,pnt)
230 send_mesg("asta|"+card+"|"+pnt);
233 function act_choose(card)
236 send_mesg("choose|"+card);
239 /* Stat: TABLE Subst: GAME */
240 function act_play(card,x,y)
243 send_mesg("play|"+card+"|"+x+"|"+y);
246 function act_tableinfo()
248 send_mesg("tableinfo");
261 function act_exitlock()
263 send_mesg("exitlock");
266 function safelogout()
271 res = window.confirm("Sei sicuro di volere abbandonare la partita?\nATTENZIONE: se esci adesso senza il consenso degli altri giocatori non potrai sederti ai tavoli per "+(Math.floor(EXIT_BAN_TIME/60))+" minuti.");
273 res = window.confirm("Sei sicuro di volere abbandonare la partita?");
275 act_logout(g_exitlock);
278 function act_lascio()
283 function safelascio()
287 res = window.confirm("Sei sicuro di volere lasciare questa mano?");
292 function act_logout(exitlock)
294 send_mesg("logout|"+exitlock);
297 function act_reload()
299 window.onunload = null;
300 document.location.reload();
303 function act_shutdown()
307 send_mesg("shutdown");
308 while (xhr_wr.readyState != 4)
312 function act_preout()
317 function postact_logout()
319 // alert("postact_logout");
324 // eraseCookie("sess");
325 document.location.assign("index.php");
329 function slowimg(img,x1,y1,deltat,free,action,srcend)
331 x1,y1 - destination coords
332 deltat - time for each frame (in msec)
333 free - when the release the local block for other operations (range: 0 - 1)
334 action - function to run when the image is moved
335 srcend - image to switch when the image is moved
338 function sleep(st, delay)
340 // alert("LOC_NEW PRE: "+st.st_loc_new);
344 setTimeout(function(obj){ if (obj.st_loc_new > obj.st_loc) { obj.st_loc++; }},
348 function slowimg(img,x1,y1,deltat,free,action,srcend) {
351 // this.x0 = parseInt(document.defaultView.getComputedStyle(this.img, "").getPropertyValue("left"));
352 this.x0 = parseInt(getStyle(this.img,"left", "left"));
353 // alert("img.x0 = "+this.x0);
354 // this.y0 = parseInt(document.defaultView.getComputedStyle(this.img, "").getPropertyValue("top"));
355 this.y0 = parseInt(getStyle(this.img,"top", "top"));
358 this.deltat = deltat;
360 this.action = action;
361 this.srcend = srcend;
364 slowimg.prototype = {
383 setstart: function(x0,y0)
389 setaction: function(act)
395 settime: function(time)
397 this.time = (time < this.deltat ? this.deltat : time);
398 this.step_n = parseInt(this.time / this.deltat);
399 this.dx = (this.x1 - this.x0) / this.step_n;
400 this.dy = (this.y1 - this.y0) / this.step_n;
401 if (this.step_n * this.deltat == this.time) {
404 this.step_free = parseInt(this.step_n * this.free);
409 // $("logz").innerHTML += " xxxxxxxxxxxxxxxxxxxxxSTART<br>";
411 this.st.st_loc_new++;
413 this.img.style.visibility = "visible";
414 setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
419 // $("log").innerHTML = "Val " + this.step_cur + " N: " + this.step_n + "<br>";
420 if (this.step_cur == 0) {
421 var date = new Date();
422 // $("logz").innerHTML = "Timestart: " + date + "<br>";
424 if (this.step_cur <= this.step_n) {
425 this.img.style.left = this.x0 + this.dx * this.step_cur;
426 this.img.style.top = this.y0 + this.dy * this.step_cur;
428 setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
429 if (this.step_cur == this.step_free && this.st != null) {
430 if (this.st != null && this.st.st_loc < this.st.st_loc_new) {
431 // alert("QUI1 " + this.step_cur + " ZZ "+ this.step_free);
438 this.img.style.left = this.x1;
439 this.img.style.top = this.y1;
440 // $("logz").innerHTML += "xxxxxxxxxxxxxxxCLEAR<br>";
441 var date = new Date();
442 // $("logz").innerHTML += "Timestop: " + date + "<br>";
443 if (this.st != null && this.st.st_loc < this.st.st_loc_new) {
448 if (this.action != null) {
451 if (this.srcend != null) {
452 this.img.src = this.srcend;
458 var asta_xarr = new Array(0,66,132);
460 /* TODO: impostare gli onclick */
461 function dispose_asta(idx, pnt, nopoint)
466 for (i = 0 ; i < 10 ; i++) {
469 btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+".png";
473 btn.src = "img/asta"+i+(pnt >= 0 ? "" : "_ro")+".png";
477 btn.style.left = asta_xarr[i % 3];
479 btn.style.left = asta_xarr[(i+1) % 3];
481 btn.style.top = parseInt(i / 3) * 50+1;
484 eval("btn.onclick = function () { act_asta("+pass+",61); }");
485 btn.style.hover_cursor = "pointer";
489 btn.style.hover_cursor = "";
494 btn = $("astaptdiv");
495 btn.style.left = asta_xarr[i % 3];
496 btn.style.top = parseInt(i / 3) * 50 - 2;
497 // btn.style.visibility = "visible";
500 var rpnt = (pnt < 0 ? -pnt : pnt);
501 btn.value = (rpnt < 61 ? 61 : (rpnt > 120 ? 120 : rpnt));
503 btn = $("astaptsub");
504 btn.style.left = asta_xarr[i % 3];
505 btn.style.top = 25 + parseInt(i / 3) * 50 - 1;
506 btn.src = "img/astaptsub"+(pnt >= 0 ? "" : "_ro")+".png";
507 // btn.style.visibility = "visible";
509 btn.onclick = function () { act_asta(9,$("astapt").value); };
510 // btn.stylehover.cursor = "pointer";
514 // btn.stylehover.cursor = "";
519 btn = $("astapasso");
520 btn.style.left = asta_xarr[i % 3];
521 btn.style.top = parseInt(i / 3) * 50;
522 btn.src = "img/astapashalf"+(pnt >= 0 ? "" : "_ro")+".png";
524 btn.onclick = function () { act_asta(-1,0); };
525 // btn.stylehover.cursor = "pointer";
529 // btn.stylehover.cursor = "";
532 btn = $("astalascio");
533 btn.style.left = asta_xarr[i % 3];
534 btn.style.top = parseInt(i / 3) * 50 + 24;
535 btn.src = "img/astalascio.png";
536 btn.style.visibility = "visible";
537 btn.onclick = function () { safelascio(); };
540 btn = $("astapasso");
541 btn.style.left = asta_xarr[i % 3];
542 btn.style.top = parseInt(i / 3) * 50;;
543 btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+".png";
545 btn.onclick = function () { act_asta(-1,0); };
546 // btn.stylehover.cursor = "pointer";
550 // btn.stylehover.cursor = "";
553 btn = $("astalascio");
554 btn.style.visibility = "hidden";
557 // btn.style.visibility = "visible";
558 $("asta").style.visibility = "visible";
563 $("asta").style.visibility = "hidden";
567 function notify(st, text, tout, butt, w, h)
574 this.ancestor = document.body;
576 this.st.st_loc_new++;
578 clo = document.createElement("input");
580 clo.className = "button";
581 clo.style.bottom = "4px";
584 clo.onclick = this.input_hide;
586 clodiv = document.createElement("div");
587 clodiv.className = "notify_clo";
588 clodiv.appendChild(clo);
590 box = document.createElement("div");
591 box.className = "notify";
592 box.innerHTML = text;
593 box.style.zIndex = 200;
594 box.style.width = w+"px";
595 box.style.marginLeft = -parseInt(w/2)+"px";
596 box.style.height = h+"px";
597 box.appendChild(clodiv);
598 box.style.visibility = "visible";
602 this.ancestor.appendChild(box);
604 this.toutid = setTimeout(function(obj){ obj.unblock(); }, tout, this);
615 if (this.st.st_loc < this.st.st_loc_new) {
622 clearTimeout(this.toutid);
623 this.ancestor.removeChild(this.notitag);
627 input_hide: function()
629 clearTimeout(this.obj.toutid);
630 this.obj.ancestor.removeChild(this.obj.notitag);
637 return document.getElementById(id);
643 this.st_loc_new = -1;
644 this.comms = new Array;
649 function remark_step()
651 var ct = $("remark").l_remct;
657 $("remark").className = "remark"+ct;
658 $("remark").l_remct = ct;
659 setTimeout(remark_step,500);
662 $("remark").className = "remark0";
669 if ($("remark").l_remct == 0) {
670 $("remark").l_remct = 1;
671 setTimeout(remark_step,500);
675 function remark_off()
677 $("remark").l_remct = 0;
678 $("remark").className = "remark0";
682 function choose_seed(card)
686 $("chooseed").style.visibility = "visible";
687 for (i = 0 ; i < 4 ; i++) {
688 $("seed"+i).src = "img/"+i+""+card+".png";
690 eval("seed.onclick = function () { act_choose("+i+""+card+"); };");
694 function set_names(so,ea,ne,nw,we)
697 $("name").innerHTML = so;
698 $("name").title = so;
699 $("name_ea").innerHTML = ea;
700 $("name_ea").title = ea;
701 $("name_ne").innerHTML = ne;
702 $("name_ne").title = ne;
703 $("name_nw").innerHTML = nw;
704 $("name_nw").title = nw;
705 $("name_we").innerHTML = we;
706 $("name_we").title = we;
711 var astat_suffix = new Array("","_ea","_ne","_nw","_we");
713 function show_astat(zer,uno,due,tre,qua)
715 var astat = new Array(zer,uno,due,tre,qua);
717 for (i = 0 ; i < PLAYERS_N ; i++) {
718 idx = (PLAYERS_N + i - table_pos) % PLAYERS_N;
720 if (astat[i] == -2) {
721 $("public"+astat_suffix[idx]).style.visibility = "hidden";
723 else if (astat[i] == -1) {
724 $("public"+astat_suffix[idx]).style.visibility = "visible";
725 $("pubacard"+astat_suffix[idx]).src = "img/astapasso.png";
726 $("pubapnt"+astat_suffix[idx]).innerHTML = "";
727 $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
729 else if (astat[i] <= 10) {
730 $("public"+astat_suffix[idx]).style.visibility = "visible";
731 $("pubacard"+astat_suffix[idx]).src = "img/asta"+astat[i]+".png";
732 $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
734 else if (astat[i] <= 120) {
735 $("public"+astat_suffix[idx]).style.visibility = "visible";
736 $("pubacard"+astat_suffix[idx]).src = "img/asta9.png";
737 $("pubapnt"+astat_suffix[idx]).style.visibility = "inherit"; // XXX VISIBLE
738 $("pubapnt"+astat_suffix[idx]).innerHTML = astat[i];
743 function exitlock_show(num, islock)
747 num = (num < 3 ? num : 3);
748 $("exitlock").src = "img/exitlock"+num+(islock ? "n" : "y")+".png";
749 // alert("EXITLOCK: "+$("exitlock").src);
750 $("exitlock").style.visibility = "visible";
755 // exitlock_show(0, true);
757 function table_init() {
758 var sux = new Array("", "_ea", "_ne", "_nw", "_we");
761 $("asta").style.visibility = "hidden";
762 $("caller").style.visibility = "hidden";
763 show_astat(-2,-2,-2,-2,-2);
764 for (i=0 ; i < 8 ; i++) {
765 Drag.init($("card" + i), card_mouseup_cb);
766 for (e = 0 ; e < PLAYERS_N ; e++)
767 $("card"+sux[e]+i).style.visibility = "hidden";
769 for (i=0 ; i < PLAYERS_N ; i++) {
770 $("takes"+sux[i]).style.visibility = "hidden";
773 for (i = 0 ; i < 8 ; i++) {
785 var chatt_lines = new Array();
786 var chatt_lines_n = 0;
788 var CHATT_MAXLINES = 40;
791 function chatt_sub(name,str)
793 // alert("ARRIVA NAME: "+ name + " STR:"+str);
794 if (chatt_lines_n == CHATT_MAXLINES) {
795 $("txt").innerHTML = "";
796 for (i = 0 ; i < (CHATT_MAXLINES - 1) ; i++) {
797 chatt_lines[i] = chatt_lines[i+1];
798 $("txt").innerHTML += chatt_lines[i];
800 chatt_lines[i] = "<b>"+name+"</b> "+str+ "<br>";
801 $("txt").innerHTML += chatt_lines[i];
804 chatt_lines[chatt_lines_n] = "<b>"+name+"</b> "+str+ "<br>";
805 $("txt").innerHTML += chatt_lines[chatt_lines_n];
809 $("txt").scrollTop = 10000000;
813 * GESTIONE DEI COOKIES
815 function createCookie(name,value,hours,path) {
817 var date = new Date();
818 date.setTime(date.getTime()+(hours*60*60*1000));
819 var expires = "; expires="+date.toGMTString();
821 else var expires = "";
822 document.cookie = name+"="+value+expires+"; path="+path;
825 function readCookie(name) {
826 var nameEQ = name + "=";
827 var ca = document.cookie.split(';');
828 for(var i=0;i < ca.length;i++) {
830 while (c.charAt(0)==' ') c = c.substring(1,c.length);
831 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
836 function eraseCookie(name) {
837 createCookie(name,"",-1);
840 var onunload_times = 0;
843 function onunload_cb () {
845 if (onunload_times == 0) {
846 var res = window.confirm(" Vuoi veramente abbandonare la briscola ?\n(clicca annulla o cancel se vuoi ricaricare la briscola)");
855 location = self.location;
857 alert("Ripristino della briscola fallito, per non perdere la sessione ricaricare la pagina manualmente.");
867 function room_checkspace(emme,tables,inpe)
870 for (i = 0 ; i < emme ; i++)
875 for (i = 0 ; i < 5 ; i++)
878 for (i = 0 ; i < tables ; i++) {
879 $("table"+i).innerHTML = alta;
880 $("table_act"+i).innerHTML = "<input type=\"button\" class=\"button\" name=\"xhenter"+i+"\" value=\"Mi siedo.\" onclick=\"act_sitdown(1);\">";
883 stand = "<table class=\"table_standup\"><tbody><tr>";
884 for (i = 0 ; i < inpe ; i++) {
885 stand += "<td class=\"td_standup\">"+nome+"</td>";
886 if ((i+1) % 4 == 0) {
887 stand += "</tr><tr>";
891 $("standup").innerHTML = stand;
893 $("esco").innerHTML = "<input name=\"logout\" type=\"button\" value=\"Esco.\" onclick=\"window.onunload = null; act_logout();\" type=\"button\">";
896 function playsound(tag, sound) {
897 // g_withflash is a global var
899 $(tag).innerHTML = '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '+
900 'codebase="http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0" id="mysound" WIDTH=1 HEIGHT=1>' +
901 '<PARAM NAME="movie" VALUE="playsound.swf"><PARAM NAME="PLAY" VALUE="true"><PARAM NAME="LOOP" VALUE="false">' +
902 '<PARAM NAME=FlashVars VALUE="streamUrl='+sound+'">' +
903 '<EMBED swliveconnect="true" name="mysound" src="playsound.swf" FlashVars="streamUrl='+sound+'" PLAY="true" LOOP="false" '+
904 ' WIDTH=1 HEIGHT=1 TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></OBJECT>';