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