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