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