/*
 Slimbox v1.62 - The ultimate lightweight Lightbox clone
 (c) 2007-2008 Christophe Beyls <http://www.digitalia.be>
 MIT-style license.
 */
var Slimbox;
(function(){
    var G = {}, H = 0, F, M, B, P, E, N, J = new Image(), L = new Image(), W, Z, Q, I, V, Y, K, X, C;
    window.addEvent("domready", function(){
        $(document.body).adopt($$([W = new Element("div", {
            id: "lbOverlay"
        }).addEvent("click", O), Z = new Element("div", {
            id: "lbCenter"
        }), Y = new Element("div", {
            id: "lbBottomContainer"
        })]).setStyle("display", "none"));
        Q = new Element("div", {
            id: "lbImage"
        }).injectInside(Z).adopt(I = new Element("a", {
            id: "lbPrevLink",
            href: "#"
        }).addEvent("click", D), V = new Element("a", {
            id: "lbNextLink",
            href: "#"
        }).addEvent("click", S));
        K = new Element("div", {
            id: "lbBottom"
        }).injectInside(Y).adopt(new Element("a", {
            id: "lbCloseLink",
            href: "#"
        }).addEvent("click", O), X = new Element("div", {
            id: "lbCaption"
        }), C = new Element("div", {
            id: "lbNumber"
        }), new Element("div", {
            styles: {
                clear: "both"
            }
        }));
        E = {
            overlay: new Fx.Tween(W, {
                property: "opacity",
                duration: 500
            }).set(0),
            image: new Fx.Tween(Q, {
                property: "opacity",
                duration: 500,
                onComplete: A
            }),
            bottom: new Fx.Tween(K, {
                property: "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
            }
            M = d;
            a();
            R(true);
            P = window.getScrollTop() + (window.getHeight() / 15);
            E.resize = new Fx.Morph(Z, $extend({
                duration: F.resizeDuration,
                onComplete: A
            }, F.resizeTransition ? {
                transition: F.resizeTransition
            } : {}));
            Z.setStyles({
                top: P,
                width: F.initialWidth,
                height: F.initialHeight,
                marginLeft: -(F.initialWidth / 2),
                display: ""
            });
            E.overlay.start(F.overlayOpacity);
            return T(c)
        }
    };
    Element.implement({
        slimbox: function(b, c){
            $$(this).slimbox(b, c)
        }
    });
    Elements.implement({
        slimbox: function(b, e, d){
            e = e ||
            function(f){
                return [f.href, f.title]
            };
            d = d ||
            function(){
                return true
            };
            var c = this;
            c.removeEvents("click").addEvent("click", function(){
                var f = c.filter(d, this);
                return Slimbox.open(f.map(e), f.indexOf(this), b)
            })
        }
    });
    function a(){
        W.setStyles({
            top: window.getScrollTop(),
            height: window.getHeight()
        })
    }
    function R(b){
        ["object", window.ie ? "select" : "embed"].forEach(function(d){
            Array.forEach(document.getElementsByTagName(d), function(e){
                if (b) {
                    G[e] = e.style.visibility
                }
                e.style.visibility = b ? "hidden" : G[e]
            })
        });
        W.style.display = b ? "" : "none";
        var c = b ? "addEvent" : "removeEvent";
        window[c]("scroll", a)[c]("resize", a);
        document[c]("keydown", U)
    }
    function U(b){
        switch (b.code) {
            case 27:
            case 88:
            case 67:
                O();
                break;
            case 37:
            case 80:
                D();
                break;
            case 39:
            case 78:
                S()
        }
        return false
    }
    function D(){
        return T(B - 1)
    }
    function S(){
        return T(B + 1)
    }
    function T(b){
        if ((H > 1) || (b < 0) || (b >= M.length)) {
            return false
        }
        H = 2;
        B = b;
        $$(I, V, Q, Y).setStyle("display", "none");
        E.bottom.cancel().set(0);
        E.image.set(0);
        Z.className = "lbLoading";
        N = new Image();
        N.onload = A;
        N.src = M[b][0];
        return false
    }
    function A(){
        switch (H++) {
            case 2:
                Z.className = "";
                Q.setStyles({
                    backgroundImage: "url(" + M[B][0] + ")",
                    display: ""
                });
                $$(Q, K).setStyle("width", N.width);
                $$(Q, I, V).setStyle("height", N.height);
                X.set("html", M[B][1] || "");
                C.set("html", (F.showCounter && (M.length > 1)) ? F.counterText.replace(/{x}/, B + 1).replace(/{y}/, M.length) : "");
                if (B) {
                    J.src = M[B - 1][0]
                }
                if (B != (M.length - 1)) {
                    L.src = M[B + 1][0]
                }
                if (Z.clientHeight != Q.offsetHeight) {
                    E.resize.start({
                        height: Q.offsetHeight
                    });
                    break
                }
                H++;
            case 3:
                if (Z.clientWidth != Q.offsetWidth) {
                    E.resize.start({
                        width: Q.offsetWidth,
                        marginLeft: -Q.offsetWidth / 2
                    });
                    break
                }
                H++;
            case 4:
                Y.setStyles({
                    top: P + Z.clientHeight,
                    height: 0,
                    marginLeft: Z.style.marginLeft,
                    display: ""
                });
                E.image.start(1);
                break;
            case 5:
                if (B) {
                    I.style.display = ""
                }
                if (B < (M.length - 1)) {
                    V.style.display = ""
                }
                if (F.animateCaption) {
                    E.bottom.set(-K.offsetHeight).start(0)
                }
                Y.style.height = "";
                H = 1
        }
    }
    function O(){
        if (!H) {
            return false
        }
        H = 0;
        N.onload = $empty;
        for (var b in E) {
            E[b].cancel()
        }
        $$(Z, Y).setStyle("display", "none");
        E.overlay.chain(R).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);
    });
    // PUT YOUR CUSTOM OPTIONS INSIDE THE FOLLOWING {}
    $$(links).slimbox({}, null, function(el){
        return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
    });
};
window.addEvent("domready", Slimbox.scanPage);
