/*
 *
 * Slimbox - The ultimate lightweight Lightbox clone
 * (c) 2007-2009 Christophe Beyls <http://www.digitalia.be>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

var Slimbox;(
function(){
		var G=0,F,L,B,O,Z,E,M,I=new Image(),K=new Image(),V,Y,P,H,U,X,J,W,C;
		window.addEvent("domready",function(){
				Z=T.bindWithEvent();
				$(document.body).adopt($$([V=new Element("div",{id:"lbOverlay"}),Y=new Element("div",{id:"lbCenter"}),X=new Element("div",{id:"lbBottomContainer"})]).setStyle("display","none"));
				P=new Element("div",{id:"lbImage"}).injectInside(Y).adopt(H=new Element("a",{id:"lbPrevLink",href:"#"}),U=new Element("a",{id:"lbNextLink",href:"#"}));
				H.onclick=D;U.onclick=R;
				var b;J=new Element("div",{id:"lbBottom"}).injectInside(X).adopt(b=new Element("a",{id:"lbCloseLink",href:"#"}),W=new Element("div",{id:"lbCaption"}),C=new Element("div",{id:"lbNumber"}),new Element("div",{styles:{clear:"both"}}));
				b.onclick=V.onclick=N;
				E={overlay:V.effect("opacity",{
							duration:500}).set(0),
								image:P.effect("opacity",{duration:500,onComplete:A}),
								bottom:J.effect("margin-top",{duration:400})}
		});
								Slimbox={
										open:function(d,c,b){
														F=$extend({overlayOpacity:0.8, resizeDuration:400, resizeTransition:false, initialWidth:250, initialHeight:250, animateCaption:true, showCounter:true, counterText:"Image {x} of {y}"},b||{});
								
				if(typeof d=="string"){
					d=[[d,c]];
					c=0}L=d;
					a();
					Q(true);
					O=window.getScrollTop()+(window.getHeight()/15);
					E.resize=Y.effects($extend({duration:F.resizeDuration, onComplete:A}, F.resizeTransition?{transition:F.resizeTransition}:{}));
					Y.setStyles({top:O, width:F.initialWidth, height:F.initialHeight, marginLeft:-(F.initialWidth/2), display:""});
					E.overlay.start(F.overlayOpacity);
					return S(c)}};
					Element.extend({slimbox:function(b,c){$$(this).slimbox(b,c)}});
					Elements.extend({slimbox:function(b,e,d){e=e||function(f){return[f.href,f.title]};
					d=d||function(){return true};
					var c=this;
					c.forEach(function(f){f.onclick=function(){var g=c.filter(d,this);
					return Slimbox.open(g.map(e),g.indexOf(this),b)}})}});
					function a(){V.setStyles({
						top:window.getScrollTop(), 
						height:window.getHeight()
					})
				}
					function Q(b)
						{
						$$("object", window.ie?"select":"embed").forEach(function(d){
							/*
							// 2009/12/10/Gs: ERROR in FF; unsupported propperty..!?
							if(b && d.style.visibility){
								d.slimbox=d.style.visibility
							}
							d.style.visibility=b?"hidden":d.slimbox
							*/
							d.style.visibility=b?"hidden":'visible'
						});

					V.style.display=b?"":"none";
					var c=b?"addEvent":"removeEvent";
					window[c]("scroll",a)[c]("resize",a);
					document[c]("keydown",Z)}function T(b){switch(b.code){case 27:case 88:case 67:N();
					break;
					case 37:
					case 80:
					D();
					break;
					case 39:
					case 78:
					R()}b.preventDefault()}
					function D(){
						return S(B-1)}function R(){return S(B+1)}function S(b){if((G>1)||(b<0)||(b>=L.length)){return false}G=2;B=b;$$(H,U,P,X).setStyle("display","none");
						E.bottom.stop().set(0);
						E.image.set(0);
						Y.className="lbLoading";
						M=new Image();
						M.onload=A;M.src=L[b][0];
						return false
						}
						function A()
						{
							switch(G++)
							{
						case 2:
							Y.className="";
							P.setStyles({backgroundImage:"url("+L[B][0]+")",display:""});
							$$(P,J).setStyle("width",M.width);
							$$(P,H,U).setStyle("height",M.height);
							W.setHTML(L[B][1]||"");
							C.setHTML((F.showCounter&&(L.length>1))?F.counterText.replace(/{x}/,B+1).replace(/{y}/,L.length):"");
							if(B){
								I.src=L[B-1][0]
							}
							if(B!=(L.length-1)){
								K.src=L[B+1][0]
							}
							if(Y.clientHeight!=P.offsetHeight){
								E.resize.start({height:P.offsetHeight});
								break
							}
							G++;
						case 3:
							if(Y.clientWidth!=P.offsetWidth){
								E.resize.start({
									width:P.offsetWidth,
									marginLeft:-P.offsetWidth/2});
								break
							}
							G++;
						case 4:
							X.setStyles({top:O+Y.clientHeight,height:0, marginLeft:Y.style.marginLeft, display:""});
							E.image.start(1);
							break;
						case 5:
							if(B)
							{
							H.style.display=""
							}
							if(B<(L.length-1)){
								U.style.display=""
							}
							if(F.animateCaption)
							{
								E.bottom.set(-J.offsetHeight).start(0)
							}
							X.style.height="";
							G=1
							}
						}
						function N(){
							if(!G){return false
						}
							G=0;M.onload=Class.empty;for(var b in E){E[b].stop()}$$(Y,X).setStyle("display","none");E.overlay.chain(Q).start(0);return false}})();

Slimbox.scanPage = function() {
	var links = $$("a").filter(function(el) {
		return el.rel && el.rel.test(/^lightbox/i);
	});
	// PUT YOUR CUSTOM OPTIONS INSIDE THE FOLLOWING {}
	$$(links).slimbox({}, 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", Slimbox.scanPage);
}
