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