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

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