(function(f,b){function G(){var a=k("div",{className:"VIMG-pics"}),d=k("div",{className:"VIMG-list-rect"}),c=k("div",{className:"VIMG-list"}),h=f.createDocumentFragment();c.style.width=86*g.images.length+"px";b(k("a",{className:"slbtn"})).mousedown(p).appendTo(a);b(k("a",{className:"srbtn"})).mousedown(p).appendTo(a);for(var l=0,r=g.images.length;l<r;l++){var e=k("a",{className:"spic"}),H=b(k("img")).attr({"data-index":l,"data-src":g.images[l].getUrl(1)}).mousedown(p).appendTo(e);g.lockMrb&&H.contextmenu(function(a){a.preventDefault&&
a.preventDefault();a.stopPropagation&&a.stopPropagation();a.cancelBubble=!0;return!1});h.appendChild(e)}c.appendChild(h);b(a).mousedown(z);I(a);d.appendChild(c);a.appendChild(d);f.body.appendChild(a);A();c.style.left=B()+"px";b(".VIMG-pics").css({opacity:1,filter:"alpha(opacity=100)"});setTimeout(function(){if(b(".VIMG-pics").size()){var a=b(".VIMG-pics").get(0);a.style&&a.style.removeProperty?(a.style.removeProperty("opacity"),a.style.removeProperty("filter")):b(a).removeAttr("style")}},3E3);f.body.appendChild(k("div",
{className:"VIMG-txt",innerHTML:"&nbsp;"}))}function k(a,b,c){a=f.createElement(a);if(b)for(var h in b)a[h]=b[h];if(c)for(var l in c)a.style[l]=c[l];return a}function t(){if(b(".VIMG").size()&&this.src==b(".bigpic").attr("src")){var a=b(".VIMG").get(0).offsetWidth,d=b(".VIMG").get(0).offsetHeight,c=b(this).attr("data-w")||this.naturalWidth||this.width,h=b(this).attr("data-h")||this.naturalHeight||this.height;b(this).attr({"data-w":c,"data-h":h});c>a&&(h=parseInt(a/c*h,10),c=a);a=h/2;h>d?(a=d/2,q=
!0):q=!1;b(this).css({width:c+"px",height:h+"px","margin-top":"-"+a+"px","margin-left":"-"+c/2+"px",visibility:"visible"});b(".VIMG-image").css("background-image","none");q&&"img"==this.tagName.toLowerCase()&&(b(".VIMG-scrollbar").size()&&f.body.removeChild(b(".VIMG-scrollbar").get(0)),d=b(".bigpic"),c=b(".VIMG").get(0).offsetHeight,h=k("div",{className:"VIMG-scrollbar"}),b(h).css({top:b(".TOP-bh").size()?"35px":"0",left:b(".VIMG").get(0).offsetLeft+d.get(0).offsetLeft+parseInt(d.css("width"),10)+
"px",height:c/parseInt(d.css("height"),10)*c+"px"}).mousedown(J),f.body.appendChild(h))}}function C(a,d){if(q&&b(".bigpic").size()){var c=b(".VIMG").get(0).offsetHeight,h=parseInt(b(".VIMG-scrollbar").css("height"),10),l=c/2*-1,r=-1*(l+b(".bigpic").get(0).height),e=parseInt(b(".bigpic").css("margin-top"),10),f=parseInt(b(".VIMG-scrollbar").css("top"),10),g=b(".TOP-bh").size()?35:0,k=c-h+g-3,c=Math.round((c-h)/(l-r)*100)/100*d;a?(e-=d,f+=c):(e+=d,f-=c);e<r&&(e=r);e>l&&(e=l);f<g&&(f=g);f>k&&(f=k);b(".bigpic").css("margin-top",
e+"px");b(".VIMG-scrollbar").css("top",f+"px")}}function K(){var a=b(".VIMG-image");a.html("").css("background-image","url(http://i2.bahamut.com.tw/vimg_loader.gif)");var d=b(".VIMG-scrollbar").get(0);d&&f.body.removeChild(d);q=!1;d=g.images[m];if(d.youtube&&d.youtube.match(D))L(d.youtube);else{var c=k("img",{className:"bigpic"},{visibility:"hidden"});c.onload=t;g.lockMrb&&b(c).contextmenu(function(a){a.preventDefault&&a.preventDefault();a.stopPropagation&&a.stopPropagation();a.cancelBubble=!0;return!1});
M(c);b(c).mousedown(p).appendTo(a);c.src=d.getUrl(2)}}function M(a){if("ontouchstart"in document.documentElement&&a.addEventListener){var d,c,h,l,f,g,k,s,m;a.addEventListener("touchstart",function(a){var e=a.touches[0];d=parseInt(b(".VIMG-scrollbar").css("top"),10);c=e.clientX;h=e.clientY;l=b(".VIMG").get(0).offsetHeight;f=parseInt(b(".VIMG-scrollbar").css("height"),10);g=b(".TOP-bh").size()?35:0;k=l-f+g-3;s=l/2*-1;m=-1*(s+b(".bigpic").get(0).height);n(a)},!1);a.addEventListener("touchmove",function(a){var c=
d+-1*(a.touches[0].clientY-h);c<g&&(c=g);c>k&&(c=k);b(".VIMG-scrollbar").css("top",c+"px");var e=(s-m)/(l-f),e=Math.round(100*e)/100*(c-g),c=-1*(l/2+e);b(".bigpic").css("margin-top",c+"px");n(a)},!1);a.addEventListener("touchend",function(a){var b=a.changedTouches[0],d=b.clientX;80>Math.abs(b.clientY-h)&&200<Math.abs(d-c)&&(d>c?e.show("-1"):e.show("+1"));n(a)},!1)}}function I(a){if("ontouchstart"in document.documentElement&&a.addEventListener){var d,c,h;a.addEventListener("touchstart",function(a){var e=
a.touches[0];d=parseInt(b(".VIMG-list").css("left"),10)||0;c=e.clientX;h=e.clientY;n(a)},!1);a.addEventListener("touchmove",function(a){n(a)},!1);a.addEventListener("touchend",function(a){var f=a.changedTouches[0],g=d+(f.clientX-c);b(".VIMG-list").css("left",g+"px");E();86>Math.abs(f.clientX-c)&&86>Math.abs(f.clientY-h)&&(f=parseInt(b(a.target).attr("data-index"),10),isNaN(f)||e.show(f));n(a)},!1)}}function J(a){a=a||window.event;if(u(a.button)){n(a);var d=a.clientY-parseInt(b(".VIMG-scrollbar").css("top"),
10),c=b(".VIMG").get(0).offsetHeight,e=parseInt(b(".VIMG-scrollbar").css("height"),10),l=b(".TOP-bh").size()?35:0,g=c-e+l-3,m=c/2*-1,p=-1*(m+b(".bigpic").get(0).height);b(k("div",{className:"VIMG-drag-mask",id:"vimgDragMask"})).mousemove(function(a){a=a||window.event;n(a);a=a.clientY-d;a<l&&(a=l);a>g&&(a=g);b(".VIMG-scrollbar").css("top",a+"px");var f=(m-p)/(c-e),f=Math.round(100*f)/100*(a-l);a=-1*(c/2+f);b(".bigpic").css("margin-top",a+"px")}).mouseup(function(a){f.body.removeChild(f.getElementById("vimgDragMask"))}).appendTo(f.body)}}
function u(a){return-1!=navigator.userAgent.toLowerCase().indexOf("msie")?1==a:0==a}function n(a){a.preventDefault&&a.preventDefault();a.returnValue=!1}function v(a){a.stopPropagation&&a.stopPropagation();a.cancelBubble=!0}function z(a){a=a||window.event;if(u(a.button)){var b="VIMG-pics"==(a.target||a.srcElement).className?5:1,c=f.documentElement.clientWidth/2;a.clientX>=c?e.show("+"+b):a.clientX<c&&e.show("-"+b);n(a);v(a)}}function N(){var a=B();if(-1==navigator.userAgent.toLowerCase().indexOf("msie"))b(".VIMG-list").css("left",
a+"px");else{var d=parseInt(b(".VIMG-list").css("left"),10)||0,c=Math.abs(d-a)/25;10>c&&(c=10);var e=d<a?c:-c;clearInterval(w);w=setInterval(function(){var c=b(".VIMG-list"),d=parseInt(c.css("left"),10)||0;0<e&&d>=a||0>e&&d<=a?(c.css("left",a+"px"),clearInterval(w)):(d+=e,c.css("left",d+"px"))},10)}if(d=b("#vimgSelected").removeAttr("id").get(0))d.style.removeProperty?(d.style.removeProperty("opacity"),d.style.removeProperty("filter"),d.style.removeProperty("border-color")):b(d).removeAttr("style");
b(".VIMG-list > a:eq("+m+")").css(O).attr("id","vimgSelected");d=b(".VIMG-txt");c=g.images[m].title||"";g.copyrightText&&(c+="&nbsp;"+g.copyrightText);c?d.html(c).attr("title",c.replace(/&nbsp;/," ")):d.html("&nbsp;").removeAttr("title");E()}function E(){var a=parseInt(b(".VIMG-list").css("left"),10)||0,d=0<a?0:parseInt(Math.abs(a)/86),a=d+8;d--;a++;0>d&&(d=0);for(a>g.images.length-1&&(a=g.images.length-1);d<=a;d++){var c=b(".VIMG-list img:eq("+d+")");c.attr("src")||c.attr("src",c.attr("data-src"))}}
function A(){var a=b(".VIMG-pics").get(0).offsetWidth-102;1>a||(774<a&&(a=774),b(".VIMG-list-rect").css("width",a+"px"))}function B(){return b(".VIMG-list-rect").get(0).offsetWidth/2-43-86*m}function p(a){a=a||window.event;if(u(a.button)||a.touches){var d=a.target||a.srcElement;switch(d.className){case "slbtn":e.show("-5");break;case "srbtn":e.show("+5");break;case "lbtn":e.show("-1");break;case "rbtn":case "bigpic":e.show("+1");break;case "xbtn":case "VIMG-container":e.destroy();break;default:d=
parseInt(b(d).attr("data-index"),10),isNaN(d)||e.show(d)}v(a)}}function L(a){a=a.replace(/\&amp;/,"&");a.match(D)&&(a=a.replace("/v/","/embed/"),a='<iframe class="bigpic" width="640" height="360" src="'+a+'" frameborder="0" allowfullscreen></iframe>',b(".VIMG-image").html(a),t.call(b(".bigpic").get(0)))}var P=/http:\/\/((?:pic|p2)\.bahamut\.com\.tw)\/[BMS]\/2KU\/[0-9]{2}\/([0-9]{10}\.(:?JPG|GIF|PNG|BMP|JPEG))(?:\?.+?)?/,Q=/http:\/\/truth\.bahamut\.com\.tw\/(s[0-9]{2})\/s?([0-9]{6})\/([a-z0-9A-Z]{32}\.(:?PNG|GIF|JPG))(?:\?.+?)?/,
D=/^http:\/\/www\.youtube\.com\/v\/([0-9a-zA-Z_-]{11})(?:(?:[\&\?]\w+=[\w\.0-9]+)*)$/,O={opacity:1,filter:"alpha(opacity=100)","border-color":"#fff"},R={acg:"/ajax/acg_loadpic.php",acgPreview:"/ajax/acg_preview_ajax.php"},g,x=[],m=0,q=!1,y=-1,w,e=function(a,d){d=d||{};this.copyrightText=d.copyrightText||"";this.ajax=d.ajax;this.ajaxData=d.ajaxData;this.lockMrb=d.lockMrb||!1;this.ajax?this.images=[]:this.setImages(a);b(a).attr("data-gallery-index",x.length).css("cursor","pointer").click(function(a){a=
a||window.event;var d=a.target||a.srcElement;a=b(d).attr("data-gallery-index");g=x[a];g.ajax&&!g.images.length?b.get(R[g.ajax],g.ajaxData,function(a){a?(g.setImages(a),g.create(d)):alert("\u8cc7\u6599\u67e5\u8a62\u932f\u8aa4")}):g.create(d)});x.push(this)};e.prototype.setImages=function(a){"string"==typeof a&&(a=a.split("\n"));this.images=[];for(var b=0,c=a.length;b<c;b++)a[b]&&this.images.push(new F(a[b]))};e.prototype.getIndex=function(a){a=a.getUrl(1);for(var b=0,c=this.images.length;b<c;b++)if(this.images[b].getUrl(1)==
a)return b;return 0};e.prototype.create=function(a){b(".VIMG").size()&&e.destroy();if(this.images&&this.images.length){a=new F(a);m=this.getIndex(a);y=-1;G();a=k("div",{className:"VIMG-container"});var d=k("div",{className:"VIMG"}),c=k("div",{className:"VIMG-image"}),h=k("div",{className:"VIMG-mask"}),g=f.documentElement.clientHeight-37,n=0;b(".TOP-bh").size()&&(g-=36,n+=35);b(a).css({top:n+"px",height:g+"px"}).mousedown(p);b(c).css("height",g+"px").mousedown(z);b(k("a",{className:"lbtn"})).mousedown(p).appendTo(d);
b(k("a",{className:"rbtn"})).mousedown(p).appendTo(d);b(k("a",{className:"xbtn"})).mousedown(p).appendTo(d);d.appendChild(c);a.appendChild(d);f.body.appendChild(h);f.body.appendChild(a);e.show(m)}};e.show=function(a){m="string"==typeof a?m+parseInt(a,10):a;0>m?m=0:m>=g.images.length&&(m=g.images.length-1);m==y?a=!1:(y=m,a=!0);a&&(N(),K())};e.destroy=function(){f.body.removeChild(b(".VIMG-mask").get(0));f.body.removeChild(b(".VIMG-txt").get(0));f.body.removeChild(b(".VIMG-pics").get(0));f.body.removeChild(b(".VIMG-container").get(0));
b(".VIMG-scrollbar").size()&&f.body.removeChild(b(".VIMG-scrollbar").get(0))};var F=function(a){if(a.tagName&&"img"==a.tagName.toLowerCase())this.src=a.src,this.title=a.title;else if("string"==typeof a){a=a.split("<>");var b=a[0].split("|");this.src=b[0];this.title=a[1]||"";this.youtube=b[1]||""}else this.src="";this.getUrl=function(a){var b;return(b=this.src.match(P))?"http://"+b[1]+"/"+(1==a?"S":"B")+"/2KU/"+b[2].substring(8,10)+"/"+b[2]:(b=this.src.match(Q))?"http://truth.bahamut.com.tw/"+b[1]+
"/"+(1==a?"s":"")+b[2]+"/"+b[3]:this.src}};window.onresize=function(){if(b(".VIMG").size()){var a=f.documentElement.clientHeight-37;b(".TOP-bh").size()&&(a-=36);b(".VIMG-container,.VIMG-image").css("height",a+"px");b(".bigpic").size()&&t.call(b(".bigpic").get(0));A()}};(function(a,b){var c=navigator.userAgent.match(/firefox/i)?"DOMMouseScroll":"mousewheel";a.attachEvent?a.attachEvent("on"+c,b):a.addEventListener?a.addEventListener(c,b,!1):a["on"+c]=b})(f,function(a){a=a||window.event;q&&b(".bigpic").size()&&
(C(a.wheelDelta&&0>a.wheelDelta||a.detail&&0<a.detail,120),n(a))});b(f).keydown(function(a){if(b(".VIMG").size()){switch(a.keyCode){case 27:e.destroy();break;case 36:case 90:e.show(0);break;case 35:case 86:e.show(g.images.length-1);break;case 37:case 88:e.show("-1");break;case 39:case 67:e.show("+1");break;case 33:e.show("-5");break;case 34:e.show("+5");break;case 38:case 40:C(40==a.keyCode,60)}n(a)}});"ontouchstart"in document.documentElement&&document.documentElement.addEventListener&&document.documentElement.addEventListener("touchstart",
function(a){v(a)},!1);b().jquery?b.fn.imageGallery=function(a){new e(this.get(),a)}:b().Egg&&(b.extend.imageGallery=function(a){new e(this.get(),a)},b.extend.imagebox=function(a,b,c,f){new e(this.get(),{copyrightText:a,ajax:b,ajaxData:c,lockMrb:f})})})(document,$);
