/*
	Picbox v2.1.2
	(c) 2010 Ben Kay <http://bunnyfire.co.uk>

	Based on code from Slimbox v1.7 - The ultimate lightweight Lightbox clone
	(c) 2007-2009 Christophe Beyls <http://www.digitalia.be>
	
	Uses jQuery-mousewheel Version: 3.0.2
	(c) 2009 Brandon Aaron <http://brandonaaron.net>
	
	MIT-style license.
*/

(function(b){function $(){var a={x:j.scrollLeft(),y:j.scrollTop()};q=j.width()/2;r=j.height()/2;if(L){q+=a.x;r+=a.y;b(k).css({left:a.x,top:a.y,width:j.width(),height:j.height()})}b(h).css({top:r,left:q,width:"1px",height:"1px"})}function M(a){e.hideFlash&&b.each(["object","embed"],function(d,f){b(f).each(function(){if(a)this._picbox=this.style.visibility;this.style.visibility=a?"hidden":this._picbox})});k.style.display="";var c=a?"bind":"unbind";b(document)[c]("keydown",aa);b(document)[c]("mousewheel", ba);b(document)[c]("mousemove",D);b(o)[c]("mouseover",function(){N(1)});b(o)[c]("mouseout",N)}function aa(a){a=a.keyCode;return b.inArray(a,e.closeKeys)>=0?O():b.inArray(a,e.nextKeys)>=0?P():b.inArray(a,e.previousKeys)>=0?Q():false}function D(){clearTimeout(E);b(o).fadeIn();E=setTimeout(function(){b(o).fadeOut()},e.controlsFadeDelay)}function N(a){a=1==a?"unbind":"bind";b(document)[a]("mousemove",D);clearTimeout(E)}function Q(){return F(w,true)}function P(){return F(x,true)}function F(a,c){if(a>= 0){s=a;B=l[a][0];w=(s||(e.loop?l.length:0))-1;x=(s+1)%l.length||(e.loop?0:-1);R();k.className="pbLoading";b(h).css("display","none");b(S).html(l[s][1]||"");b(T).html((l.length>1&&e.counterText||"").replace(/{x}/,s+1).replace(/{y}/,l.length));if(w>=0){U.src=l[w][0];b(G).removeClass(y)}if(x>=0){V.src=l[x][0];b(H).removeClass(y)}i=new Image;i.onload=function(){ca(c)};i.src=B}return false}function ca(a){W();var c=j.width()-e.margins,d=j.height()-e.margins,f=1;if(i.width>c||i.height>d){f=Math.min(c/i.width, d/i.height);b(zoomBtn).removeClass(y);I=false}else{b(zoomBtn).addClass(y);I=true}t=J=f;z(f,a);b(h).attr("src",B);b(h).css("display","");k.className=""}function z(a,c){var d=a/t;u=q-(q-u)*d;v=r-(r-v)*d;t=a;d=i.width*a;var f=i.height*a,g=u-d/2>>0,p=v-f/2>>0;c=c?0:e.resizeDuration;a=0==a?function(){b(h).hide()}:function(){};b(h).animate({width:d,height:f,top:p,left:g},{queue:false,duration:c,easing:e.resizeEasing,complete:a});return false}function W(){u=q;v=r}function ba(a,c){b(zoomBtn).addClass(A); return z(t+c*t/10)}function X(){if(t==J&&u==q&&v==r&&!I){b(zoomBtn).addClass(A);return z(1)}else{b(zoomBtn).removeClass(A);W();return z(J)}}function R(){i.onload=function(){};i.src=U.src=V.src=B;b(h).stop();b([G,H]).addClass(y);b(zoomBtn).removeClass(A)}function O(){if(s>=0){R();s=w=x=-1;z(0);M();b(o).stop().hide();b(k).stop().fadeOut()}return false}function K(a){var c=[].slice.call(arguments,1),d=0;a=b.event.fix(a||window.event);a.type="mousewheel";if(a.wheelDelta)d=a.wheelDelta/120;if(a.detail)d= -a.detail/3;c.unshift(a,d);return b.event.handle.apply(this,c)}var j=b(window),e,l,s=-1,B,w,x,da=window.XMLHttpRequest==undefined&&ActiveXObject!=undefined,L,q,r,u,v,t,J,E,I,i={},U=new Image,V=new Image,k,Y,h,G,H,o,S,T,A="pbzoomed",y="pbgreyed";b(document).ready(function(){b(document.body).append(b([k=b('<div id="pbOverlay" />').click(O).append(Y=b('<div id="pbCloseBtn" />')[0])[0],h=b('<img id="pbImage" />').dblclick(X)[0],o=b('<div id="pbBottom" />').append([S=b('<div id="pbCaption" />')[0],b('<div id="pbNav" />').append([G= b('<a id="pbPrevBtn" href="#" />').click(Q)[0],zoomBtn=b('<a id="pbZoomBtn" href="#" />').click(X)[0],H=b('<a id="pbNextBtn" href="#" />').click(P)[0]])[0],T=b('<div id="pbNumber" />')[0]])[0]]).css("display","none"));(L=da||k.currentStyle&&k.currentStyle.position!="fixed")&&b([k,Y,h,o]).css("position","absolute");b(h).tinyDrag(function(){var a=b(h),c=a.position();u=c.left-j.scrollLeft()+a.width()/2;v=c.top-j.scrollTop()+a.height()/2;b(zoomBtn).addClass(A)})});b.picbox=function(a,c,d){e=b.extend({loop:false, overlayOpacity:0.8,overlayFadeDuration:200,resizeDuration:300,resizeEasing:"swing",controlsFadeDelay:2E3,counterText:false,hideFlash:true,closeKeys:[27,88,67],previousKeys:[37,80],nextKeys:[39,78],margins:0},d||{});if(typeof a=="string"){a=[[a,c]];c=0}b(k).css("opacity",0).fadeTo(e.overlayFadeDuration,e.overlayOpacity);b(o).css("display","");D();$();M(1);l=a;e.loop=e.loop&&l.length>1;return F(c)};b.fn.picbox=function(a,c,d){c=c||function(g){return[g.href,g.title]};d=d||function(){return true};var f= this;b(f).unbind("click").click(function(){var g=this,p=[];filteredLinks=b.grep(f,function(m){return d.call(g,m)});for(var n=0;n<filteredLinks.length;n++)p[n]=c(filteredLinks[n]);return b.picbox(p,b.inArray(this,filteredLinks),a)});return f};b.fn.tinyDrag=function(a){return b.tinyDrag(this,a)};b.tinyDrag=function(a,c){function d(m){var Z=m.pageX;m=m.pageY;if(n)a.css({left:p.x+(Z-g.x),top:p.y+(m-g.y)});else if(Math.abs(Z-g.x)>1||Math.abs(m-g.y)>1)n=true;return false}function f(){b(document).unbind("mousemove", d).unbind("mouseup");n&&c&&c()}var g,p,n;a.mousedown(function(m){a.offset();n=false;g={x:m.pageX,y:m.pageY};p={x:parseInt(a.css("left")),y:parseInt(a.css("top"))};b(document).mousemove(d).mouseup(f);return false});return a};var C=["DOMMouseScroll","mousewheel"];b.event.special.mousewheel={setup:function(){if(this.addEventListener)for(var a=C.length;a;)this.addEventListener(C[--a],K,false);else this.onmousewheel=K},teardown:function(){if(this.removeEventListener)for(var a=C.length;a;)this.removeEventListener(C[--a], K,false);else this.onmousewheel=null}};b.fn.extend({mousewheel:function(a){return a?this.bind("mousewheel",a):this.trigger("mousewheel")},unmousewheel:function(a){return this.unbind("mousewheel",a)}})})(jQuery);