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