/*
	Picbox v1.0.1
	(c) 2009 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>
	MIT-style license.
*/
Picbox=(function(y){var K=window,p=Browser.Engine.trident4,x,f,L=-1,q,A,J,j,H,F,u,t,s,o,R,m={},w=new Image(),P=new Image(),N,z,g,O,d,M,E,Q,B,h;K.addEvent("domready",function(){y(document.body).adopt($$(N=new Element("div",{id:"pbOverlay",events:{click:I}}).adopt(z=new Element("div",{id:"pbCloseBtn"})),g=new Element("img",{id:"pbImage"}),M=new Element("div",{id:"pbBottomContainer"})).setStyle("display","none"));g.addEvent("dblclick",b);g.ondragstart=function(){return false};bottom=new Element("div",{id:"pbBottom"}).injectInside(M).adopt(E=new Element("div",{id:"pbCaption"}),Q=new Element("div",{id:"pbNumber"}),new Element("div",{id:"pbArrows"}).adopt(O=new Element("a",{id:"pbPrevLink",href:"#",events:{click:G},html:"Previous"}),d=new Element("a",{id:"pbNextLink",href:"#",events:{click:e},html:"Next"})),new Element("div",{styles:{clear:"both"}}));j=p||(N.currentStyle&&(N.currentStyle.position!="fixed"));if(j){$$(N,z,g,M).setStyle("position","absolute");$$(O,d).setStyles({"background-image":"none","text-indent":0})}});function D(){var S=K.getScroll(),T=K.getSize();H=K.getWidth()/2;F=K.getHeight()/2.2;if(j){H=H+S.x;F=F+S.y;N.setStyles({left:S.x,top:S.y,width:T.x,height:T.y})}g.setStyles({top:Math.max(0,F),left:Math.max(0,H),width:1,height:1})}function l(S){["object",p?"select":"embed"].forEach(function(U){Array.forEach(document.getElementsByTagName(U),function(V){if(S){V._picbox=V.style.visibility}V.style.visibility=S?"hidden":V._picbox})});N.style.display=S?"":"none";var T=S?"addEvent":"removeEvent";document[T]("keydown",r);document[T]("mousewheel",k)}function r(T){var S=T.code;return x.closeKeys.contains(S)?I():x.nextKeys.contains(S)?e():x.previousKeys.contains(S)?G():false}function G(){return a(A,true)}function e(){return a(J,true)}function a(S,T){if(S>=0){L=S;q=f[S][0];A=(L||(x.loop?f.length:0))-1;J=((L+1)%f.length)||(x.loop?0:-1);v();N.className="pbLoading";g.setStyle("display","none");E.set("html",f[L][1]||"");Q.set("html",(((f.length>1)&&x.counterText)||"").replace(/{x}/,L+1).replace(/{y}/,f.length));if(A>=0){w.src=f[A][0];O.style.visibility=""}if(J>=0){P.src=f[J][0];d.style.visibility=""}M.setStyle("display","");m=new Image();m.onload=function(){C(T)};m.src=q}return false}function C(S){i();var V=K.getWidth()/1.3,T=K.getHeight()/1.3,U=1;if((m.width>V)||(m.height>T)){U=Math.min(V/m.width,T/m.height)}s=o=U;n(U,S);g.set("src",q);g.setStyle("display","");N.className=""}function n(Z,Y,S){var V=Z/s;u=H-(H-u)*V;t=F-(F-t)*V;s=Z;var T=m.width*Z,aa=m.height*Z,U=u-(T/2),X=t-(aa/2);var W=x.animateResize?Y?"set":"start":"set";h[W]({width:T,height:aa,top:X,left:U}).chain(S);return false}function i(){u=H;t=F}function k(S){return c(S.wheel)}function c(S){var T=s+S*(s/10);return n(T)}function b(){if(s==o&&u==H&&F==F){n(1)}else{i();n(o)}}function v(){m.onload=$empty;m.src=w.src=P.src=q;h.cancel();$$(O,d).setStyle("visibility","hidden")}function I(){if(L>=0){v();L=A=J=-1;n(0,false,function(){$$(g,M).setStyle("display","none")});B.cancel().chain(l).start(0)}return false}Element.implement({picbox:function(S,T){$$(this).picbox(S,T);return this}});Elements.implement({picbox:function(S,V,U){V=V||function(W){return[W.href,W.title]};U=U||function(){return true};var T=this;T.removeEvents("click").addEvent("click",function(){var W=T.filter(U,this);return Picbox.open(W.map(V),W.indexOf(this),S)});return T}});return{open:function(U,T,S){x=$extend({loop:false,overlayOpacity:0.8,overlayFadeDuration:400,animateResize:true,resizeDuration:300,resizeTransition:Fx.Transitions.Sine.easeOut,counterText:"Image {x} of {y}",closeKeys:[27,88,67],previousKeys:[37,80],nextKeys:[39,78]},S||{});B=new Fx.Tween(N,{property:"opacity",duration:x.overlayFadeDuration});h=new Fx.Morph(g,$extend({duration:x.resizeDuration,link:"cancel"},x.resizeTransition?{transition:x.resizeTransition}:{}));R=new Drag(g,{snap:0,onComplete:function(){var V=Browser.Engine.trident5?N:undefined;var W=g.getPosition(V);u=W.x+g.offsetWidth/2;t=W.y+g.offsetHeight/2}}).attach();if(typeof U=="string"){U=[[U,T]];T=0}B.set(0).start(x.overlayOpacity);D();l(1);f=U;x.loop=x.loop&&(f.length>1);return a(T)}}})(document.id);

// AUTOLOAD CODE BLOCK (MAY BE CHANGED OR REMOVED)
Picbox.scanPage = function() {
	$$(document.links).filter(function(el) {
		return el.rel && el.rel.test(/^lightbox/i);
	}).picbox({/* Put custom options here */}, null, function(el) {
		return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
	});
};
window.addEvent("domready", Picbox.scanPage);