/*!
	Slimbox v1.64 - The ultimate lightweight Lightbox clone
	(c) 2007-2008 Christophe Beyls <http://www.digitalia.be>
	MIT-style license.
*/

/*!
	Modified on 2008-11-11 (Thomas Pickert)

	Mod Notes

	changes include:
	- a custom AUTOLOAD block at the bottom of this file
	- changes to add and manipulate DOM element 'description'

	usage:
	- attribute rel="lightbox" for a-elements activates lightbox
	- attribute rel="lightbox[identifier]" for a-elements activates lightbox and groups all elements with the same identifier together
	- attribute rel="lightbox[identifier][description]" adds the text in 'description' to the lightbox
*/
var Slimbox;(function(){var g=0,f,l,b,s,t,o,e,m,j=new Image(),k=new Image(),y,B,p,h,x,A,i,z,u,c;window.addEvent("domready",function(){$(document.body).adopt($$([y=new Element("div",{id:"lbOverlay"}).addEvent("click",n),B=new Element("div",{id:"lbCenter"}),A=new Element("div",{id:"lbBottomContainer"})]).setStyle("display","none"));p=new Element("div",{id:"lbImage"}).injectInside(B).adopt(h=new Element("a",{id:"lbPrevLink",href:"#"}).addEvent("click",d),x=new Element("a",{id:"lbNextLink",href:"#"}).addEvent("click",r));i=new Element("div",{id:"lbBottom"}).injectInside(A).adopt(new Element("a",{id:"lbCloseLink",href:"#"}).addEvent("click",n),z=new Element("div",{id:"lbCaption"}),u=new Element("div",{id:"lbDescription"}),c=new Element("div",{id:"lbNumber"}),new Element("div",{styles:{clear:"both"}}));e={overlay:new Fx.Tween(y,{property:"opacity",duration:500}).set(0),image:new Fx.Tween(p,{property:"opacity",duration:500,onComplete:a}),bottom:new Fx.Tween(i,{property:"margin-top",duration:400})}});Slimbox={open:function(F,E,D){f=$extend({loop:false,overlayOpacity:0.8,resizeDuration:400,resizeTransition:false,initialWidth:250,initialHeight:250,animateCaption:true,showCounter:true,counterText:"Image {x} of {y}"},D||{});if(typeof F=="string"){F=[[F,E]];E=0}l=F;f.loop=f.loop&&(l.length>1);C();q(true);o=window.getScrollTop()+(window.getHeight()/15);e.resize=new Fx.Morph(B,$extend({duration:f.resizeDuration,onComplete:a},f.resizeTransition?{transition:f.resizeTransition}:{}));B.setStyles({top:o,width:f.initialWidth,height:f.initialHeight,marginLeft:-(f.initialWidth/2),display:""});e.overlay.start(f.overlayOpacity);g=1;return v(E)}};Element.implement({slimbox:function(D,E){$$(this).slimbox(D,E);return this}});Elements.implement({slimbox:function(D,G,F){G=G||function(H){return[H.href,H.title]};F=F||function(){return true};var E=this;E.removeEvents("click").addEvent("click",function(){var H=E.filter(F,this);return Slimbox.open(H.map(G),H.indexOf(this),D)});return E}});function C(){y.setStyles({top:window.getScrollTop(),height:window.getHeight()})}function q(D){["object",window.ie?"select":"embed"].forEach(function(F){Array.forEach(document.getElementsByTagName(F),function(G){if(D){G._slimbox=G.style.visibility}G.style.visibility=D?"hidden":G._slimbox})});y.style.display=D?"":"none";var E=D?"addEvent":"removeEvent";window[E]("scroll",C)[E]("resize",C);document[E]("keydown",w)}function w(D){switch(D.code){case 27:case 88:case 67:n();break;case 37:case 80:d();break;case 39:case 78:r()}return false}function d(){return v(s)}function r(){return v(t)}function v(D){if((g==1)&&(D>=0)){g=2;b=D;s=((b||!f.loop)?b:l.length)-1;t=b+1;if(t==l.length){t=f.loop?0:-1}$$(h,x,p,A).setStyle("display","none");e.bottom.cancel().set(0);e.image.set(0);B.className="lbLoading";m=new Image();m.onload=a;m.src=l[D][0]}return false}function a(){switch(g++){case 2:B.className="";p.setStyles({backgroundImage:"url("+l[b][0]+")",display:""});$$(p,i).setStyle("width",m.width);$$(p,h,x).setStyle("height",m.height);z.set("html",l[b][1]||"");u.set("html",l[b][2]||"");c.set("html",(f.showCounter&&(l.length>1))?f.counterText.replace(/{x}/,b+1).replace(/{y}/,l.length):"");if(s>=0){j.src=l[s][0]}if(t>=0){k.src=l[t][0]}if(B.clientHeight!=p.offsetHeight){e.resize.start({height:p.offsetHeight});break}g++;case 3:if(B.clientWidth!=p.offsetWidth){e.resize.start({width:p.offsetWidth,marginLeft:-p.offsetWidth/2});break}g++;case 4:A.setStyles({top:o+B.clientHeight,marginLeft:B.style.marginLeft,visibility:"hidden",display:""});e.image.start(1);break;case 5:if(s>=0){h.style.display=""}if(t>=0){x.style.display=""}if(f.animateCaption){e.bottom.set(-i.offsetHeight).start(0)}A.style.visibility="";g=1}}function n(){if(g){g=0;m.onload=$empty;for(var D in e){e[D].cancel()}$$(B,A).setStyle("display","none");e.overlay.chain(q).start(0)}return false}})();

// AUTOLOAD CODE BLOCK (MAY BE CHANGED OR REMOVED)
Slimbox.scanPage = function() {
	var links = $$("a").filter(function(el) {
		return el.rel && el.rel.test(/^lightbox/i);
	});
	$$(links).slimbox(
		{
			counterText: "Bild {x} von {y}"
		},
		function(el) {
			var pattern = new RegExp("^lightbox(?:\\[([^\\]]*)\\]){2,}");
			if (pattern.test(el.rel)) {
				return [el.href, el.title, RegExp.lastParen];
			} else {
				return [el.href, el.title];
			}
		},
		function(el) {
			var pattern = new RegExp("^lightbox\\[[^\\]]*\\]");
			pattern.test(this.rel);
			var thisMatch = RegExp.lastMatch;
			pattern.test(el.rel);
			var elMatch = RegExp.lastMatch;
			return (this == el) || (thisMatch == elMatch);
		}
	);
};
window.addEvent("domready", Slimbox.scanPage);
