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