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