fix cookie
[booh] / lib / booh / html-merges.rb
1 #
2 #                         *  BOOH  *
3 #
4 # A.k.a `Best web-album Of the world, Or your money back, Humerus'.
5 #
6 # The acronyn sucks, however this is a tribute to Dragon Ball by
7 # Akira Toriyama, where the last enemy beaten by heroes of Dragon
8 # Ball is named "Boo". But there was already a free software project
9 # called Boo, so this one will be it "Booh". Or whatever.
10 #
11 #
12 # Copyright (c) 2004 Guillaume Cottenceau <gc3 at bluewin.ch>
13 #
14 # This software may be freely redistributed under the terms of the GNU
15 # public license version 2.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 # holds static data to merge in the html "themes"
22
23 require 'gettext'
24 bindtextdomain("booh")
25
26 require 'booh/booh-lib'
27 include Booh
28
29 $image_head_code = <<'EOF'
30 <meta name="generator" content="Generated by Booh! http://zarb.org/~gc/html/booh.html">
31
32 <script language="JavaScript1.1" type="text/JavaScript">
33
34 var images = new Array(~~images~~);
35 ~~other_images~~
36 var other_sizes = new Array(~~other_sizes~~);
37 var captions = new Array(~~captions~~);
38
39 var images_ary = new Array();
40 var images_loaded = new Array();
41 var current = 0;
42 var slideshow = 0;
43 var slideshow_pause = 3;
44 var slideshow_timer = null;
45
46 for (i = 0; i < images.length; i++) { 
47     /* this array will contain 0 if image not yet loaded, 1 when loading,
48      * 2 when complete */
49     images_loaded[i] = 0;
50 }
51
52 function dbg(t) {
53     document.getElementById('dbg_text').firstChild.data += t + "\n";
54 }
55
56 /* load image, return 1 if image is finished loading */
57 function load(i) {
58     if (images_loaded[i] == 0) {
59         images_ary[i] = new Image();
60         images_ary[i].src = images[i];
61         images_loaded[i] = 1;
62     }
63     if (images_loaded[i] == 1) {
64         if (images_ary[i].complete) {
65             images_loaded[i] = 2;
66         } else {
67             return 0;
68         }
69     }
70     return 1;
71 }
72
73 function preload() { 
74
75     /* favor current image, if user clicked on `last' or something */
76     load(current);
77
78     /* don't blindly preload all images at the beginning,
79      * but rather load them one by one, in order to get
80      * next ones faster, beginning with next to current
81      */
82     for (i = current + 1; i < images.length && i <= current + 5; i++) { 
83         if (load(i) == 0) {
84             setTimeout("preload()", 500);
85             return;
86         }
87     }
88     for (i = current - 1; i >= current - 3; i--) { 
89         if (i >= 0) {
90             if (load(i) == 0) {
91                 setTimeout("preload()", 500);
92                 return;
93             }
94         }
95     }
96
97     setTimeout("preload()", 500);
98 }
99
100 function init() {
101
102     var expires = new Date(new Date().getTime() + (30 * 86400000));  // 30 days
103     document.cookie = 'booh-preferred-size-~~theme~~=~~current_size~~'
104                       + '; expires=' + expires.toGMTString()
105                       + '; path=/';
106
107     /* retrieve GET parameters */
108     all_params = location.href.split("?")
109     if (all_params.length > 1) {
110         params = all_params[1].split("&");
111         for (i = 0; i < params.length; i++) {
112             keyvalue = params[i].split("=");
113             if (keyvalue[0] == "slideshow_pause") {
114                 slideshow_pause = keyvalue[1];
115             }
116             if (keyvalue[0] == "run_slideshow") {
117                 toggle_slideshow();
118             }
119             if (keyvalue[0] == "current") {
120                 for (i = 0; i < images.length; i++) {
121                     if (images[i] == keyvalue[1]) {
122                         current = i;
123                         break;
124                     }
125                 }
126             }
127         }
128     }
129
130     preload();
131     display_current();
132     if (images.length == 1) {
133         document.getElementById("b_slideshow").disabled = true;
134     }
135 }
136
137 function update_sensibilities() {
138     if (current == 0) {
139         document.getElementById("b_first").disabled = true;
140         document.getElementById("b_previous").disabled = true;
141     } else {
142         document.getElementById("b_first").disabled = false;
143         document.getElementById("b_previous").disabled = false;
144     }
145
146     if (current == images.length - 1) {
147         document.getElementById("b_next").disabled = true;
148         document.getElementById("b_last").disabled = true;
149     } else {
150         document.getElementById("b_next").disabled = false;
151         document.getElementById("b_last").disabled = false;
152     }
153 }
154
155 function set_cursor_(value, element) {
156
157     if (!element || !element.style) {
158         return;
159     }
160
161     element.style.cursor = value;
162
163     children = element.childNodes;
164     for (i = 0; i < children.length; i++) {
165         set_cursor_(value, children.item[i]);
166     }
167 }
168
169 function set_cursor(value) {
170     set_cursor_(value, document.getElementById('body'));
171 }
172
173 function show_current_text() {
174     /* don't show text if image not yet loaded because navigator
175      * won't refresh it during load */
176     if (images_loaded[current] == 2) {
177         eval("document.getElementById('image_counter')" +
178                      ".firstChild.data = '" + ( current + 1 ) + "/" + images.length + "'");
179         eval("document.getElementById('main_text')" +
180                      ".firstChild.data = \"" + ( captions[current] || images[current] ) + "\"");
181         for (i = 0; i < other_sizes.length; i++) { 
182             eval("linkelems = document.getElementById('link" + other_sizes[i] + "').href.split('?');" +
183                  "document.getElementById('link" + other_sizes[i] + "').href = linkelems[0] + '?current=" + eval("images_" + other_sizes[i] + "[current]") + "'");
184         }
185         set_cursor("default");
186     } else {
187         setTimeout("show_current_text()", 100);
188         set_cursor("wait");
189     }
190 }
191
192 function display_current() {
193     eval("document.main_img.src = images[" + current + "]");
194     show_current_text();
195     update_sensibilities();
196 }
197
198 function first() {
199     if (slideshow == 1) {
200         toggle_slideshow(true);
201     }
202     
203     current = 0;
204     display_current();
205 }
206
207 function next() {
208     if (slideshow == 1) {
209         toggle_slideshow(true);
210     }
211
212     if (current < images.length - 1) {
213         current++;
214         display_current();
215     }
216 }
217
218 function previous() {
219     if (slideshow == 1) {
220         toggle_slideshow(true);
221     }
222
223     if (current > 0) {
224         current--;
225         display_current();
226     }
227 }
228
229 function last() {
230     if (slideshow == 1) {
231         toggle_slideshow(true);
232     }
233
234     current = images.length - 1;
235     display_current();
236 }
237
238 function toggle_slideshow(now) {
239     if (slideshow == 0) {
240         document.getElementById("b_slideshow").value = "~~stop_slideshow~~";
241         slideshow = 1;
242         if (current == images.length - 1) {
243             current = -1;
244         }
245         if (now) {
246             run_slideshow();
247         } else {
248             setTimeout("run_slideshow()", slideshow_pause * 1000);
249         }
250     } else {
251         clearTimeout(slideshow_timer);
252         document.getElementById("b_slideshow").value = "~~run_slideshow~~";
253         slideshow = 0;
254     }
255 }
256
257 function run_slideshow() {
258     if (slideshow == 0) {
259         return;
260     }
261
262     if (images_loaded[current + 1] == 2) {
263         current++;
264         display_current();
265         slideshow_timer = setTimeout("run_slideshow()", slideshow_pause * 1000);
266     } else {
267         slideshow_timer = setTimeout("run_slideshow()", 500);
268     }
269
270     if (current == images.length - 1) {
271         toggle_slideshow(true);
272     }
273 }
274 </script>
275 EOF
276
277 $image_head_code.sub!('~~run_slideshow~~', utf8(_('Run slideshow!')))
278 $image_head_code.sub!('~~stop_slideshow~~', utf8(_('Stop slideshow')))
279
280 $body_additions = <<'EOF'
281 onload="init()" id="body"
282 EOF
283
284 $button_first = '
285     <form><input type="button"
286                  onclick="first()"
287                  value="' + utf8(_('<<- First')) + '"
288                  id="b_first"></form>';
289
290 $button_previous = '
291     <form><input type="button"
292                  onclick="previous()"
293                  value="' + utf8(_('<- Previous')) + '"
294                  id="b_previous"></form>';
295
296 $button_next = '
297     <form><input type="button"
298                  onclick="next()"
299                  value="' + utf8(_('Next ->')) + '"
300                  id="b_next"></form>';
301
302 $button_last = '
303     <form><input type="button"
304                  onclick="last()"
305                  value="' + utf8(_('Last ->>')) + '"
306                  id="b_last"></form>';
307
308 $button_slideshow = '
309   <form><input type="button"
310                onclick="toggle_slideshow(true)"
311                value="' + utf8(_('Run slideshow!')) + '"
312                id="b_slideshow">
313   </form>';
314
315 $image = <<'EOF'
316   <img name="main_img">
317 EOF
318
319 $image_counter_additions = <<'EOF'
320   id="image_counter"
321 EOF
322
323 $caption_additions = <<'EOF'
324   id="main_text"
325 EOF
326
327 $body_code = <<'EOF'
328 EOF
329
330
331 $thumbnails_head_code = <<'EOF'
332 <script language="JavaScript1.1" type="text/JavaScript">
333     var expires = new Date(new Date().getTime() + (30 * 86400000));  // 30 days
334     document.cookie = 'booh-preferred-size-~~theme~~=~~current_size~~'
335                       + '; expires=' + expires.toGMTString()
336                       + '; path=/';
337 </script>
338 EOF
339
340
341 $preferred_size_reloader = <<'EOF'
342 <html>
343     <head>
344         <script language="JavaScript1.1" type="text/JavaScript">
345
346 function getPreferredSize() {
347     if (document.cookie) {
348         var index = document.cookie.indexOf('booh-preferred-size-~~theme~~');
349         if (index != -1) {
350             var oleft = (document.cookie.indexOf('=', index) + 1);
351             var oright = document.cookie.indexOf(';', index);
352             if (oright == -1) {
353                 oright = document.cookie.length;
354             }
355             return 'thumbnails-' + document.cookie.substring(oleft, oright) + '.html';
356         }
357     }
358     return 'thumbnails-~~default_size~~.html';
359 }
360
361 window.location.href = getPreferredSize();
362
363         </script>
364     </head>
365 </html>
366 EOF