5 req = new XMLHttpRequest();
11 return [window.innerWidth * dpr, window.innerHeight * dpr];
16 return window.devicePixelRatio || 1;
20 * pr0n can resize to any size we'd like, but we're much more likely
21 * to have this set of fixed-resolution screens cached, so to increase
22 * performance, we round down to the closest fit and use that. This
23 * function is a pessimal estimate of what thumbnail size we can _always_
24 * fit on the screen -- it's right if and only if all images are 4:3
25 * (and landscape). If individual size information is available, use
26 * pick_image_size, below.
45 function max_image_size(screen_size)
48 for (i = 0; i < fixed_sizes.length; ++i) {
49 if (screen_size[0] >= fixed_sizes[i][0] && screen_size[1] >= fixed_sizes[i][1]) {
50 return fixed_sizes[i];
56 function pick_image_size(screen_size, image_size)
59 for (i = 0; i < fixed_sizes.length; ++i) {
60 // this is a duplicate of pr0n's resizing code, hope for no floating-point
62 var thumbxres = fixed_sizes[i][0];
63 var thumbyres = fixed_sizes[i][1];
64 var width = image_size[0];
65 var height = image_size[1];
67 if (!(thumbxres >= width && thumbyres >= height)) {
68 var sfh = width / thumbxres;
69 var sfv = height / thumbyres;
77 width = Math.floor(width);
78 height = Math.floor(height);
81 if (screen_size[0] >= width && screen_size[1] >= height) {
82 // be sure _not_ to return a reference
83 return [ fixed_sizes[i][0], fixed_sizes[i][1], width, height ];
89 function replace_image_element(url, element_id, parent_node)
91 var img = document.getElementById(element_id);
93 if (img.src === url) {
96 img.parentNode.removeChild(img);
99 img = document.createElement("img");
103 parent_node.appendChild(img);
107 function rename_element(old_name, new_name)
109 // Remove any element that's in the way.
110 var elem = document.getElementById(new_name);
112 elem.parentNode.removeChild(elem);
115 elem = document.getElementById(old_name);
122 function display_image(width, height, evt, filename, element_id)
124 var url = window.location.origin + "/" + evt + "/" + width + "x" + height + "/nobox/" + filename;
125 var main = document.getElementById("iehack");
126 var preload = document.getElementById("preload");
127 var dpr = find_dpr();
129 // See if we have a preload going on that we can reuse.
130 if (element_id == "image" && preload !== null && preload.src == url) {
131 rename_element("preload_box", "image_box");
132 img = rename_element("preload", "image");
134 img = replace_image_element(url, element_id, main);
136 img.style.position = "absolute";
137 img.style.left = "0px";
138 img.style.top = "0px";
139 img.style.transformOrigin = "top left";
140 img.style.transform = "scale(" + (1.0 / dpr) + ")";
145 url = window.location.origin + "/" + evt + "/" + width + "x" + height + "/box/" + filename;
147 url = window.location.origin + "/" + evt + "/" + width + "x" + height + "@" + dpr.toFixed(2) + "/box/" + filename;
149 var boximg = replace_image_element(url, element_id + "_box", main);
151 boximg.style.position = "absolute";
152 boximg.style.left = "0px";
153 boximg.style.bottom = "-1px";
154 boximg.style.transformOrigin = "bottom left";
155 boximg.style.transform = "scale(" + (1.0 / dpr) + ")";
160 function display_image_num(num, element_id)
162 var screen_size = find_width();
165 if (global_image_list[num][2] == -1) {
166 // no size information, use our pessimal guess
167 adjusted_size = max_image_size(screen_size);
169 adjusted_size = pick_image_size(screen_size, [ global_image_list[num][2], global_image_list[num][3] ]);
172 var img = display_image(adjusted_size[0], adjusted_size[1], global_image_list[num][0], global_image_list[num][1], element_id);
174 if (element_id == "image") {
175 // we want to shrink the box as much as possible if we know the true
179 // replace the anchor part (if any) with the image number
180 window.location.hash = "#" + (num+1);
186 function prepare_preload(img, num)
188 // cancel any pending preload
189 var preload = document.getElementById("preload");
190 if (preload !== null) {
191 preload.parentNode.removeChild(preload);
194 var preload_box = document.getElementById("preload_box");
195 if (preload_box !== null) {
196 preload_box.parentNode.removeChild(preload_box);
199 // grmf -- IE doesn't fire onload if the image was loaded from cache, so check for
200 // completeness first; should at least be _somewhat_ better
202 display_image_num(num, "preload");
204 img.onload = function() { display_image_num(num, "preload"); };
208 function can_go_next()
210 return (global_image_num < global_image_list.length - 1);
213 function can_go_previous()
215 return (global_image_num > 0);
218 function set_opacity(id, amount)
220 var elem = document.getElementById(id);
221 if (typeof(elem.style.opacity) != 'undefined') { // W3C
222 elem.style.opacity = amount;
223 } else if (typeof(elem.style.mozOpacity) != 'undefined') { // older Mozilla
224 elem.style.mozOpacity = amount;
225 } else if (typeof(elem.style.filter) != 'undefined') { // IE
226 if (elem.style.filter.indexOf("alpha") == -1) {
227 // add an alpha filter if there isn't one already
228 if (elem.style.filter) {
229 elem.style.filter += " ";
231 elem.style.filter = "";
233 elem.style.filter += "alpha(opacity=" + (amount*100.0) + ")";
235 // ugh? this seems to break in color index mode...
236 if (typeof(elem.filters) == 'unknown') {
237 elem.style.filter = "alpha(opacity=" + (amount*100.0) + ")";
239 elem.filters.alpha.opacity = (amount * 100.0);
242 } else { // no alpha support
244 elem.style.visibility = "visible";
245 elem.style.zorder = 1;
247 elem.style.visibility = "hidden";
252 function center_image(num)
254 var screen_size = find_width();
255 var dpr = find_dpr();
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];
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];
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) / dpr) + "px";
274 main.style.top = (((screen_size[1] - height) / 2) / dpr) + "px";
275 main.style.width = (width / dpr) + "px";
276 main.style.height = (height / dpr) + "px";
277 main.style.lineHeight = (height / dpr) + "px";
282 var img = display_image_num(global_image_num, "image");
284 prepare_preload(img, global_image_num + 1);
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);
292 function go_previous()
294 if (!can_go_previous()) {
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);
303 set_opacity("previous", 0.1);
305 set_opacity("next", can_go_next() ? 0.7 : 0.1);
310 if (!can_go_next()) {
314 var img = display_image_num(++global_image_num, "image");
316 set_opacity("next", 0.7);
317 prepare_preload(img, global_image_num + 1);
319 set_opacity("next", 0.1);
321 set_opacity("previous", can_go_previous() ? 0.7 : 0.1);
326 window.location = global_return_url;
329 function draw_text(msg)
331 // remove any text we might have left
332 var text = document.getElementById("text");
334 text.parentNode.removeChild(text);
337 text = document.createElement("p");
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;
345 var main = document.getElementById("main");
346 main.appendChild(text);
348 text.style.left = (main.clientWidth - text.clientWidth) / 2 + "px";
349 text.style.top = (main.clientHeight - text.clientHeight) / 2 + "px";
352 function fade_text(opacity)
354 set_opacity("text", opacity);
360 setTimeout("fade_text(" + opacity + ")", 30);
362 var text = document.getElementById("text");
364 text.parentNode.removeChild(text);
369 function select_image(evt, filename, selected)
376 draw_text("Selecting " + filename + "...");
378 draw_text("Unselecting " + filename + "...");
381 req.open("POST", window.location.origin + "/select", false);
382 req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
383 req.send("event=" + evt + "&filename=" + filename + "&selected=" + selected);
385 setTimeout("fade_text(0.99)", 30);
388 function key_down(which)
390 if (which == 39) { // right
392 set_opacity("next", 0.99);
394 } else if (which == 37) { // left
395 if (can_go_previous()) {
396 set_opacity("previous", 0.99);
398 } else if (which == 27) { // escape
399 set_opacity("close", 0.99);
401 check_for_hash_change();
405 function key_up(which) {
406 if (which == 39) { // right
408 set_opacity("next", 0.7);
411 } else if (which == 37) { // left
412 if (can_go_previous()) {
413 set_opacity("previous", 0.7);
416 } else if (which == 27) { // escape
417 set_opacity("close", 0.7);
419 } else if (which == 32 && global_select) { // space
420 select_image(global_image_list[global_image_num][0], global_image_list[global_image_num][1], 1);
421 } else if (which == 85 && global_select) { // u
422 select_image(global_image_list[global_image_num][0], global_image_list[global_image_num][1], 0);
424 check_for_hash_change();
428 function parse_image_num(default_value) {
429 var num = parseInt(window.location.hash.substr(1));
430 if (num >= 1 && num <= global_image_list.length) { // and then num != NaN
433 return default_value;
437 function check_for_hash_change() {
438 var num = parse_image_num(-1);
439 if (num != -1 && num != global_image_num) {
440 global_image_num = num;