/*
	Picbox v1.0
	(c) 2009 Ben Kay <http://bunnyfire.co.uk>

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

// AUTOLOAD CODE BLOCK (MAY BE CHANGED OR REMOVED)
Picbox.scanPage = function() {
	$$(document.links).filter(function(el) {
		return el.rel && el.rel.test(/^picbox/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);