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