(function(a){var b,c,d,e,f,g,h,i,j,k,l=0,m={},n=[],o=0,p={},q=[],r=new Image,s,t=1,u="",v,w,x=false,y=a.extend(a("<div/>")[0],{prop:0}),z=function(){c.hide();r.onerror=r.onload=null;b.empty()},A=function(){var d=n[l],e,f,h;z();m=a.extend({},a.fn.fimg.defaults,typeof a(d).data("fimg")=="undefined"?m:a(d).data("fimg"));if(h===false){x=false;return}else if(typeof h=="object"){m=a.extend(m,h)}f=m.title||(d.nodeName?a(d).attr("title"):d.title)||"";if(d.nodeName&&!m.orig){m.orig=a(d).children("img:first").length?a(d).children("img:first"):a(d)}if(f===""&&m.orig){f=m.orig.attr("alt")}e=m.href||(d.nodeName?a(d).attr("href"):d.href)||null;m.href=e;m.title=f;b.css("padding",m.padding+m.margin);a(".fimg-inline-tmp").unbind("fimg-cancel").bind("fimg-change",function(){a(this).replaceWith(g.children())});x=false;clearInterval(s);c.show();s=setInterval(J,66);r=new Image;r.onerror=function(){b.html("<p>Sorry, the image cannot be loaded.<br />Try the next image.</p>");b.wrapInner('<div style="width:auto;height:auto;overflow:auto;position:relative;"></div>');m.width=b.width();m.height=b.height();m.titleShow=false;B()};r.onload=function(){x=true;r.onerror=r.onload=null;m.width=r.width;m.height=r.height;a("<img />").attr({id:"fimg-img",src:r.src,alt:m.title}).appendTo(b);B()};r.src=e},B=function(){var f,i;c.hide();x=true;a(g.add(d)).unbind();a(window).unbind("resize.fb scroll.fb");a(document).unbind("keydown.fb");if(e.is(":visible")){e.css("height",e.height())}q=n;o=l;p=m;d.css({"background-color":"#111",opacity:.85,cursor:"pointer",height:a(document).height()});if(!d.is(":visible")){d.show()}w=I();C();if(e.is(":visible")){a(h.add(j).add(k)).hide();f=e.position(),v={top:f.top,left:f.left,width:e.width(),height:e.height()};i=v.width==w.width&&v.height==w.height;g.fadeTo("fast",.3,function(){var c=function(){g.html(b.contents()).fadeTo("fast",1,E)};a.event.trigger("fimg-change");g.empty().css({"border-width":p.padding,width:w.width-p.padding*2,height:w.height-p.padding*2});if(i){c()}else{y.prop=0;a(y).animate({prop:1},{duration:130,step:G,complete:c})}});return}e.removeAttr("style");g.css("border-width",p.padding);g.css({width:w.width-p.padding*2,height:w.height-p.padding*2}).html(b.contents());e.css(w).fadeIn(300,E)},C=function(){u=p.title||"";i.empty().removeAttr("style").removeClass();if(p.titleShow===false){i.hide();return}u='<span id="fimg-title-over">Image '+(o+1)+" / "+q.length+(u.length?"   "+u:"")+"</span>";if(!u||u===""){i.hide();return}i.addClass("fimg-title-over").html(u).appendTo("body").show();i.css({marginLeft:p.padding,width:w.width-p.padding*2,bottom:p.padding}).appendTo(f);i.hide()},D=function(){a(document).bind("keydown.fb",function(b){if(b.keyCode==27){b.preventDefault();a.fimg.close()}else if(b.keyCode==37||b.keyCode==39){b.preventDefault();a.fimg[b.keyCode==37?"prev":"next"]()}});if(o!==0){j.show()}if(o!=q.length-1){k.show()}},E=function(){e.css("height","auto");if(u&&u.length){i.show()}h.show();D();d.bind("click",a.fimg.close);a(window).bind("resize.fb",a.fimg.resize);e.show();x=false;a.fimg.center();F()},F=function(){var a,b;if(q.length-1>o){a=q[o+1].href;if(typeof a!=="undefined"){b=new Image;b.src=a}}if(o>0){a=q[o-1].href;if(typeof a!=="undefined"){b=new Image;b.src=a}}},G=function(a){var b={width:parseInt(v.width+(w.width-v.width)*a,10),height:parseInt(v.height+(w.height-v.height)*a,10),top:parseInt(v.top+(w.top-v.top)*a,10),left:parseInt(v.left+(w.left-v.left)*a,10)};if(typeof w.opacity!=="undefined"){b.opacity=a<.5?.5:a}e.css(b);g.css({width:b.width-p.padding*2,height:b.height-p.padding*2})},H=function(){return[a(window).width()-p.margin*2,a(window).height()-p.margin*2,a(document).scrollLeft()+p.margin,a(document).scrollTop()+p.margin]},I=function(){var a=H(),b={},c=p.padding*2,d;b.width=p.width+c;b.height=p.height+c;if(b.width>a[0]||b.height>a[1]){d=p.width/p.height;if(b.width>a[0]){b.width=a[0];b.height=parseInt((b.width-c)/d+c,10)}if(b.height>a[1]){b.height=a[1];b.width=parseInt((b.height-c)*d+c,10)}}b.top=parseInt(Math.max(a[3]-20,a[3]+(a[1]-b.height-40)*.5),10);b.left=parseInt(Math.max(a[2]-20,a[2]+(a[0]-b.width-40)*.5),10);return b},J=function(){if(!c.is(":visible")){clearInterval(s);return}a("div",c).css("top",t*-40+"px");t=(t+1)%12};a.fn.fimg=function(b){if(!a(this).length){return this}a(this).data("fimg",a.extend({},b,a.metadata?a(this).metadata():{})).unbind("click.fb").bind("click.fb",function(b){b.preventDefault();if(x){return}x=true;a(this).blur();n=[];l=0;var c=a(this).attr("rel");n=a("a[rel="+c+"]");l=n.index(this);A();return});return this};a.fimg=function(a){if(x){return}x=true;var b={};n=[];l=0;if(l>n.length||l<0){l=0}A()};a.fimg.next=function(){return a.fimg.pos(o+1)};a.fimg.prev=function(){return a.fimg.pos(o-1)};a.fimg.pos=function(a){if(x){return}a=parseInt(a);n=q;if(a>-1&&a<q.length){l=a;A()}return};a.fimg.cancel=function(){if(x){return}x=true;a.event.trigger("fimg-cancel");z();x=false};a.fimg.close=function(){function b(){d.fadeOut("fast");i.empty().hide();e.hide();a.event.trigger("fimg-cleanup");g.empty();q=m=[];o=l=0;p=m={};x=false}if(x||e.is(":hidden")){return}x=true;z();a(h.add(j).add(k)).hide();a(g.add(d)).unbind();a(window).unbind("resize.fb scroll.fb");a(document).unbind("keydown.fb");g.find("iframe").attr("src","about:blank");i.empty();e.stop();e.fadeOut(300,b)};a.fimg.resize=function(){if(d.is(":visible")){d.css("height",a(document).height())}a.fimg.center(true)};a.fimg.center=function(){var a,b;if(x){return}b=arguments[0]===true?1:0;a=H();if(!b&&(e.width()>a[0]||e.height()>a[1])){return}e.stop().animate({top:parseInt(Math.max(a[3]-20,a[3]+(a[1]-g.height()-40)*.5-p.padding)),left:parseInt(Math.max(a[2]-20,a[2]+(a[0]-g.width()-40)*.5-p.padding))},typeof arguments[0]=="number"?arguments[0]:200)};a.fimg.init=function(){if(a("#fimg-wrap").length){return}a("body").append(b=a('<div id="fimg-tmp"></div>'),c=a('<div id="fimg-loading"><div></div></div>'),d=a('<div id="fimg-overlay"></div>'),e=a('<div id="fimg-wrap"></div>'));f=a('<div id="fimg-outer"></div>').append('<div class="fimg-bg"></div>').appendTo(e);f.append(g=a('<div id="fimg-content"></div>'),h=a('<a id="fimg-close"></a>'),i=a('<div id="fimg-title"></div>'),j=a('<a href="javascript:;" id="fimg-left"><span id="fimg-left-ico"></span></a>'),k=a('<a href="javascript:;" id="fimg-right"><span id="fimg-right-ico"></span></a>'));h.click(a.fimg.close);c.click(a.fimg.cancel);j.click(function(b){b.preventDefault();a.fimg.prev()});k.click(function(b){b.preventDefault();a.fimg.next()})};a.fn.fimg.defaults={padding:15,margin:15,width:560,height:340,titleShow:true};a(document).ready(function(){a.fimg.init()})})(jQuery)

