proper language management for deck reload (and close button)
[brisk.git] / web / briskin5 / briskin5.js
1 /*
2  *  brisk - briskin5.js
3  *
4  *  Copyright (C) 2006-2012 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  */
23
24 var mlang_briskin5 = { 'is_calling' : { 'it' : ' sta chiamando', 
25                                         'en' : ' is calling' },
26                        'deckreload' : { 'it' : '<br/>Per rendere visibile il nuovo mazzo di carte<br/><br/>occorre fare reload della pagina.',
27                                         'en' : '<br/>EN: Per rendere visibile il nuovo mazzo di carte<br/><br/>occorre fare reload della pagina.'},
28                        'close_btn':   { 'it' : 'Chiudi',
29                                         'en' : 'Close'}
30                      }
31
32 function Preferences(ring_endauct, deck, deck_old)
33 {
34     this.ring_endauct = ring_endauct;
35     this.deck = deck;
36     this.deck_old = deck_old;
37 }
38
39 Preferences.prototype = {
40     ring_endauct: true,
41     deck: null,
42     deck_old: null
43 }
44
45 function background_set()
46 {
47     $("bg").style.backgroundImage = 'url("img/brisk_table_sand'+table_pos+'.jpg")'; 
48 }
49
50 /* Stat: TABLE  Subst: ASTA */
51 function act_asta(card,pnt)
52 {
53     send_mesg("asta|"+card+"|"+pnt);
54 }
55
56 var asta_xarr = new Array(0,66,132);
57
58 /* TODO: impostare gli onclick */
59 function dispose_asta(idx, pnt, nopoint)
60 {
61     var i, btn, pass;
62     var btn;
63     var lng = langtolng(g_lang);
64
65     for (i = 0 ; i < 10 ; i++) {
66         btn = $("asta"+i);
67         if (i < idx) {
68             btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+lng+".png";
69             btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
70             pass = -1;
71         }
72         else {
73             btn.src = "img/asta"+i+(pnt >= 0 ? "" : "_ro")+lng+".png";
74             btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
75             pass = i;
76         }
77         if (i < 19)
78             btn.style.left = asta_xarr[i % 3];
79         else
80             btn.style.left = asta_xarr[(i+1) % 3];
81         
82         btn.style.top  = parseInt(i / 3) * 50 + (i == 9 ? 0 : 1);
83
84         if (pnt >= 0) {
85             eval("btn.onclick = function () { act_asta("+pass+",61); }");
86             btn.style.cursor = "pointer";
87         }
88         else {
89             btn.onclick = null;
90             btn.style.cursor = "default";
91         }
92     }
93     
94     
95     btn = $("astaptdiv");
96     btn.style.left = asta_xarr[i % 3];
97     btn.style.top = parseInt(i / 3) * 50 - 2;
98     // btn.style.visibility  = "visible";
99     
100     btn = $("astapt");
101     var rpnt = (pnt < 0 ? -pnt : pnt);
102     btn.value = (rpnt < 61 ? 61 : (rpnt > 120 ? 120 : rpnt));
103     
104     btn = $("astaptsub");
105     btn.style.left = asta_xarr[i % 3];
106     btn.style.top = 25 + parseInt(i / 3) * 50 - 1;
107     btn.src = "img/astaptsub"+(pnt >= 0 ? "" : "_ro")+lng+".png";
108     btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
109     if (pnt >= 0) {
110         btn.onclick = function () { act_asta(9,$("astapt").value); };
111         btn.style.cursor = "pointer";
112     }
113     else {
114         btn.onclick = null;
115         btn.style.cursor = "default";
116     }
117     
118     i+=1;
119     if (nopoint) {
120         btn = $("astapasso");
121         btn.style.left = asta_xarr[i % 3];
122         btn.style.top = parseInt(i / 3) * 50;
123         btn.src = "img/astapashalf"+(pnt >= 0 ? "" : "_ro")+lng+".png";
124         btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
125         if (pnt >= 0) {
126             btn.onclick = function () { act_asta(-1,0); };
127         }
128         else {          
129             btn.onclick = null;
130         }
131
132         btn = $("astalascio");
133         btn.style.left = asta_xarr[i % 3];
134         btn.style.top = parseInt(i / 3) * 50 + 24;
135         btn.src = "img/astalascio"+lng+".png";
136         btn.style.visibility = "";
137         btn.onclick = function () { safelascio(); };
138         }
139     else {
140         btn = $("astapasso");
141         btn.style.left = asta_xarr[i % 3];
142         btn.style.top = parseInt(i / 3) * 50;;
143         btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+lng+".png";
144         btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
145         if (pnt >= 0) {
146             btn.onclick = function () { act_asta(-1,0); };
147         }
148         else {
149             btn.onclick = null;
150         }
151
152         btn = $("astalascio");
153         btn.style.visibility = "hidden";
154         btn.onclick = null;
155     }
156     // btn.style.visibility  = "visible";
157     $("asta").style.visibility = "visible";
158 }
159
160 function asta_pnt_set(pnt)
161 {
162     btn = $("astapt");
163     var rpnt = (pnt < 0 ? -pnt : pnt);
164     btn.value = (rpnt < 61 ? 61 : (rpnt > 120 ? 120 : rpnt));
165 }
166
167 function hide_asta()
168 {
169     $("asta").style.visibility = "hidden"; 
170 }
171
172 function choose_seed(card)
173 {
174     var i;
175
176     $("asta").style.visibility = "hidden"; 
177     $("astalascio").style.visibility = "hidden"; 
178     $("chooseed").style.visibility = "visible";
179     for (i = 0 ; i < 4 ; i++) {
180         $("seed"+i).setAttribute('data-card-id', '' + i + card);
181         seed=$("seed"+i);
182         eval("seed.onclick = function () { act_choose("+i+""+card+"); };");
183     }
184 }
185
186 var astat_suffix = new Array("","_ea","_ne","_nw","_we");
187
188 function show_astat(zer,uno,due,tre,qua)
189 {
190     var astat = new Array(zer,uno,due,tre,qua);
191     var lng = langtolng(g_lang);
192
193     for (i = 0 ; i < PLAYERS_N ; i++) {
194         idx = (PLAYERS_N + i - table_pos) % PLAYERS_N;
195
196         if (astat[i] == -2) {
197             $("public"+astat_suffix[idx]).style.visibility = "hidden";
198         }
199         else if (astat[i] == -1) {
200             $("public"+astat_suffix[idx]).style.visibility = "visible";
201             $("pubacard"+astat_suffix[idx]).src = "img/astapasso"+lng+".png";
202             $("pubapnt"+astat_suffix[idx]).innerHTML = "";
203             $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
204         }
205         else if (astat[i] <= 10) {
206             $("public"+astat_suffix[idx]).style.visibility = "visible";
207             $("pubacard"+astat_suffix[idx]).src = "img/asta"+astat[i]+lng+".png";
208             $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
209         }
210         else if (astat[i] <= 120) {
211             $("public"+astat_suffix[idx]).style.visibility = "visible";
212             $("pubacard"+astat_suffix[idx]).src = "img/asta9"+lng+".png";
213             $("pubapnt"+astat_suffix[idx]).style.visibility = "inherit"; // XXX VISIBLE
214             $("pubapnt"+astat_suffix[idx]).innerHTML = astat[i];
215         }
216     }
217 }
218
219
220 function table_init() {
221     var sux = new Array("", "_ea", "_ne", "_nw", "_we");
222
223     // console.log("table_init");
224
225     remark_off();
226     $("asta").style.visibility = "hidden";
227     $("caller").style.visibility = "hidden";
228     show_astat(-2,-2,-2,-2,-2);
229     set_iscalling(-1);
230
231     for (i=0 ; i < CARD_HAND ; i++) {
232         Drag.init($("card" + i), card_mouseup_cb);
233         for (e = 0 ; e < PLAYERS_N ; e++)
234             $("card"+sux[e]+i).style.visibility = "hidden";
235     }
236     for (i=0 ; i < PLAYERS_N ; i++) {
237         // console.log("shut: "+"takes"+sux[i]);
238         $("takes"+sux[i]).style.visibility = "hidden";
239         }
240
241     for (i=0 ; i < CARD_HAND ; i++) {
242         cards_pos[i] = i;
243         cards_ea_pos[i] = i;
244         cards_ne_pos[i] = i;
245         cards_nw_pos[i] = i;
246         cards_we_pos[i] = i;
247     }
248
249 }
250   
251 function act_choose(card)
252 {
253     send_mesg("choose|"+card);
254 }
255
256 function act_play(card,x,y)
257 {
258     send_mesg("play|"+card+"|"+x+"|"+y);
259 }
260
261 function act_tableinfo()
262 {
263     send_mesg("tableinfo");
264 }
265
266 function act_exitlock()
267 {
268     send_mesg("exitlock");
269 }
270
271 function safelogout()
272 {
273     var res;
274     
275     if (g_exitlock < 2) 
276         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.");    
277     else 
278         res = window.confirm("Sei sicuro di volere abbandonare la partita?");
279     if (res)
280         act_logout(g_exitlock);
281 }
282
283 function act_reload()
284 {
285     window.onunload = null;
286     window.onbeforeunload = null;
287     // alert(document.location.toString());
288     document.location.assign("index.php");
289     // document.location.reload();
290 }
291
292 function set_names(so,ea,ne,nw,we)
293 {
294     // alert("SET NAME");
295     $("name").innerHTML    = user_decorator(so, false);
296     $("name").title    = unescapeHTML(so[1]); 
297     $("name_ea").innerHTML = user_decorator(ea, false);
298     $("name_ea").title = unescapeHTML(ea[1]);
299     $("name_ne").innerHTML = user_decorator(ne, false);
300     $("name_ne").title = unescapeHTML(ne[1]);
301     $("name_nw").innerHTML = user_decorator(nw, false);
302     $("name_nw").title = unescapeHTML(nw[1]);
303     $("name_we").innerHTML = user_decorator(we, false);
304     $("name_we").title = unescapeHTML(we[1]);
305
306     for (i = 0 ; i < PLAYERS_N ; i++) 
307         $("name"+astat_suffix[i]).title_orig = $("name"+astat_suffix[i]).title;
308
309     return;
310 }
311
312 function set_iscalling(idx)
313 {
314     var i;
315
316     for (i = 0 ; i < PLAYERS_N ; i++) {
317         $("name"+astat_suffix[i]).className = "pubinfo"+astat_suffix[i]+(i == idx ? "_iscalling" : "");
318         $("name"+astat_suffix[i]).title = $("name"+astat_suffix[i]).title_orig + (i == idx ? mlang_briskin5['is_calling'][g_lang] : "");
319     }
320 }
321
322 var preferences = new Preferences(true, 'xx', 'xx');
323 // FIXME move it in the html dynamic generation scope
324 var deck_list = { 'xx': 'Normal cards',
325                   'yy': 'Slim cards' }
326 function preferences_init()
327 {
328     var rd;
329
330     if ((rd = readCookie("CO_bin5_pref_ring_endauct")) != null) {
331         preferences.ring_endauct = $('pref_ring_endauct').checked = (rd == "true" ? true : false);
332     }
333     else {
334         preferences.ring_endauct = $('pref_ring_endauct').checked = true;
335     }
336
337     if ((rd = readCookie("CO_bin5_pref_deck")) != null && rd in deck_list) {
338         $$('#pref_deck').val(rd);
339         preferences.deck = rd;
340         preferences.deck_old = rd;
341     }
342     else {
343         rd = 'xx';
344         $$('#pref_deck').val(rd);
345         preferences.deck = rd;
346         preferences.deck_old = rd;
347     }
348 }
349
350 function preferences_update()
351 {
352     var ret;
353     createCookie("CO_bin5_pref_ring_endauct", (preferences.ring_endauct ? "true" : "false"), 24*3650, cookiepath);
354     createCookie("CO_bin5_pref_deck", preferences.deck, 24*3650, cookiepath);
355     ret = server_request('mesg', 'preferences_update');
356 }
357
358 function act_preferences_update()
359 {
360     preferences_update();
361     if (preferences.deck != preferences.deck_old) {
362         // FIXME: with dynamic text
363         x = new notify(gst, mlang_briskin5['deckreload'],
364                        0, mlang_briskin5['close_btn'][g_lang], 400, 110);
365     }
366     preferences_showhide();
367 }
368
369 function pref_ring_endauct_set(obj)
370 {
371     preferences.ring_endauct = obj.checked;
372 }
373
374 function pref_deck_set(obj)
375 {
376     preferences.deck = $$(obj).val();
377 }
378
379
380 function preferences_show()
381 {
382     var no;
383
384     div_show($('preferences'));
385 }
386
387 function preferences_showhide()
388 {
389     if ($('preferences').style.visibility == 'hidden') {
390         preferences_init();
391         
392         $('preferences').style.top = parseInt((document.body.clientHeight - 
393                                                parseInt(getStyle($('preferences'), "height","height"))
394                                                ) / 2) + document.body.scrollTop;
395         $('preferences').style.visibility = 'visible';
396     }
397     else
398         $('preferences').style.visibility = 'hidden';
399 }
400
401 function act_select_rules(rule_id)
402 {
403     send_mesg("chatt|/rules " + rule_id);
404 }
405
406 function act_select_deck(deck_id)
407 {
408     preferences.deck = deck_id;
409 }
410
411 function rules_set(id)
412 {
413     $('select_rules').value = id;
414 }