var ACLightbox=new Class({options:{resizeDuration:400,resizeTransition:Fx.Transitions.Sine.easeInOut,fadeDuration:500,startWidth:400,startHeight:300,animateCaption:true,captionDuration:300,showCounter:true},initialize:function(A){this.anchors=[];$each(document.links,function(B){var C=$(B);if(C.rel&&C.rel.test(/^lightbox+/i)){C.addEvent("click",function(D){new Event(D).stop();this.clickImage(C)}.bind(this));this.anchors.push(B)}},this);this.overlay=new Element("div",{id:"lbOverlay"});this.overlay.inject(document.body,"bottom");this.center=new Element("div",{id:"lbCenter",styles:{width:this.options.startWidth,height:this.options.startHeight,marginLeft:-(this.options.startWidth/2),display:"none"}}).injectInside(document.body);this.image=new Element("div",{id:"lbImage"}).injectInside(this.center);this.prevLink=new Element("a",{id:"lbPrevLink",styles:{display:"none"}}).injectInside(this.image);this.nextLink=this.prevLink.clone().setProperty("id","lbNextLink").injectInside(this.image);this.prevLink.addEvent("click",this.previous.bindWithEvent(this));this.nextLink.addEvent("click",this.next.bindWithEvent(this));this.bottomContainer=new Element("div",{id:"lbBottomContainer",styles:{display:"none"}}).injectInside(document.body);this.bottom=new Element("div",{id:"lbBottom"}).injectInside(this.bottomContainer);new Element("a",{id:"lbCloseLink",href:"#"}).injectInside(this.bottom).onclick=this.overlay.onclick=this.closeLightbox.bind(this);this.caption=new Element("div",{id:"lbCaption"}).injectInside(this.bottom);this.number=new Element("div",{id:"lbNumber"}).injectInside(this.bottom);new Element("div",{styles:{clear:"both"}}).injectInside(this.bottom);this.fx={overlay:new Fx.Tween(this.overlay,{property:"opacity",duration:this.options.fadeDuration}).set(0),image:new Fx.Tween(this.image,{property:"opacity",duration:this.options.fadeDuration,onComplete:this.nextEffect.bind(this)}),bottom:new Fx.Tween(this.bottom,{property:"margin-top",duration:this.options.captionDuration,onComplete:this.nextEffect.bind(this)})};this.preloadPrev=new Image();this.preloadNext=new Image()},clickImage:function(C){if(C.getProperty("rel")=="lightbox"){return this.showImage(C.getProperty("href"),C.getProperty("title"))}var D,B,A=[];$each(this.anchors,function(E){if(E.rel==C.getProperty("rel")){for(D=0;D<A.length;D++){if(A[D][0]==E.getProperty("href")){break}}if(D==A.length){A.push([E.getProperty("href"),E.getProperty("title")]);if(E.getProperty("href")==C.getProperty("href")){B=D}}}},this);return this.openImage(A,B)},showImage:function(A,B){return this.openImage([[A,B]],0)},openImage:function(A,B){this.images=A;this.setOverlay();$$('SELECT').setStyle('display','none');this.setupLightbox(true);this.top=window.getScrollTop()+(window.getHeight()/15);this.fx.resize=new Fx.Morph(this.center,$extend({duration:this.options.resizeDuration,onComplete:this.nextEffect.bind(this)},this.options.resizeTransition?{transition:this.options.resizeTransition}:{}));this.center.setStyles({top:this.top,width:this.options.startWidth,height:this.options.startHeight,marginLeft:-(this.options.startWidth/2),display:""});this.fx.overlay.start(0.7);return this.changeImage(B)},setOverlay:function(){this.overlay.setStyles({top:window.getScrollTop(),height:window.getHeight()})},setupLightbox:function(A){if(A){AC.addEvent(window,"scroll",this.setOverlay.bind(this));AC.addEvent(window,"resize",this.setOverlay.bind(this))}else{AC.removeEvent(window,"scroll",this.setOverlay.bind(this));AC.removeEvent(window,"resize",this.setOverlay.bind(this))}this.fxStep=0;document.addEvent("keydown",this.keyDown.bind(this))},previous:function(){return this.changeImage(this.activeImage-1)},next:function(){return this.changeImage(this.activeImage+1)},changeImage:function(A){if(this.fxStep||(A<0)||(A>=this.images.length)){return false}this.fxStep=1;this.activeImage=A;this.bottomContainer.style.display=this.prevLink.style.display=this.nextLink.style.display="none";this.fx.image.set(0);this.center.addClass("lbLoading");this.preload=new Image();this.preload.onload=this.nextEffect.bind(this);this.preload.src=this.images[A][0];return false},nextEffect:function(){switch(this.fxStep++){case 1:this.center.removeClass("lbLoading");this.image.style.backgroundImage="url("+this.images[this.activeImage][0]+")";this.image.style.width=this.bottom.style.width=this.preload.width+"px";this.image.style.height=this.prevLink.style.height=this.nextLink.style.height=this.preload.height+"px";this.caption.set("html",this.images[this.activeImage][1]||"");this.number.set("html",(!this.options.showCounter||(this.images.length==1))?"":"Image "+(this.activeImage+1)+" of "+this.images.length);if(this.activeImage){this.preloadPrev.src=this.images[this.activeImage-1][0]}if(this.activeImage!=(this.images.length-1)){this.preloadNext.src=this.images[this.activeImage+1][0]}if(this.center.clientHeight!=this.image.offsetHeight){this.fx.resize.start({height:this.image.offsetHeight});break}this.fxStep++;case 2:if(this.center.clientWidth!=this.image.offsetWidth){this.fx.resize.start({width:this.image.offsetWidth,marginLeft:-this.image.offsetWidth/2});break}this.fxStep++;case 3:this.bottomContainer.setStyles({top:this.top+this.center.clientHeight,height:0,marginLeft:this.center.style.marginLeft,display:""});this.fx.image.start(1);break;case 4:if(this.options.animateCaption){this.fx.bottom.set(-this.bottom.offsetHeight);this.bottomContainer.style.height="";this.fx.bottom.start(0);break}this.bottomContainer.style.height="";case 5:if(this.activeImage){this.prevLink.style.display=""}if(this.activeImage!=(this.images.length-1)){this.nextLink.style.display=""}this.fxStep=0}},keyDown:function(A){switch(A.code){case 27:case 88:case 67:this.closeLightbox();break;case 37:case 80:this.previous();break;case 39:case 78:this.next()}return false},closeLightbox:function(){if(this.fxStep<0){return }this.fxStep=-1;if(this.preload){this.preload.onLoad=Class.empty;this.preload=null}for(var A in this.fx){this.fx[A].cancel()}this.center.style.display=this.bottomContainer.style.display="none";$$('SELECT').setStyle('display','block');this.fx.overlay.chain(function(){this.setupLightbox(false)}.bind(this)).start(0);return false}});

AC.addEvent(window, 'load', function(){var lghtBx = new ACLightbox();});
