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