7 if (window.XMLHttpRequest) {
10 req = new XMLHttpRequest();
14 } else if (window.ActiveXObject) {
17 req = new ActiveXObject("Msxml2.XMLHTTP");
20 req = new ActiveXObject("Microsoft.XMLHTTP");
30 if (typeof(window.innerWidth) == 'number') {
33 return [window.innerWidth * dpr, window.innerHeight * dpr];
34 } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
35 // IE 6+ in 'standards compliant mode'
36 return [document.documentElement.clientWidth, document.documentElement.clientHeight];
37 } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
39 return [document.body.clientWidth, document.body.clientHeight];
46 return window.devicePixelRatio || 1;
50 * pr0n can resize to any size we'd like, but we're much more likely
51 * to have this set of fixed-resolution screens cached, so to increase
52 * performance, we round down to the closest fit and use that. This
53 * function is a pessimal estimate of what thumbnail size we can _always_
54 * fit on the screen -- it's right if and only if all images are 4:3
55 * (and landscape). If individual size information is available, use
56 * pick_image_size, below.
75 function max_image_size(screen_size)
78 for (i = 0; i < fixed_sizes.length; ++i) {
79 if (screen_size[0] >= fixed_sizes[i][0] && screen_size[1] >= fixed_sizes[i][1]) {
80 return fixed_sizes[i];
86 function pick_image_size(screen_size, image_size)
89 for (i = 0; i < fixed_sizes.length; ++i) {
90 // this is a duplicate of pr0n's resizing code, hope for no floating-point
92 var thumbxres = fixed_sizes[i][0];
93 var thumbyres = fixed_sizes[i][1];
94 var width = image_size[0];
95 var height = image_size[1];
97 if (!(thumbxres >= width && thumbyres >= height)) {
98 var sfh = width / thumbxres;
99 var sfv = height / thumbyres;
107 width = Math.floor(width);
108 height = Math.floor(height);
111 if (screen_size[0] >= width && screen_size[1] >= height) {
112 // be sure _not_ to return a reference
113 return [ fixed_sizes[i][0], fixed_sizes[i][1], width, height ];
119 function replace_image_element(url, element_id, parent_node)
121 var img = document.getElementById(element_id);
123 if (img.src === url) {
126 img.parentNode.removeChild(img);
129 img = document.createElement("img");
133 parent_node.appendChild(img);
137 function rename_element(old_name, new_name)
139 // Remove any element that's in the way.
140 var elem = document.getElementById(new_name);
142 elem.parentNode.removeChild(elem);
145 elem = document.getElementById(old_name);
152 function display_image(width, height, evt, filename, element_id)
154 var url = "http://" + global_vhost + "/" + evt + "/" + width + "x" + height + "/nobox/" + filename;
155 var main = document.getElementById("iehack");
156 var preload = document.getElementById("preload");
157 var dpr = find_dpr();
159 // See if we have a preload going on that we can reuse.
160 if (element_id == "image" && preload !== null && preload.src == url) {
161 rename_element("preload_box", "image_box");
162 img = rename_element("preload", "image");
164 img = replace_image_element(url, element_id, main);
166 img.style.position = "absolute";
167 img.style.left = "0px";
168 img.style.top = "0px";
169 img.style.transformOrigin = "top left";
170 img.style.transform = "scale(" + (1.0 / dpr) + ")";
172 if (global_infobox != 'nobox/') {
175 url = "http://" + global_vhost + "/" + evt + "/" + width + "x" + height + "/box/" + filename;
177 url = "http://" + global_vhost + "/" + evt + "/" + width + "x" + height + "@" + dpr + "/box/" + filename;
179 var boximg = replace_image_element(url, element_id + "_box", main);
181 boximg.style.position = "absolute";
182 boximg.style.left = "0px";
183 boximg.style.bottom = "-1px";
184 boximg.style.transformOrigin = "bottom left";
185 boximg.style.transform = "scale(" + (1.0 / dpr) + ")";
191 function display_image_num(num, element_id)
193 var screen_size = find_width();
196 if (global_image_list[num][2] == -1) {
197 // no size information, use our pessimal guess
198 adjusted_size = max_image_size(screen_size);
200 adjusted_size = pick_image_size(screen_size, [ global_image_list[num][2], global_image_list[num][3] ]);
203 var img = display_image(adjusted_size[0], adjusted_size[1], global_image_list[num][0], global_image_list[num][1], element_id);
205 if (element_id == "image") {
206 // we want to shrink the box as much as possible if we know the true
210 // replace the anchor part (if any) with the image number
211 window.location.hash = "#" + (num+1);
217 function prepare_preload(img, num)
219 // cancel any pending preload
220 var preload = document.getElementById("preload");
221 if (preload !== null) {
222 preload.parentNode.removeChild(preload);
225 var preload_box = document.getElementById("preload_box");
226 if (preload_box !== null) {
227 preload_box.parentNode.removeChild(preload_box);
230 // grmf -- IE doesn't fire onload if the image was loaded from cache, so check for
231 // completeness first; should at least be _somewhat_ better
233 display_image_num(num, "preload");
235 img.onload = function() { display_image_num(num, "preload"); };
239 function can_go_next()
241 return (global_image_num < global_image_list.length - 1);
244 function can_go_previous()
246 return (global_image_num > 0);
249 function set_opacity(id, amount)
251 var elem = document.getElementById(id);
252 if (typeof(elem.style.opacity) != 'undefined') { // W3C
253 elem.style.opacity = amount;
254 } else if (typeof(elem.style.mozOpacity) != 'undefined') { // older Mozilla
255 elem.style.mozOpacity = amount;
256 } else if (typeof(elem.style.filter) != 'undefined') { // IE
257 if (elem.style.filter.indexOf("alpha") == -1) {
258 // add an alpha filter if there isn't one already
259 if (elem.style.filter) {
260 elem.style.filter += " ";
262 elem.style.filter = "";
264 elem.style.filter += "alpha(opacity=" + (amount*100.0) + ")";
266 // ugh? this seems to break in color index mode...
267 if (typeof(elem.filters) == 'unknown') {
268 elem.style.filter = "alpha(opacity=" + (amount*100.0) + ")";
270 elem.filters.alpha.opacity = (amount * 100.0);
273 } else { // no alpha support
275 elem.style.visibility = "visible";
276 elem.style.zorder = 1;
278 elem.style.visibility = "hidden";
283 function center_image(num)
285 var screen_size = find_width();
286 var dpr = find_dpr();
289 if (global_image_list[num][2] == -1) {
290 // no size information, use our pessimal guess
291 var adjusted_size = max_image_size(screen_size);
292 width = adjusted_size[0];
293 height = adjusted_size[1];
295 // use the exact information
296 var adjusted_size = pick_image_size(screen_size, [ global_image_list[num][2], global_image_list[num][3] ]);
297 width = adjusted_size[2];
298 height = adjusted_size[3];
301 // center the image on-screen
302 var main = document.getElementById("main");
303 main.style.position = "absolute";
304 main.style.left = (((screen_size[0] - width) / 2) / dpr) + "px";
305 main.style.top = (((screen_size[1] - height) / 2) / dpr) + "px";
306 main.style.width = (width / dpr) + "px";
307 main.style.height = (height / dpr) + "px";
308 main.style.lineHeight = (height / dpr) + "px";
313 var img = display_image_num(global_image_num, "image");
315 prepare_preload(img, global_image_num + 1);
318 set_opacity("previous", can_go_previous() ? 0.7 : 0.1);
319 set_opacity("next", can_go_next() ? 0.7 : 0.1);
320 set_opacity("close", 0.7);
323 function go_previous()
325 if (!can_go_previous()) {
329 var img = display_image_num(--global_image_num, "image");
330 if (can_go_previous()) {
331 set_opacity("previous", 0.7);
332 prepare_preload(img, global_image_num - 1);
334 set_opacity("previous", 0.1);
336 set_opacity("next", can_go_next() ? 0.7 : 0.1);
341 if (!can_go_next()) {
345 var img = display_image_num(++global_image_num, "image");
347 set_opacity("next", 0.7);
348 prepare_preload(img, global_image_num + 1);
350 set_opacity("next", 0.1);
352 set_opacity("previous", can_go_previous() ? 0.7 : 0.1);
357 window.location = global_return_url;
360 function draw_text(msg)
362 // remove any text we might have left
363 var text = document.getElementById("text");
365 text.parentNode.removeChild(text);
368 text = document.createElement("p");
370 text.style.position = "absolute";
371 text.style.color = "white";
372 text.style.lineHeight = "24px";
373 text.style.font = "24px verdana, arial, sans-serif";
374 text.innerHTML = msg;
376 var main = document.getElementById("main");
377 main.appendChild(text);
379 text.style.left = (main.clientWidth - text.clientWidth) / 2 + "px";
380 text.style.top = (main.clientHeight - text.clientHeight) / 2 + "px";
383 function fade_text(opacity)
385 set_opacity("text", opacity);
391 setTimeout("fade_text(" + opacity + ")", 30);
393 var text = document.getElementById("text");
395 text.parentNode.removeChild(text);
400 function select_image(evt, filename, selected)
407 draw_text("Selecting " + filename + "...");
409 draw_text("Unselecting " + filename + "...");
412 req.open("POST", "http://" + global_vhost + "/select", false);
413 req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
414 req.send("event=" + evt + "&filename=" + filename + "&selected=" + selected);
416 setTimeout("fade_text(0.99)", 30);
419 function key_down(which)
421 if (which == 39) { // right
423 set_opacity("next", 0.99);
425 } else if (which == 37) { // left
426 if (can_go_previous()) {
427 set_opacity("previous", 0.99);
429 } else if (which == 27) { // escape
430 set_opacity("close", 0.99);
432 check_for_hash_change();
436 function key_up(which) {
437 if (which == 39) { // right
439 set_opacity("next", 0.7);
442 } else if (which == 37) { // left
443 if (can_go_previous()) {
444 set_opacity("previous", 0.7);
447 } else if (which == 27) { // escape
448 set_opacity("close", 0.7);
450 } else if (which == 32 && global_select) { // space
451 select_image(global_image_list[global_image_num][0], global_image_list[global_image_num][1], 1);
452 } else if (which == 85 && global_select) { // u
453 select_image(global_image_list[global_image_num][0], global_image_list[global_image_num][1], 0);
455 check_for_hash_change();
459 // enable the horrible horrible IE PNG hack
460 function ie_png_hack()
462 var vstr = navigator.appVersion.split("MSIE");
463 var v = parseFloat(vstr[1]);
464 if (v >= 5.5 && v < 7.0 && document.body.filters) {
465 var next = document.getElementById("next");
466 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); }\" />";
468 var previous = document.getElementById("previous");
469 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); }\" />";
471 var close = document.getElementById("close");
472 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);\" />";
476 function parse_image_num(default_value) {
477 var num = parseInt(window.location.hash.substr(1));
478 if (num >= 1 && num <= global_image_list.length) { // and then num != NaN
481 return default_value;
485 function check_for_hash_change() {
486 var num = parse_image_num(-1);
487 if (num != -1 && num != global_image_num) {
488 global_image_num = num;