d1101718d6b0bc68b58f972bd949ab115c14ab05
[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         if (global_infobox != 'nobox/') {
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
188         return img;
189 }
190
191 function display_image_num(num, element_id)
192 {
193         var screen_size = find_width();
194         var adjusted_size;
195
196         if (global_image_list[num][2] == -1) {
197                 // no size information, use our pessimal guess
198                 adjusted_size = max_image_size(screen_size);
199         } else {
200                 adjusted_size = pick_image_size(screen_size, [ global_image_list[num][2], global_image_list[num][3] ]);
201         }
202
203         var img = display_image(adjusted_size[0], adjusted_size[1], global_image_list[num][0], global_image_list[num][1], element_id);
204         
205         if (element_id == "image") {
206                 // we want to shrink the box as much as possible if we know the true
207                 // size of the image
208                 center_image(num);
209                 
210                 // replace the anchor part (if any) with the image number
211                 window.location.hash = "#" + (num+1);
212         }
213
214         return img;
215 }
216
217 function prepare_preload(img, num)
218 {
219         // cancel any pending preload
220         var preload = document.getElementById("preload");
221         if (preload !== null) {
222                 preload.parentNode.removeChild(preload);
223         }
224         
225         var preload_box = document.getElementById("preload_box");
226         if (preload_box !== null) {
227                 preload_box.parentNode.removeChild(preload_box);
228         }
229
230         // grmf -- IE doesn't fire onload if the image was loaded from cache, so check for
231         // completeness first; should at least be _somewhat_ better
232         if (img.complete) {
233                 display_image_num(num, "preload");
234         } else {
235                 img.onload = function() { display_image_num(num, "preload"); };
236         }       
237 }
238
239 function can_go_next()
240 {
241         return (global_image_num < global_image_list.length - 1);
242 }
243
244 function can_go_previous()
245 {
246         return (global_image_num > 0);
247 }
248
249 function set_opacity(id, amount)
250 {
251         var elem = document.getElementById(id);
252         if (typeof(elem.style.opacity) != 'undefined') {            // W3C
253                 elem.style.opacity = amount;
254         } else if (typeof(elem.style.mozOpacity) != 'undefined') {  // older Mozilla
255                 elem.style.mozOpacity = amount;
256         } else if (typeof(elem.style.filter) != 'undefined') {      // IE
257                 if (elem.style.filter.indexOf("alpha") == -1) {
258                         // add an alpha filter if there isn't one already
259                         if (elem.style.filter) {
260                                 elem.style.filter += " ";
261                         } else {
262                                 elem.style.filter = "";
263                         }
264                         elem.style.filter += "alpha(opacity=" + (amount*100.0) + ")";
265                 } else {        
266                         // ugh? this seems to break in color index mode...
267                         if (typeof(elem.filters) == 'unknown') {
268                                 elem.style.filter = "alpha(opacity=" + (amount*100.0) + ")";
269                         } else {
270                                 elem.filters.alpha.opacity = (amount * 100.0);
271                         }
272                 }
273         } else {                             // no alpha support
274                 if (amount > 0.5) {
275                         elem.style.visibility = "visible";
276                         elem.style.zorder = 1;
277                 } else {
278                         elem.style.visibility = "hidden";
279                 }
280         }
281 }
282
283 function center_image(num)
284 {
285         var screen_size = find_width();
286         var dpr = find_dpr();
287         var width, height;
288         
289         if (global_image_list[num][2] == -1) {
290                 // no size information, use our pessimal guess
291                 var adjusted_size = max_image_size(screen_size);
292                 width = adjusted_size[0];
293                 height = adjusted_size[1];
294         } else {
295                 // use the exact information
296                 var adjusted_size = pick_image_size(screen_size, [ global_image_list[num][2], global_image_list[num][3] ]);
297                 width = adjusted_size[2];
298                 height = adjusted_size[3];
299         }
300
301         // center the image on-screen
302         var main = document.getElementById("main");
303         main.style.position = "absolute";
304         main.style.left = (((screen_size[0] - width) / 2) / dpr) + "px";
305         main.style.top = (((screen_size[1] - height) / 2) / dpr) + "px";
306         main.style.width = (width / dpr) + "px";
307         main.style.height = (height / dpr) + "px";
308         main.style.lineHeight = (height / dpr) + "px";
309 }
310
311 function relayout()
312 {
313         var img = display_image_num(global_image_num, "image");
314         if (can_go_next()) {
315                 prepare_preload(img, global_image_num + 1);
316         }
317
318         set_opacity("previous", can_go_previous() ? 0.7 : 0.1);
319         set_opacity("next", can_go_next() ? 0.7 : 0.1);
320         set_opacity("close", 0.7);
321 }
322
323 function go_previous()
324 {
325         if (!can_go_previous()) {
326                 return;
327         }
328
329         var img = display_image_num(--global_image_num, "image");
330         if (can_go_previous()) {
331                 set_opacity("previous", 0.7);
332                 prepare_preload(img, global_image_num - 1);
333         } else {
334                 set_opacity("previous", 0.1);
335         }
336         set_opacity("next", can_go_next() ? 0.7 : 0.1);
337 }
338
339 function go_next()
340 {
341         if (!can_go_next()) {
342                 return;
343         }
344
345         var img = display_image_num(++global_image_num, "image");
346         if (can_go_next()) {
347                 set_opacity("next", 0.7);
348                 prepare_preload(img, global_image_num + 1);
349         } else {
350                 set_opacity("next", 0.1);
351         }
352         set_opacity("previous", can_go_previous() ? 0.7 : 0.1);
353 }
354
355 function do_close()
356 {
357         window.location = global_return_url;
358 }
359
360 function draw_text(msg)
361 {
362         // remove any text we might have left
363         var text = document.getElementById("text");
364         if (text !== null) {
365                 text.parentNode.removeChild(text);
366         }
367
368         text = document.createElement("p");
369         text.id = "text";
370         text.style.position = "absolute";
371         text.style.color = "white";
372         text.style.lineHeight = "24px";
373         text.style.font = "24px verdana, arial, sans-serif";
374         text.innerHTML = msg;
375
376         var main = document.getElementById("main");
377         main.appendChild(text);
378
379         text.style.left = (main.clientWidth - text.clientWidth) / 2 + "px";
380         text.style.top = (main.clientHeight - text.clientHeight) / 2 + "px";
381 }
382
383 function fade_text(opacity)
384 {
385         set_opacity("text", opacity);
386         if (opacity > 0.0) {
387                 opacity -= 0.03;
388                 if (opacity < 0.0) {
389                         opacity = 0.0;
390                 }
391                 setTimeout("fade_text(" + opacity + ")", 30);
392         } else {
393                 var text = document.getElementById("text");
394                 if (text !== null) {
395                         text.parentNode.removeChild(text);
396                 }
397         }
398 }
399
400 function select_image(evt, filename, selected)
401 {
402         if (!req) {
403                 return;
404         }
405
406         if (selected) {
407                 draw_text("Selecting " + filename + "...");
408         } else {
409                 draw_text("Unselecting " + filename + "...");
410         }
411         
412         req.open("POST", window.location.origin + "/select", false);
413         req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
414         req.send("event=" + evt + "&filename=" + filename + "&selected=" + selected);
415
416         setTimeout("fade_text(0.99)", 30);
417 }
418
419 function key_down(which)
420 {
421         if (which == 39) {   // right
422                 if (can_go_next()) {
423                         set_opacity("next", 0.99);
424                 }
425         } else if (which == 37) {   // left
426                 if (can_go_previous()) {
427                         set_opacity("previous", 0.99);
428                 }
429         } else if (which == 27) {   // escape
430                 set_opacity("close", 0.99);
431         } else {
432                 check_for_hash_change();
433         }
434 }
435
436 function key_up(which) {
437         if (which == 39) {   // right
438                 if (can_go_next()) {
439                         set_opacity("next", 0.7);
440                         go_next();
441                 }
442         } else if (which == 37) {   // left
443                 if (can_go_previous()) {
444                         set_opacity("previous", 0.7);
445                         go_previous();
446                 }
447         } else if (which == 27) {   // escape
448                 set_opacity("close", 0.7);
449                 do_close();
450         } else if (which == 32 && global_select) {   // space
451                 select_image(global_image_list[global_image_num][0], global_image_list[global_image_num][1], 1);
452         } else if (which == 85 && global_select) {   // u
453                 select_image(global_image_list[global_image_num][0], global_image_list[global_image_num][1], 0);
454         } else {
455                 check_for_hash_change();
456         }
457 }
458
459 // enable the horrible horrible IE PNG hack
460 function ie_png_hack()
461 {
462         var vstr = navigator.appVersion.split("MSIE");
463         var v = parseFloat(vstr[1]);
464         if (v >= 5.5 && v < 7.0 && document.body.filters) {
465                 var next = document.getElementById("next");
466                 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); }\" />";
467                 
468                 var previous = document.getElementById("previous");
469                 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); }\" />";
470                 
471                 var close = document.getElementById("close");
472                 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);\" />";
473         }
474 }
475
476 function parse_image_num(default_value) {
477         var num = parseInt(window.location.hash.substr(1));
478         if (num >= 1 && num <= global_image_list.length) {  // and then num != NaN
479                 return (num - 1);
480         } else {
481                 return default_value;
482         }
483 }
484
485 function check_for_hash_change() {
486         var num = parse_image_num(-1);
487         if (num != -1 && num != global_image_num) {
488                 global_image_num = num;
489                 relayout();
490         }
491 }