Fix the fullscreen mode for +all. Probably fixes some infobox+select bugs while
[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.
47  */
48 function reduce_to_fixed_width(size)
49 {
50         var fixed_sizes = [
51                 [ 1280, 960 ],
52                 [ 1024, 768 ],
53                 [ 800, 600 ],
54                 [ 640, 480 ],
55                 [ 512, 384 ],
56                 [ 320, 256 ],
57                 [ 240, 192 ],
58                 [ 120, 96 ],
59                 [ 80, 64 ]
60         ];
61         for (i = 0; i < fixed_sizes.length; ++i) {
62                 if (size[0] >= fixed_sizes[i][0] && size[1] >= fixed_sizes[i][1])
63                         return fixed_sizes[i];
64         }
65         return [ 80, 64 ];
66 }
67         
68 function display_image(width, height, evt, filename, element_id)
69 {
70         var url = "http://" + global_vhost + "/" + evt + "/" + width + "x" + height + "/" + global_infobox + filename;
71         var img = document.getElementById(element_id);
72         if (img != null) {
73                 img.src = "data:";
74                 img.parentNode.removeChild(img);
75         }
76
77         img = document.createElement("img");
78         img.id = element_id;
79         img.alt = "";
80
81         if (img.src != url) {
82                 img.src = url;
83         }
84         
85         var main = document.getElementById("iehack");
86         main.appendChild(img);
87
88         return img;
89 }
90
91 function prepare_preload(img, width, height, evt, filename)
92 {
93         // cancel any pending preload
94         var preload = document.getElementById("preload");
95         if (preload != null) {
96                 preload.src = "data:";
97                 preload.parentNode.removeChild(preload);
98         }
99
100         // grmf -- IE doesn't fire onload if the image was loaded from cache, so check for
101         // completeness first; should at least be _somewhat_ better
102         if (img.complete) {
103                 display_image(width, height, evt, filename, "preload");
104         } else {
105                 img.onload = function() { display_image(width, height, evt, filename, "preload"); };
106         }       
107 }
108
109 function relayout()
110 {
111         var size = find_width();
112         var adjusted_size = reduce_to_fixed_width(size);
113
114         var img = display_image(adjusted_size[0], adjusted_size[1], global_image_list[global_image_num][0], global_image_list[global_image_num][1], "image");
115         if (can_go_next()) {
116                 prepare_preload(img, adjusted_size[0], adjusted_size[1], global_image_list[global_image_num + 1][0], global_image_list[global_image_num + 1]);
117         }
118         
119         // center the image on-screen
120         var main = document.getElementById("main");
121         main.style.position = "absolute";
122         main.style.left = (size[0] - adjusted_size[0]) / 2 + "px";
123         main.style.top = (size[1] - adjusted_size[1]) / 2 + "px"; 
124         main.style.width = adjusted_size[0] + "px";
125         main.style.height = adjusted_size[1] + "px";
126         main.style.lineHeight = adjusted_size[1] + "px"; 
127
128         set_opacity("previous", can_go_previous() ? 0.7 : 0.1);
129         set_opacity("next", can_go_next() ? 0.7 : 0.1);
130         set_opacity("close", 0.7);
131 }
132
133 function set_opacity(id, amount)
134 {
135         var elem = document.getElementById(id);
136         if (typeof(elem.style.opacity) != 'undefined') {            // W3C
137                 elem.style.opacity = amount;
138         } else if (typeof(elem.style.mozOpacity) != 'undefined') {  // older Mozilla
139                 elem.style.mozOpacity = amount;
140         } else if (typeof(elem.style.filter) != 'undefined') {      // IE
141                 if (elem.style.filter.indexOf("alpha") == -1) {
142                         // add an alpha filter if there isn't one already
143                         if (elem.style.filter) {
144                                 elem.style.filter += " ";
145                         } else {
146                                 elem.style.filter = "";
147                         }
148                         elem.style.filter += "alpha(opacity=" + (amount*100.0) + ")";
149                 } else {        
150                         // ugh? this seems to break in color index mode...
151                         if (typeof(elem.filters) == 'unknown') {
152                                 elem.style.filter = "alpha(opacity=" + (amount*100.0) + ")";
153                         } else {
154                                 elem.filters.alpha.opacity = (amount * 100.0);
155                         }
156                 }
157         } else {                             // no alpha support
158                 if (amount > 0.5) {
159                         elem.style.visibility = "visible";
160                         elem.style.zorder = 1;
161                 } else {
162                         elem.style.visibility = "hidden";
163                 }
164         }
165 }
166
167 function can_go_previous()
168 {
169         return (global_image_num > 0);
170 }
171
172 function go_previous()
173 {
174         if (!can_go_previous())
175                 return;
176
177         --global_image_num;
178
179         var adjusted_size = reduce_to_fixed_width(find_width());
180
181         var img = display_image(adjusted_size[0], adjusted_size[1], global_image_list[global_image_num][0], global_image_list[global_image_num][1], "image");
182         if (can_go_previous()) {
183                 set_opacity("previous", 0.7);
184                 prepare_preload(img, adjusted_size[0], adjusted_size[1], global_image_list[global_image_num - 1][0], global_image_list[global_image_num - 1][1]);
185         } else {
186                 set_opacity("previous", 0.1);
187         }
188         set_opacity("next", can_go_next() ? 0.7 : 0.1);
189 }
190
191 function can_go_next()
192 {
193         return (global_image_num < global_image_list.length - 1);
194 }
195
196 function go_next()
197 {
198         if (!can_go_next())
199                 return;
200
201         ++global_image_num;
202
203         var adjusted_size = reduce_to_fixed_width(find_width());
204
205         var img = display_image(adjusted_size[0], adjusted_size[1], global_image_list[global_image_num][0], global_image_list[global_image_num][1], "image");
206         if (can_go_next()) {
207                 set_opacity("next", 0.7);
208                 prepare_preload(img, adjusted_size[0], adjusted_size[1], global_image_list[global_image_num + 1][0], global_image_list[global_image_num + 1][1]);
209         } else {
210                 set_opacity("next", 0.1);
211         }
212         set_opacity("previous", can_go_previous() ? 0.7 : 0.1);
213 }
214
215 function key_down(which)
216 {
217         if (which == 39) {   // right
218                 if (can_go_next()) {
219                         set_opacity("next", 0.99);
220                 }
221         } else if (which == 37) {   // left
222                 if (can_go_previous()) {
223                         set_opacity("previous", 0.99);
224                 }
225         } else if (which == 27) {   // escape
226                 set_opacity("close", 0.99);
227         }
228 }
229
230 function key_up(which) {
231         if (which == 39) {   // right
232                 if (can_go_next()) {
233                         set_opacity("next", 0.7);
234                         go_next();
235                 }
236         } else if (which == 37) {   // left
237                 if (can_go_previous()) {
238                         set_opacity("previous", 0.7);
239                         go_previous();
240                 }
241         } else if (which == 27) {   // escape
242                 set_opacity("close", 0.7);
243                 do_close();
244         } else if (which == 32 && global_select) {   // space
245                 select_image(global_image_list[global_image_num][0], global_image_list[global_image_num][1]);
246         }
247 }
248
249 function select_image(evt, filename)
250 {
251         if (!req)
252                 return;
253
254         draw_text("Selecting " + filename + "...");
255         
256         req.open("POST", "http://" + global_vhost + "/select", false);
257         req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
258         req.send("mode=single&event=" + evt + "&filename=" + filename);
259
260         setTimeout("fade_text(0.99)", 30);
261 }
262
263 function fade_text(opacity)
264 {
265         set_opacity("text", opacity);
266         if (opacity > 0.0) {
267                 opacity -= 0.03;
268                 if (opacity < 0.0)
269                         opacity = 0.0;
270                 setTimeout("fade_text(" + opacity + ")", 30);
271         } else {
272                 var text = document.getElementById("text");
273                 if (text != null) {
274                         text.parentNode.removeChild(text);
275                 }
276         }
277 }
278
279 function do_close()
280 {
281         window.location = global_return_url;
282 }
283
284 function draw_text(msg)
285 {
286         // remove any text we might have left
287         var text = document.getElementById("text");
288         if (text != null) {
289                 text.parentNode.removeChild(text);
290         }
291
292         text = document.createElement("p");
293         text.id = "text";
294         text.style.position = "absolute";
295         text.style.color = "white";
296         text.style.lineHeight = "24px";
297         text.style.font = "24px verdana, arial, sans-serif";
298         text.innerHTML = msg;
299
300         var main = document.getElementById("main");
301         main.appendChild(text);
302
303         text.style.left = (main.clientWidth - text.clientWidth) / 2 + "px";
304         text.style.top = (main.clientHeight - text.clientHeight) / 2 + "px";
305 }
306
307 // enable the horrible horrible IE PNG hack
308 function ie_png_hack()
309 {
310         var vstr = navigator.appVersion.split("MSIE");
311         var v = parseFloat(vstr[1]);
312         if (v >= 5.5 && v < 7.0 && document.body.filters) {
313                 var next = document.getElementById("next");
314                 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); }\" />";
315                 
316                 var previous = document.getElementById("previous");
317                 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); }\" />";
318                 
319                 var close = document.getElementById("close");
320                 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);\" />";
321         }
322 }