Assorted infobox bugfixing.
[pr0n] / files / pr0n-fullscreen.js
1 var req;
2
3 function init_ajax()
4 {
5         req = false;
6
7         if (window.XMLHttpRequest) {
8                 // Mozilla/Safari
9                 try {
10                         req = new XMLHttpRequest();
11                 } catch(e) {
12                         req = false;
13                 }
14         } else if (window.ActiveXObject) {
15                 // IE/Windows
16                 try {
17                         req = new ActiveXObject("Msxml2.XMLHTTP");
18                 } catch(e) {
19                         try {
20                                 req = new ActiveXObject("Microsoft.XMLHTTP");
21                         } catch(e) {
22                                 req = false;
23                         }
24                 }
25         }
26 }
27
28 function find_width()
29 {
30         if (typeof(window.innerWidth) == 'number') {
31                 // non-IE
32                 return [window.innerWidth, window.innerHeight];
33         } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
34                 // IE 6+ in 'standards compliant mode'
35                 return [document.documentElement.clientWidth, document.documentElement.clientHeight];
36         } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
37                 // IE 4-compatible
38                 return [document.body.clientWidth, document.body.clientHeight];
39         }
40         return [null,null];
41 }
42
43 function parse_image_num(url, default_value) {
44         var url_array = (window.location.toString().split("#"));
45         if (url_array.length != 2) {
46                 return default_value;
47         }
48
49         var num = parseInt(url_array[1]);
50         if (num > 1 && num <= global_image_list.length) {  // and then num != NaN
51                 return (num - 1);
52         } else {
53                 return default_value;
54         }
55 }
56
57 /*
58  * pr0n can resize to any size we'd like, but we're much more likely
59  * to have this set of fixed-resolution screens cached, so to increase
60  * performance, we round down to the closest fit and use that. This 
61  * function is a pessimal estimate of what thumbnail size we can _always_
62  * fit on the screen -- it's right if and only if all images are 4:3
63  * (and landscape). If individual size information is available, use
64  * pick_image_size, below.
65  */
66 var fixed_sizes = [
67         [ 1600, 1200 ],
68         [ 1400, 1050 ],
69         [ 1280, 960 ],
70         [ 1152, 864 ],
71         [ 1024, 768 ],
72         [ 800, 600 ],
73         [ 640, 480 ],
74         [ 512, 384 ],
75         [ 320, 256 ],
76         [ 240, 192 ],
77         [ 120, 96 ],
78         [ 80, 64 ]
79 ];
80 function max_image_size(screen_size)
81 {
82         var i;
83         for (i = 0; i < fixed_sizes.length; ++i) {
84                 if (screen_size[0] >= fixed_sizes[i][0] && screen_size[1] >= fixed_sizes[i][1]) {
85                         return fixed_sizes[i];
86                 }
87         }
88         return [ 80, 64 ];
89 }
90
91 function pick_image_size(screen_size, image_size)
92 {
93         var i;
94         for (i = 0; i < fixed_sizes.length; ++i) {
95                 // this is a duplicate of pr0n's resizing code, hope for no floating-point
96                 // inaccuracies :-)
97                 var thumbxres = fixed_sizes[i][0];
98                 var thumbyres = fixed_sizes[i][1];
99                 var width = image_size[0];
100                 var height = image_size[1];
101
102                 if (!(thumbxres >= width && thumbyres >= height)) {
103                         var sfh = width / thumbxres;
104                         var sfv = height / thumbyres;
105                         if (sfh > sfv) {
106                                 width  /= sfh;
107                                 height /= sfh;
108                         } else {
109                                 width  /= sfv;
110                                 height /= sfv;
111                         }
112                         width = Math.floor(width);
113                         height = Math.floor(height);
114                 }
115
116                 if (screen_size[0] >= width && screen_size[1] >= height) {
117                         // be sure _not_ to return a reference
118                         return [ fixed_sizes[i][0], fixed_sizes[i][1], width, height ];
119                 }
120         }
121         return [ 80, 64 ];
122 }
123
124 function replace_image_element(url, element_id, parent_node)
125 {
126         var img = document.getElementById(element_id);
127         if (img !== null) {
128                 img.src = "data:";
129                 img.parentNode.removeChild(img);
130         }
131
132         img = document.createElement("img");
133         img.id = element_id;
134         img.alt = "";
135
136         if (img.src != url) {
137                 img.src = url;
138         }
139         
140         parent_node.appendChild(img);
141         return img;
142 }
143
144 function display_image(width, height, evt, filename, element_id)
145 {
146         var url = "http://" + global_vhost + "/" + evt + "/" + width + "x" + height + "/nobox/" + filename;
147         var main = document.getElementById("iehack");
148         var img = replace_image_element(url, element_id, main);
149
150         if (global_infobox != 'nobox') {
151                 var url = "http://" + global_vhost + "/" + evt + "/" + width + "x" + height + "/box/" + filename;
152                 var boximg = replace_image_element(url, element_id + "_box", main);
153
154                 boximg.style.position = "absolute";
155                 boximg.style.left = "0px";
156                 boximg.style.bottom = "-1px";
157         }
158
159         return img;
160 }
161
162 function display_image_num(num, element_id)
163 {
164         var screen_size = find_width();
165         var adjusted_size;
166
167         if (global_image_list[num][2] == -1) {
168                 // no size information, use our pessimal guess
169                 adjusted_size = max_image_size(screen_size);
170         } else {
171                 adjusted_size = pick_image_size(screen_size, [ global_image_list[num][2], global_image_list[num][3] ]);
172         }
173
174         var img = display_image(adjusted_size[0], adjusted_size[1], global_image_list[num][0], global_image_list[num][1], element_id);
175         
176         if (element_id == "image") {
177                 // we want to shrink the box as much as possible if we know the true
178                 // size of the image
179                 center_image(num);
180                 
181                 // replace the anchor part (if any) with the image number
182                 var baseurl = (window.location.toString().split("#"))[0];
183                 window.location = baseurl + "#" + (num+1);
184         }
185
186         return img;
187 }
188
189 function prepare_preload(img, num)
190 {
191         // cancel any pending preload
192         var preload = document.getElementById("preload");
193         if (preload !== null) {
194                 preload.src = "data:";
195                 preload.parentNode.removeChild(preload);
196         }
197         
198         var preload_box = document.getElementById("preload_box");
199         if (preload_box !== null) {
200                 preload_box.src = "data:";
201                 preload_box.parentNode.removeChild(preload_box);
202         }
203
204         // grmf -- IE doesn't fire onload if the image was loaded from cache, so check for
205         // completeness first; should at least be _somewhat_ better
206         if (img.complete) {
207                 display_image_num(num, "preload");
208         } else {
209                 img.onload = function() { display_image_num(num, "preload"); };
210         }       
211 }
212
213 function can_go_next()
214 {
215         return (global_image_num < global_image_list.length - 1);
216 }
217
218 function can_go_previous()
219 {
220         return (global_image_num > 0);
221 }
222
223 function set_opacity(id, amount)
224 {
225         var elem = document.getElementById(id);
226         if (typeof(elem.style.opacity) != 'undefined') {            // W3C
227                 elem.style.opacity = amount;
228         } else if (typeof(elem.style.mozOpacity) != 'undefined') {  // older Mozilla
229                 elem.style.mozOpacity = amount;
230         } else if (typeof(elem.style.filter) != 'undefined') {      // IE
231                 if (elem.style.filter.indexOf("alpha") == -1) {
232                         // add an alpha filter if there isn't one already
233                         if (elem.style.filter) {
234                                 elem.style.filter += " ";
235                         } else {
236                                 elem.style.filter = "";
237                         }
238                         elem.style.filter += "alpha(opacity=" + (amount*100.0) + ")";
239                 } else {        
240                         // ugh? this seems to break in color index mode...
241                         if (typeof(elem.filters) == 'unknown') {
242                                 elem.style.filter = "alpha(opacity=" + (amount*100.0) + ")";
243                         } else {
244                                 elem.filters.alpha.opacity = (amount * 100.0);
245                         }
246                 }
247         } else {                             // no alpha support
248                 if (amount > 0.5) {
249                         elem.style.visibility = "visible";
250                         elem.style.zorder = 1;
251                 } else {
252                         elem.style.visibility = "hidden";
253                 }
254         }
255 }
256
257 function center_image(num)
258 {
259         var screen_size = find_width();
260         var width, height;
261         
262         if (global_image_list[num][2] == -1) {
263                 // no size information, use our pessimal guess
264                 var adjusted_size = max_image_size(screen_size);
265                 width = adjusted_size[0];
266                 height = adjusted_size[1];
267         } else {
268                 // use the exact information
269                 var adjusted_size = pick_image_size(screen_size, [ global_image_list[num][2], global_image_list[num][3] ]);
270                 width = adjusted_size[2];
271                 height = adjusted_size[3];
272         }
273
274         // center the image on-screen
275         var main = document.getElementById("main");
276         main.style.position = "absolute";
277         main.style.left = (screen_size[0] - width) / 2 + "px";
278         main.style.top = (screen_size[1] - height) / 2 + "px"; 
279         main.style.width = width + "px";
280         main.style.height = height + "px";
281         main.style.lineHeight = height + "px";
282 }
283
284 function relayout()
285 {
286         var img = display_image_num(global_image_num, "image");
287         if (can_go_next()) {
288                 prepare_preload(img, global_image_num + 1);
289         }
290
291         set_opacity("previous", can_go_previous() ? 0.7 : 0.1);
292         set_opacity("next", can_go_next() ? 0.7 : 0.1);
293         set_opacity("close", 0.7);
294 }
295
296 function go_previous()
297 {
298         if (!can_go_previous()) {
299                 return;
300         }
301
302         var img = display_image_num(--global_image_num, "image");
303         if (can_go_previous()) {
304                 set_opacity("previous", 0.7);
305                 prepare_preload(img, global_image_num - 1);
306         } else {
307                 set_opacity("previous", 0.1);
308         }
309         set_opacity("next", can_go_next() ? 0.7 : 0.1);
310 }
311
312 function go_next()
313 {
314         if (!can_go_next()) {
315                 return;
316         }
317
318         var img = display_image_num(++global_image_num, "image");
319         if (can_go_next()) {
320                 set_opacity("next", 0.7);
321                 prepare_preload(img, global_image_num + 1);
322         } else {
323                 set_opacity("next", 0.1);
324         }
325         set_opacity("previous", can_go_previous() ? 0.7 : 0.1);
326 }
327
328 function do_close()
329 {
330         window.location = global_return_url;
331 }
332
333 function draw_text(msg)
334 {
335         // remove any text we might have left
336         var text = document.getElementById("text");
337         if (text !== null) {
338                 text.parentNode.removeChild(text);
339         }
340
341         text = document.createElement("p");
342         text.id = "text";
343         text.style.position = "absolute";
344         text.style.color = "white";
345         text.style.lineHeight = "24px";
346         text.style.font = "24px verdana, arial, sans-serif";
347         text.innerHTML = msg;
348
349         var main = document.getElementById("main");
350         main.appendChild(text);
351
352         text.style.left = (main.clientWidth - text.clientWidth) / 2 + "px";
353         text.style.top = (main.clientHeight - text.clientHeight) / 2 + "px";
354 }
355
356 function fade_text(opacity)
357 {
358         set_opacity("text", opacity);
359         if (opacity > 0.0) {
360                 opacity -= 0.03;
361                 if (opacity < 0.0) {
362                         opacity = 0.0;
363                 }
364                 setTimeout("fade_text(" + opacity + ")", 30);
365         } else {
366                 var text = document.getElementById("text");
367                 if (text !== null) {
368                         text.parentNode.removeChild(text);
369                 }
370         }
371 }
372
373 function select_image(evt, filename)
374 {
375         if (!req) {
376                 return;
377         }
378
379         draw_text("Selecting " + filename + "...");
380         
381         req.open("POST", "http://" + global_vhost + "/select", false);
382         req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
383         req.send("mode=single&event=" + evt + "&filename=" + filename);
384
385         setTimeout("fade_text(0.99)", 30);
386 }
387
388 function key_down(which)
389 {
390         if (which == 39) {   // right
391                 if (can_go_next()) {
392                         set_opacity("next", 0.99);
393                 }
394         } else if (which == 37) {   // left
395                 if (can_go_previous()) {
396                         set_opacity("previous", 0.99);
397                 }
398         } else if (which == 27) {   // escape
399                 set_opacity("close", 0.99);
400         }
401 }
402
403 function key_up(which) {
404         if (which == 39) {   // right
405                 if (can_go_next()) {
406                         set_opacity("next", 0.7);
407                         go_next();
408                 }
409         } else if (which == 37) {   // left
410                 if (can_go_previous()) {
411                         set_opacity("previous", 0.7);
412                         go_previous();
413                 }
414         } else if (which == 27) {   // escape
415                 set_opacity("close", 0.7);
416                 do_close();
417         } else if (which == 32 && global_select) {   // space
418                 select_image(global_image_list[global_image_num][0], global_image_list[global_image_num][1]);
419         }
420 }
421
422 // enable the horrible horrible IE PNG hack
423 function ie_png_hack()
424 {
425         var vstr = navigator.appVersion.split("MSIE");
426         var v = parseFloat(vstr[1]);
427         if (v >= 5.5 && v < 7.0 && document.body.filters) {
428                 var next = document.getElementById("next");
429                 next.outerHTML = "<span id=\"next\" style=\"display: inline-block; position: absolute; bottom: 0px; right: 0px; width: 50px; height: 50px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + next.src + "')\" onmousedown=\"if (can_go_next()) set_opacity('next', 1.0)\" onmouseup=\"if (can_go_next()) { set_opacity('next', 0.7); go_next(); }\" onmouseout=\"if (can_go_next()) { set_opacity('next', 0.7); }\" />";
430                 
431                 var previous = document.getElementById("previous");
432                 previous.outerHTML = "<span id=\"previous\" style=\"display: inline-block; position: absolute; bottom: 0px; right: 0px; width: 50px; height: 50px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + previous.src + "')\" onmousedown=\"if (can_go_previous()) set_opacity('previous', 1.0)\" onmouseup=\"if (can_go_previous()) { set_opacity('previous', 0.7); go_previous(); }\" onmouseout=\"if (can_go_previous()) { set_opacity('previous', 0.7); }\" />";
433                 
434                 var close = document.getElementById("close");
435                 close.outerHTML = "<span id=\"close\" style=\"display: inline-block; position: absolute; top: 0px; right: 0px; width: 50px; height: 50px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + close.src + "')\" onmousedown=\"set_opacity('close', 1.0)\" onmouseup=\"set_opacity('close', 0.7); do_close();\" onmouseout=\"set_opacity('close', 0.7);\" />";
436         }
437 }