jQuery.fn.extend({
    captify: function (n) {
        var a = j$.extend({
            speedOver: "fast",
            speedOut: "normal",
            hideDelay: 500,
            animation: "slide",
            prefix: "",
            opacity: "0.45",
            className: "caption-bottom",
            position: "bottom",
            spanWidth: "100%"
        }, n);
        j$(this).each(function () {
            var c = this;
            j$(this).load(function () {
                if (c.hasInit) return false;
                c.hasInit = true;
                var i = false,
                    k = false,
                    e = j$("#" + j$(this).attr("rel")),
                    g = !e.length ? j$(this).attr("alt") : e.html();
                e.remove();
                e = this.parent && this.parent.tagName == "a" ? this.parent : j$(this);
                var h = e.wrap("<div></div>").parent().css({
                    overflow: "hidden",
                    padding: 0,
                    fontSize: 1
                }).addClass("captionWrapper").width(j$(this).width()).height(j$(this).height());
                j$.map(["top", "right", "bottom", "left"], function (f) {
                    h.css("margin-" + f, j$(c).css("margin-" + f));
                    j$.map(["style", "width", "color"], function (j) {
                        j = "border-" + f + "-" + j;
                        h.css(j, j$(c).css(j))
                    })
                });
                j$(c).css({
                    border: "0 none"
                });
                var b = j$("div:last", h.append("<div></div>")).addClass(a.className),
                    d = j$("div:last", h.append("<div></div>")).addClass(a.className).append(a.prefix).append(g);
                j$("*", h).css({
                    margin: 0
                }).show();
                g = jQuery.browser.msie ? "static" : "relative";
                b.css({
                    zIndex: 1,
                    position: g,
                    opacity: a.animation == "fade" ? 0 : a.opacity,
                    width: a.spanWidth
                });
                if (a.position == "bottom") {
                    e = parseInt(b.css("border-top-width").replace("px", "")) + parseInt(d.css("padding-top").replace("px", "")) - 1;
                    d.css("paddingTop", e)
                }
                d.css({
                    position: g,
                    zIndex: 2,
                    background: "none",
                    border: "0 none",
                    opacity: a.animation == "fade" ? 0 : 1,
                    width: a.spanWidth
                });
                b.width(d.outerWidth());
                b.height(d.height());
                g = a.position == "bottom" && jQuery.browser.msie ? -4 : 0;
                var l = a.position == "top" ? {
                    hide: -j$(c).height() - b.outerHeight() - 1,
                    show: -j$(c).height()
                } : {
                    hide: 0,
                    show: -b.outerHeight() + g
                };
                d.css("marginTop", -b.outerHeight());
                b.css("marginTop", l[a.animation == "fade" || a.animation == "always-on" ? "show" : "hide"]);
                var m = function () {
                    if (!i && !k) {
                        var f = a.animation == "fade" ? {
                            opacity: 0
                        } : {
                            marginTop: l.hide
                        };
                        b.animate(f, a.speedOut);
                        a.animation == "fade" && d.animate({
                            opacity: 0
                        }, a.speedOver)
                    }
                };
                if (a.animation != "always-on") {
                    j$(this).hover(function () {
                        k = true;
                        if (!i) {
                            var f = a.animation == "fade" ? {
                                opacity: a.opacity
                            } : {
                                marginTop: l.show
                            };
                            b.animate(f, a.speedOver);
                            a.animation == "fade" && d.animate({
                                opacity: 1
                            }, a.speedOver / 2)
                        }
                    }, function () {
                        k = false;
                        window.setTimeout(m, a.hideDelay)
                    });
                    j$("div", h).hover(function () {
                        i = true
                    }, function () {
                        i = false;
                        window.setTimeout(m, a.hideDelay)
                    })
                }
            });
            if (this.complete || this.naturalWidth > 0) j$(c).trigger("load")
        })
    }
});