allignment pre merge in master
[brisk.git] / web / briskin5 / briskin5.js
1 /*
2  *  brisk - briskin5.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 background_set()
27 {
28     $("bg").style.backgroundImage = 'url("img/brisk_table_sand'+table_pos+'.jpg")'; 
29 }
30
31 /* Stat: TABLE  Subst: ASTA */
32 function act_asta(card,pnt)
33 {
34     send_mesg("asta|"+card+"|"+pnt);
35 }
36
37 var asta_xarr = new Array(0,66,132);
38
39 /* TODO: impostare gli onclick */
40 function dispose_asta(idx, pnt, nopoint)
41 {
42     var i, btn, pass;
43     var btn;
44     var lng = langtolng(g_lang);
45
46     for (i = 0 ; i < 10 ; i++) {
47         btn = $("asta"+i);
48         if (i < idx) {
49             btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+lng+".png";
50             btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
51             pass = -1;
52         }
53         else {
54             btn.src = "img/asta"+i+(pnt >= 0 ? "" : "_ro")+lng+".png";
55             btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
56             pass = i;
57         }
58         if (i < 19)
59             btn.style.left = asta_xarr[i % 3];
60         else
61             btn.style.left = asta_xarr[(i+1) % 3];
62         
63         btn.style.top  = parseInt(i / 3) * 50 + (i == 9 ? 0 : 1);
64
65         if (pnt >= 0) {
66             eval("btn.onclick = function () { act_asta("+pass+",61); }");
67             btn.style.cursor = "pointer";
68         }
69         else {
70             btn.onclick = null;
71             btn.style.cursor = "default";
72         }
73     }
74     
75     
76     btn = $("astaptdiv");
77     btn.style.left = asta_xarr[i % 3];
78     btn.style.top = parseInt(i / 3) * 50 - 2;
79     // btn.style.visibility  = "visible";
80     
81     btn = $("astapt");
82     var rpnt = (pnt < 0 ? -pnt : pnt);
83     btn.value = (rpnt < 61 ? 61 : (rpnt > 120 ? 120 : rpnt));
84     
85     btn = $("astaptsub");
86     btn.style.left = asta_xarr[i % 3];
87     btn.style.top = 25 + parseInt(i / 3) * 50 - 1;
88     btn.src = "img/astaptsub"+(pnt >= 0 ? "" : "_ro")+lng+".png";
89     btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
90     if (pnt >= 0) {
91         btn.onclick = function () { act_asta(9,$("astapt").value); };
92         btn.style.cursor = "pointer";
93     }
94     else {
95         btn.onclick = null;
96         btn.style.cursor = "default";
97     }
98     
99     i+=1;
100     if (nopoint) {
101         btn = $("astapasso");
102         btn.style.left = asta_xarr[i % 3];
103         btn.style.top = parseInt(i / 3) * 50;
104         btn.src = "img/astapashalf"+(pnt >= 0 ? "" : "_ro")+lng+".png";
105         btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
106         if (pnt >= 0) {
107             btn.onclick = function () { act_asta(-1,0); };
108         }
109         else {          
110             btn.onclick = null;
111         }
112
113         btn = $("astalascio");
114         btn.style.left = asta_xarr[i % 3];
115         btn.style.top = parseInt(i / 3) * 50 + 24;
116         btn.src = "img/astalascio"+lng+".png";
117         btn.style.visibility = "visible";
118         btn.onclick = function () { safelascio(); };
119         }
120     else {
121         btn = $("astapasso");
122         btn.style.left = asta_xarr[i % 3];
123         btn.style.top = parseInt(i / 3) * 50;;
124         btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+lng+".png";
125         btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
126         if (pnt >= 0) {
127             btn.onclick = function () { act_asta(-1,0); };
128         }
129         else {
130             btn.onclick = null;
131         }
132
133         btn = $("astalascio");
134         btn.style.visibility = "hidden";
135         btn.onclick = null;
136     }
137     // btn.style.visibility  = "visible";
138     $("asta").style.visibility = "visible";
139 }
140
141 function asta_pnt_set(pnt)
142 {
143     btn = $("astapt");
144     var rpnt = (pnt < 0 ? -pnt : pnt);
145     btn.value = (rpnt < 61 ? 61 : (rpnt > 120 ? 120 : rpnt));
146 }
147
148 function hide_asta()
149 {
150     $("asta").style.visibility = "hidden"; 
151 }
152
153 function choose_seed(card)
154 {
155     var i;
156
157     $("asta").style.visibility = "hidden"; 
158     $("astalascio").style.visibility = "hidden"; 
159     $("chooseed").style.visibility = "visible";
160     for (i = 0 ; i < 4 ; i++) {
161         $("seed"+i).src = "img/"+i+""+card+".png";
162         seed=$("seed"+i);
163         eval("seed.onclick = function () { act_choose("+i+""+card+"); };");
164     }
165 }
166
167 var astat_suffix = new Array("","_ea","_ne","_nw","_we");
168
169 function show_astat(zer,uno,due,tre,qua)
170 {
171     var astat = new Array(zer,uno,due,tre,qua);
172     var lng = langtolng(g_lang);
173
174     for (i = 0 ; i < PLAYERS_N ; i++) {
175         idx = (PLAYERS_N + i - table_pos) % PLAYERS_N;
176
177         if (astat[i] == -2) {
178             $("public"+astat_suffix[idx]).style.visibility = "hidden";
179         }
180         else if (astat[i] == -1) {
181             $("public"+astat_suffix[idx]).style.visibility = "visible";
182             $("pubacard"+astat_suffix[idx]).src = "img/astapasso"+lng+".png";
183             $("pubapnt"+astat_suffix[idx]).innerHTML = "";
184             $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
185         }
186         else if (astat[i] <= 10) {
187             $("public"+astat_suffix[idx]).style.visibility = "visible";
188             $("pubacard"+astat_suffix[idx]).src = "img/asta"+astat[i]+lng+".png";
189             $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
190         }
191         else if (astat[i] <= 120) {
192             $("public"+astat_suffix[idx]).style.visibility = "visible";
193             $("pubacard"+astat_suffix[idx]).src = "img/asta9"+lng+".png";
194             $("pubapnt"+astat_suffix[idx]).style.visibility = "inherit"; // XXX VISIBLE
195             $("pubapnt"+astat_suffix[idx]).innerHTML = astat[i];
196         }
197     }
198 }
199
200
201 function table_init() {
202     var sux = new Array("", "_ea", "_ne", "_nw", "_we");
203
204     // console.log("table_init");
205
206     remark_off();
207     $("asta").style.visibility = "hidden";
208     $("caller").style.visibility = "hidden";
209     show_astat(-2,-2,-2,-2,-2);
210     set_iscalling(-1);
211
212     for (i=0 ; i < 8 ; i++) {
213         Drag.init($("card" + i), card_mouseup_cb);
214         for (e = 0 ; e < PLAYERS_N ; e++)
215             $("card"+sux[e]+i).style.visibility = "hidden";
216     }
217     for (i=0 ; i < PLAYERS_N ; i++) {
218         // console.log("shut: "+"takes"+sux[i]);
219         $("takes"+sux[i]).style.visibility = "hidden";
220         }
221
222     for (i = 0 ; i < 8 ; i++) {
223         cards_pos[i] = i;
224         cards_ea_pos[i] = i;
225         cards_ne_pos[i] = i;
226         cards_nw_pos[i] = i;
227         cards_we_pos[i] = i;
228     }
229
230 }
231   
232 function act_choose(card)
233 {
234     send_mesg("choose|"+card);
235 }
236
237 function act_play(card,x,y)
238 {
239     send_mesg("play|"+card+"|"+x+"|"+y);
240 }
241
242 function act_tableinfo()
243 {
244     send_mesg("tableinfo");
245 }
246
247 function act_exitlock()
248 {
249     send_mesg("exitlock");
250 }
251
252 function safelogout()
253 {
254     var res;
255     
256     if (g_exitlock < 2) 
257         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.");    
258     else 
259         res = window.confirm("Sei sicuro di volere abbandonare la partita?");
260     if (res)
261         act_logout(g_exitlock);
262 }
263
264 function act_reload()
265 {
266     window.onunload = null;
267     window.onbeforeunload = null;
268     document.location.reload();
269 }
270
271 function set_names(so,ea,ne,nw,we)
272 {
273     // alert("SET NAME");
274     $("name").innerHTML = italizer(so);
275     $("name").title = unescapeHTML(so[1]); 
276     $("name_ea").innerHTML = italizer(ea);
277     $("name_ea").title = unescapeHTML(ea[1]);
278     $("name_ne").innerHTML = italizer(ne);
279     $("name_ne").title = unescapeHTML(ne[1]);
280     $("name_nw").innerHTML = italizer(nw);
281     $("name_nw").title = unescapeHTML(nw[1]);
282     $("name_we").innerHTML = italizer(we);
283     $("name_we").title = unescapeHTML(we[1]);
284
285     for (i = 0 ; i < PLAYERS_N ; i++) 
286         $("name"+astat_suffix[i]).title_orig = $("name"+astat_suffix[i]).title;
287
288     return;
289 }
290
291 function set_iscalling(idx)
292 {
293     var i;
294
295     for (i = 0 ; i < PLAYERS_N ; i++) {
296         $("name"+astat_suffix[i]).className = "pubinfo"+astat_suffix[i]+(i == idx ? "_iscalling" : "");
297         $("name"+astat_suffix[i]).title = $("name"+astat_suffix[i]).title_orig + (i == idx ? " sta chiamando" : "");
298     }
299 }
300
301 function preferences_init()
302 {
303     var rd;
304
305     if ((rd = readCookie("CO_bin5_pref_ring_endauct")) != null) {
306         $('pref_ring_endauct').checked = (rd == "true" ? true : false);
307     }
308     else {
309         $('pref_ring_endauct').checked = false;
310     }
311     $('preferences').ring_endauct = $('pref_ring_endauct').checked;
312 }
313
314 function preferences_update()
315 {
316     var ret;
317     createCookie("CO_bin5_pref_ring_endauct", ($('preferences').ring_endauct ? "true" : "false"), 24*3650, cookiepath); 
318     ret = server_request('mesg', 'preferences_update');
319 }
320
321 function act_preferences_update()
322 {
323     preferences_update()
324     preferences_showhide();
325 }
326
327 function pref_ring_endauct_set(obj)
328 {
329     $('preferences').ring_endauct = obj.checked;
330 }
331
332
333 function preferences_show()
334 {
335     var no;
336
337     div_show($('preferences'));
338 }
339
340 function preferences_showhide()
341 {
342     if ($('preferences').style.visibility == 'hidden') {
343         preferences_init();
344         
345         $('preferences').style.top = parseInt((document.body.clientHeight - 
346                                                parseInt(getStyle($('preferences'), "height","height"))
347                                                ) / 2) + document.body.scrollTop;
348         $('preferences').style.visibility = 'visible';
349     }
350     else
351         $('preferences').style.visibility = 'hidden';
352 }