/*
	Picbox v1.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>
	MIT-style license.
*/

Picbox=function(m){function H(){var a=n.getScroll(),b=n.getSize();o=n.getWidth()/2;p=n.getHeight()/2;if(R){o+=a.x;p+=a.y;h.setStyles({left:a.x,top:a.y,width:b.x,height:b.y})}f.setStyles({top:Math.max(0,p),left:Math.max(0,o),width:1,height:1})}function I(a){c.hideFlash&&["object","embed"].forEach(function(d){Array.forEach(document.getElementsByTagName(d),function(e){if(a)e._picbox=e.style.visibility;e.style.visibility=a?"hidden":e._picbox})});h.style.display="";var b=a?"addEvent":"removeEvent";document[b]("keydown", w);document[b]("mousewheel",$);document[b]("mousemove",k)}function w(a){a=a.code;return c.closeKeys.contains(a)?S():c.nextKeys.contains(a)?x():c.previousKeys.contains(a)?y():false}function k(){q([s,z,zoomBtn,A])}function q(a,b){clearTimeout(L);$$(a).fade("in");b&&a.extend(b);L=setTimeout(function(){$$(a).fade("out")},c.controlsFadeDelay)}function J(a){document[1==a?"removeEvent":"addEvent"]("mousemove",k);clearTimeout(L)}function y(){return g(B,true)}function x(){return g(C,true)}function g(a,b){if(a>= 0){l=a;K=i[a][0];B=(l||(c.loop?i.length:0))-1;C=(l+1)%i.length||(c.loop?0:-1);T();h.className="pbLoading";f.setStyle("display","none");i[l][1]?m(M).set("html",i[l][1]).fade("show"):m(M).fade("hide");U.set("html",(i.length>1&&c.counterText||"").replace(/{x}/,l+1).replace(/{y}/,i.length));if(B>=0){V.src=i[B][0];z.removeClass(D)}if(C>=0){W.src=i[C][0];A.removeClass(D)}s.setStyle("display","");j=new Image;j.onload=function(){r(b)};j.src=K}return false}function r(a){X();var b=n.getWidth()-c.margins,d= n.getHeight()-c.margins,e=1;if(j.width>b||j.height>d){e=Math.min(b/j.width,d/j.height);zoomBtn.removeClass(D);N=false}else{zoomBtn.addClass(D);N=true}t=O=e;E(e,a);f.set("src",K);f.setStyle("display","");h.className="";q([s],[z,zoomBtn,A])}function E(a,b){var d=a/t;u=o-(o-u)*d;v=p-(p-v)*d;t=a;d=j.width*a;var e=j.height*a;a=0==a?function(){f.setStyle("display","none")}:$empty;P[b?"set":"start"]({width:d,height:e,top:v-e/2,left:u-d/2}).chain(a);return false}function X(){u=o;v=p}function $(a){zoomBtn.addClass(F); return E(t+a.wheel*(t/10))}function Y(){if(t==O&&Math.abs(u-o+v-p)<2&&!N){zoomBtn.addClass(F);return E(1)}else{zoomBtn.removeClass(F);X();return E(O)}}function T(){j.onload=$empty;j.src=V.src=W.src=K;P.cancel();$$(z,A).addClass(D);zoomBtn.removeClass(F)}function S(){if(l>=0){T();l=B=C=-1;E(0);I();s.setStyle("display","none");Q.cancel().chain(function(){h.setStyle("display","none")}).start(0)}return false}var n=window,aa=Browser.Engine.trident4,R,c,i,l=-1,K,B,C,o,p,u,v,t,O,L,N,j={},V=new Image,W=new Image, h,Z,f,z,A,s,M,U,Q,P,F="pbzoomed",D="pbgreyed";n.addEvent("domready",function(){m(document.body).adopt($$(h=(new Element("div",{id:"pbOverlay",events:{click:S}})).adopt(Z=new Element("div",{id:"pbCloseBtn"})),f=new Element("img",{id:"pbImage",events:{dblclick:Y}}),s=(new Element("div",{id:"pbBottom",events:{mouseover:function(){J(1)},mouseout:J}})).adopt(M=new Element("div",{id:"pbCaption"}),U=new Element("div",{id:"pbNumber"}),(new Element("div",{id:"pbNav"})).adopt(z=new Element("a",{id:"pbPrevBtn", href:"#",events:{click:y}}),zoomBtn=new Element("a",{id:"pbZoomBtn",href:"#",events:{click:Y}}),A=new Element("a",{id:"pbNextBtn",href:"#",events:{click:x}})))).setStyle("display","none"));(R=aa||h.currentStyle&&h.currentStyle.position!="fixed")&&$$(h,Z,f,s).setStyle("position","absolute");f.tinyDrag(function(){var a=f.getPosition(Browser.Engine.trident5?h:undefined);u=a.x+f.offsetWidth/2;v=a.y+f.offsetHeight/2;m(zoomBtn).addClass(F)})});Element.implement({picbox:function(a,b){$$(this).picbox(a,b); return this}});Elements.implement({picbox:function(a,b,d){b=b||function(G){return[G.href,G.title]};d=d||function(){return true};var e=this;e.removeEvents("click").addEvent("click",function(){var G=e.filter(d,this);return Picbox.open(G.map(b),G.indexOf(this),a)});return e}});return{open:function(a,b,d){c=$extend({loop:false,overlayOpacity:0.8,overlayFadeDuration:200,resizeDuration:300,resizeEasing:Fx.Transitions.Sine.easeOut,controlsFadeDelay:3E3,counterText:false,hideFlash:true,closeKeys:[27,88,67], previousKeys:[37,80],nextKeys:[39,78],margins:0},d||{});Q=new Fx.Tween(h,{property:"opacity",duration:c.overlayFadeDuration});P=new Fx.Morph(f,$extend({duration:c.resizeDuration,link:"cancel"},c.resizeTransition?{transition:c.resizeTransition}:{}));if(typeof a=="string"){a=[[a,b]];b=0}Q.set(0).start(c.overlayOpacity);H();I(1);i=a;c.loop=c.loop&&i.length>1;return g(b)}}}(document.id); (function(){Element.implement({tinyDrag:function(m){function H(g){var r=g.page.x;g=g.page.y;if(q)J.setStyles({left:r-w.x,top:g-w.y});else if(x(r-k.x)>1||x(g-k.y)>1)q=true;return false}function I(){y.removeEvent("mousemove",H).removeEvent("mouseup");q&&m&&m()}var w,k,q,J=this,y=document,x=Math.abs;this.addEvent("mousedown",function(g){var r=this.getPosition();q=false;k={x:g.page.x,y:g.page.y};w={x:k.x-r.x,y:k.y-r.y};y.addEvent("mousemove",H).addEvent("mouseup",I);return false});return this}})})(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));
	});
};
if (!/android|iphone|ipod|series60|symbian|windows ce|blackberry/i.test(navigator.userAgent)) {
	window.addEvent("domready", Picbox.scanPage);
}
