]> git.sesse.net Git - pr0n/blob - pr0n-fullscreen.js
468f249c0c6c780b30c87eed132d5923f8ec8968
[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];
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
99         set_opacity("previous", can_go_previous() ? 0.7 : 0.1);
100         set_opacity("next", can_go_next() ? 0.7 : 0.1);
101         set_opacity("close", 0.7);
102 }
103
104 function set_opacity(id, amount)
105 {
106         var elem = document.getElementById(id);
107         if (typeof(elem.style.opacity) != 'undefined') {            // W3C
108                 elem.style.opacity = amount;
109         } else if (typeof(elem.style.mozOpacity) != 'undefined') {  // older Mozilla
110                 elem.style.mozOpacity = amount;
111         } else if (typeof(elem.style.filter) != 'undefined') {      // IE
112                 if (elem.style.filter.indexOf("alpha") == -1) {
113                         // add an alpha filter if there isn't one already
114                         if (elem.style.filter) {
115                                 elem.style.filter += " ";
116                         } else {
117                                 elem.style.filter = "";
118                         }
119                         elem.style.filter += "alpha(opacity=" + (amount*100.0) + ")";
120                 } else {
121                         elem.filters.alpha.opacity = (amount * 100.0);
122                 }
123         } else {                             // no alpha support
124                 if (amount > 0.5) {
125                         elem.style.visibility = "visible";
126                         elem.style.zorder = 1;
127                 } else {
128                         elem.style.visibility = "hidden";
129                 }
130         }
131 }
132
133 function can_go_previous()
134 {
135         return (global_image_num > 0);
136 }
137
138 function go_previous()
139 {
140         if (!can_go_previous())
141                 return;
142
143         --global_image_num;
144
145         var adjusted_size = reduce_to_fixed_width(find_width());
146
147         var img = display_image(adjusted_size[0], adjusted_size[1], global_evt, global_image_list[global_image_num], "image");
148         if (can_go_previous()) {
149                 set_opacity("previous", 0.7);
150                 prepare_preload(img, adjusted_size[0], adjusted_size[1], global_evt, global_image_list[global_image_num - 1]);
151         } else {
152                 set_opacity("previous", 0.1);
153         }
154         set_opacity("next", can_go_next() ? 0.7 : 0.1);
155 }
156
157 function can_go_next()
158 {
159         return (global_image_num < global_image_list.length - 1);
160 }
161
162 function go_next()
163 {
164         if (!can_go_next())
165                 return;
166
167         ++global_image_num;
168
169         var adjusted_size = reduce_to_fixed_width(find_width());
170
171         var img = display_image(adjusted_size[0], adjusted_size[1], global_evt, global_image_list[global_image_num], "image");
172         if (can_go_next()) {
173                 set_opacity("next", 0.7);
174                 prepare_preload(img, adjusted_size[0], adjusted_size[1], global_evt, global_image_list[global_image_num + 1]);
175         } else {
176                 set_opacity("next", 0.1);
177         }
178         set_opacity("previous", can_go_previous() ? 0.7 : 0.1);
179 }
180
181 function key_down(which)
182 {
183         if (which == 39) {   // right
184                 if (can_go_next()) {
185                         set_opacity("next", 0.99);
186                 }
187         } else if (which == 37) {   // left
188                 if (can_go_previous()) {
189                         set_opacity("previous", 0.99);
190                 }
191         } else if (which == 27) {   // escape
192                 set_opacity("close", 0.99);
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         } else if (which == 27) {   // escape
208                 set_opacity("close", 0.7);
209                 do_close();
210         }
211 }
212
213 function do_close()
214 {
215         window.location = global_return_url;
216 }
217
218 // enable the horrible horrible IE PNG hack
219 function ie_png_hack()
220 {
221         var vstr = navigator.appVersion.split("MSIE");
222         var v = parseFloat(vstr[1]);
223         if (v >= 5.5 && v < 7.0 && document.body.filters) {
224                 var next = document.getElementById("next");
225                 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); }\" />";
226                 
227                 var previous = document.getElementById("previous");
228                 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); }\" />";
229         }
230 }