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