f087a8f185f8f953cd7f8cb935f6da4b272e41c8
[pr0n] / pr0n-fullscreen.js
1 function find_width()
2 {
3         if (typeof(window.innerWidth) == 'number') {
4                 // non-IE
5                 return [window.innerWidth, window.innerHeight];
6         } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
7                 // IE 6+ in 'standards compliant mode'
8                 return [document.documentElement.clientWidth, document.documentElement.clientHeight - 42];
9         } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
10                 // IE 4-compatible
11                 return [document.body.clientWidth, document.body.clientHeight];
12         }
13         return (null,null);
14 }
15
16 /*
17  * pr0n can resize to any size we'd like, but we're much more likely
18  * to have this set of fixed-resolution screens cached, so to increase
19  * performance, we round down to the closest fit and use that.
20  */
21 function reduce_to_fixed_width(size)
22 {
23         var fixed_sizes = [
24                 [ 1280, 960 ],
25                 [ 1024, 768 ],
26                 [ 800, 600 ],
27                 [ 640, 480 ],
28                 [ 512, 384 ],
29                 [ 320, 256 ],
30                 [ 240, 192 ],
31                 [ 120, 96 ],
32                 [ 80, 64 ]
33         ];
34         for (i = 0; i < fixed_sizes.length; ++i) {
35                 if (size[0] >= fixed_sizes[i][0] && size[1] >= fixed_sizes[i][1])
36                         return fixed_sizes[i];
37         }
38         return [ 80, 64 ];
39 }
40         
41 function display_image(width, height, evt, filename, element_id)
42 {
43         var url = "http://" + global_vhost + "/" + evt + "/" + width + "x" + height + "/" + filename;
44         var img = document.getElementById(element_id);
45         if (img != null) {
46                 img.src = "";
47                 img.parentNode.removeChild(img);
48         }
49
50         img = document.createElement("img");
51         img.id = element_id;
52         img.alt = "";
53
54         if (img.src != url) {
55                 img.src = url;
56         }
57         
58         var main = document.getElementById("main");
59         main.appendChild(img);
60
61         return img;
62 }
63
64 function prepare_preload(img, width, height, evt, filename)
65 {
66         // cancel any pending preload
67         var preload = document.getElementById("preload");
68         if (preload != null) {
69                 preload.src = "";
70                 preload.parentNode.removeChild(preload);
71         }
72                 
73 //      img.onload = function() { display_image(width, height, evt, filename, "preload"); }
74         img.onload = "display_image(" + width + "," + height + ",\"" + evt + "\",\"" + filename + "\",\"preload\");";
75 }
76
77 function relayout()
78 {
79         var size = find_width();
80         var adjusted_size = reduce_to_fixed_width(size);
81
82         var img = display_image(adjusted_size[0], adjusted_size[1], global_evt, global_image_list[global_image_num], "image");
83         if (can_go_next()) {
84                 prepare_preload(img, adjusted_size[0], adjusted_size[1], global_evt, global_image_list[global_image_num + 1]);
85         }
86         
87         // center the image on-screen
88         var main = document.getElementById("main");
89         main.style.position = "absolute";
90         main.style.left = (size[0] - adjusted_size[0]) / 2 + "px";
91         main.style.top = (size[1] - adjusted_size[1]) / 2 + "px"; 
92         main.style.width = adjusted_size[0] + "px";
93         main.style.height = adjusted_size[1] + "px";
94         main.style.lineHeight = adjusted_size[1] + "px"; 
95         if (document.all) {  // IE-specific
96                 main.style.fontSize = adjusted_size[1] + "px";
97         }
98
99         set_opacity("previous", can_go_previous() ? 0.7 : 0.1);
100         set_opacity("next", can_go_next() ? 0.7 : 0.1);
101 }
102
103 function set_opacity(id, amount)
104 {
105         var elem = document.getElementById(id);
106         if (typeof(elem.style.opacity) != 'undefined') {            // W3C
107                 elem.style.opacity = amount;
108         } else if (typeof(elem.style.mozOpacity) != 'undefined') {  // older Mozilla
109                 elem.style.mozOpacity = amount;
110         } else if (typeof(elem.style.filter) != 'undefined') {      // IE
111                 if (elem.style.filter.indexOf("alpha") == -1) {
112                         // add an alpha filter if there isn't one already
113                         if (elem.style.filter) {
114                                 elem.style.filter += " ";
115                         } else {
116                                 elem.style.filter = "";
117                         }
118                         elem.style.filter += "alpha(opacity=" + (amount*100.0) + ")";
119                 } else {
120                         elem.filters.alpha.opacity = (amount * 100.0);
121                 }
122         } else {                             // no alpha support
123                 if (amount > 0.5) {
124                         elem.style.visibility = "visible";
125                         elem.style.zorder = 1;
126                 } else {
127                         elem.style.visibility = "hidden";
128                 }
129         }
130 }
131
132 function can_go_previous()
133 {
134         return (global_image_num > 0);
135 }
136
137 function go_previous()
138 {
139         if (!can_go_previous())
140                 return;
141
142         --global_image_num;
143
144         var adjusted_size = reduce_to_fixed_width(find_width());
145
146         var img = display_image(adjusted_size[0], adjusted_size[1], global_evt, global_image_list[global_image_num], "image");
147         if (can_go_previous()) {
148                 set_opacity("previous", 0.7);
149                 prepare_preload(img, adjusted_size[0], adjusted_size[1], global_evt, global_image_list[global_image_num - 1]);
150         } else {
151                 set_opacity("previous", 0.1);
152         }
153         set_opacity("next", can_go_next() ? 0.7 : 0.1);
154 }
155
156 function can_go_next()
157 {
158         return (global_image_num < global_image_list.length - 1);
159 }
160
161 function go_next()
162 {
163         if (!can_go_next())
164                 return;
165
166         ++global_image_num;
167
168         var adjusted_size = reduce_to_fixed_width(find_width());
169
170         var img = display_image(adjusted_size[0], adjusted_size[1], global_evt, global_image_list[global_image_num], "image");
171         if (can_go_next()) {
172                 set_opacity("next", 0.7);
173                 prepare_preload(img, adjusted_size[0], adjusted_size[1], global_evt, global_image_list[global_image_num + 1]);
174         } else {
175                 set_opacity("next", 0.1);
176         }
177         set_opacity("previous", can_go_previous() ? 0.7 : 0.1);
178 }
179
180 function key_down(which)
181 {
182         if (which == 39) {   // right
183                 if (can_go_next()) {
184                         set_opacity("next", 0.99);
185                 }
186         } else if (which == 37) {   // left
187                 if (can_go_previous()) {
188                         set_opacity("previous", 0.99);
189                 }
190         }
191 }
192
193 function key_up(which) {
194         if (which == 39) {   // right
195                 if (can_go_next()) {
196                         set_opacity("next", 0.7);
197                         go_next();
198                 }
199         } else if (which == 37) {   // left
200                 if (can_go_previous()) {
201                         set_opacity("previous", 0.7);
202                         go_previous();
203                 }
204         }
205 }
206
207 // enable the horrible horrible IE PNG hack
208 function ie_png_hack()
209 {
210         var vstr = navigator.appVersion.split("MSIE");
211         var v = parseFloat(vstr[1]);
212         if (v >= 5.5 && v < 7.0 && document.body.filters) {
213                 var next = document.getElementById("next");
214                 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); }\" />";
215                 
216                 var previous = document.getElementById("previous");
217                 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); }\" />";
218         }
219 }