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