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