]> git.sesse.net Git - pr0n/blob - pr0n-fullscreen.js
d28c2a4421ccbacbcec99d7266c3be6cf01ab4fb
[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         if (document.all) {  // IE-specific
74                 img.onload = "display_image(" + width + "," + height + ",\"" + evt + "\",\"" + filename + "\",\"preload\");";
75         } else {
76                 img.onload = function() { display_image(width, height, evt, filename, "preload"); }
77         }
78 }
79
80 function relayout()
81 {
82         var size = find_width();
83         var adjusted_size = reduce_to_fixed_width(size);
84
85         var img = display_image(adjusted_size[0], adjusted_size[1], global_evt, global_image_list[global_image_num], "image");
86         if (can_go_next()) {
87                 prepare_preload(img, adjusted_size[0], adjusted_size[1], global_evt, global_image_list[global_image_num + 1]);
88         }
89         
90         // center the image on-screen
91         var main = document.getElementById("main");
92         main.style.position = "absolute";
93         main.style.left = (size[0] - adjusted_size[0]) / 2 + "px";
94         main.style.top = (size[1] - adjusted_size[1]) / 2 + "px"; 
95         main.style.width = adjusted_size[0] + "px";
96         main.style.height = adjusted_size[1] + "px";
97         main.style.lineHeight = adjusted_size[1] + "px"; 
98         if (document.all) {  // IE-specific
99                 main.style.fontSize = adjusted_size[1] + "px";
100         }
101
102         set_opacity("previous", can_go_previous() ? 0.7 : 0.1);
103         set_opacity("next", can_go_next() ? 0.7 : 0.1);
104 }
105
106 function set_opacity(id, amount)
107 {
108         var elem = document.getElementById(id);
109         if (typeof(elem.style.opacity) != 'undefined') {            // W3C
110                 elem.style.opacity = amount;
111         } else if (typeof(elem.style.mozOpacity) != 'undefined') {  // older Mozilla
112                 elem.style.mozOpacity = amount;
113         } else if (typeof(elem.style.filter) != 'undefined') {      // IE
114                 if (elem.style.filter.indexOf("alpha") == -1) {
115                         // add an alpha filter if there isn't one already
116                         if (elem.style.filter) {
117                                 elem.style.filter += " ";
118                         } else {
119                                 elem.style.filter = "";
120                         }
121                         elem.style.filter += "alpha(opacity=" + (amount*100.0) + ")";
122                 } else {
123                         elem.filters.alpha.opacity = (amount * 100.0);
124                 }
125         } else {                             // no alpha support
126                 if (amount > 0.5) {
127                         elem.style.visibility = "visible";
128                         elem.style.zorder = 1;
129                 } else {
130                         elem.style.visibility = "hidden";
131                 }
132         }
133 }
134
135 function can_go_previous()
136 {
137         return (global_image_num > 0);
138 }
139
140 function go_previous()
141 {
142         if (!can_go_previous())
143                 return;
144
145         --global_image_num;
146
147         var adjusted_size = reduce_to_fixed_width(find_width());
148
149         var img = display_image(adjusted_size[0], adjusted_size[1], global_evt, global_image_list[global_image_num], "image");
150         if (can_go_previous()) {
151                 set_opacity("previous", 0.7);
152                 prepare_preload(img, adjusted_size[0], adjusted_size[1], global_evt, global_image_list[global_image_num - 1]);
153         } else {
154                 set_opacity("previous", 0.1);
155         }
156         set_opacity("next", can_go_next() ? 0.7 : 0.1);
157 }
158
159 function can_go_next()
160 {
161         return (global_image_num < global_image_list.length - 1);
162 }
163
164 function go_next()
165 {
166         if (!can_go_next())
167                 return;
168
169         ++global_image_num;
170
171         var adjusted_size = reduce_to_fixed_width(find_width());
172
173         var img = display_image(adjusted_size[0], adjusted_size[1], global_evt, global_image_list[global_image_num], "image");
174         if (can_go_next()) {
175                 set_opacity("next", 0.7);
176                 prepare_preload(img, adjusted_size[0], adjusted_size[1], global_evt, global_image_list[global_image_num + 1]);
177         } else {
178                 set_opacity("next", 0.1);
179         }
180         set_opacity("previous", can_go_previous() ? 0.7 : 0.1);
181 }
182
183 function key_down(which)
184 {
185         if (which == 39) {   // right
186                 if (can_go_next()) {
187                         set_opacity("next", 0.99);
188                 }
189         } else if (which == 37) {   // left
190                 if (can_go_previous()) {
191                         set_opacity("previous", 0.99);
192                 }
193         }
194 }
195
196 function key_up(which) {
197         if (which == 39) {   // right
198                 if (can_go_next()) {
199                         set_opacity("next", 0.7);
200                         go_next();
201                 }
202         } else if (which == 37) {   // left
203                 if (can_go_previous()) {
204                         set_opacity("previous", 0.7);
205                         go_previous();
206                 }
207         }
208 }
209
210 // enable the horrible horrible IE PNG hack
211 function ie_png_hack()
212 {
213         var vstr = navigator.appVersion.split("MSIE");
214         var v = parseFloat(vstr[1]);
215         if (v >= 5.5 && v < 7.0 && document.body.filters) {
216                 var next = document.getElementById("next");
217                 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); }\" />";
218                 
219                 var previous = document.getElementById("previous");
220                 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); }\" />";
221         }
222 }