(function(a) {
    a.fn.piroBox = function(d) {
        function G() {
            var n = a(window).height(), r = a(jQuery('<div class="piro_overlay"></div>').hide().css({ opacity: +d.bg_alpha, height: n + "px" })), g = a(jQuery('<table class="pirobox_content" cellpadding="0" cellspacing="0"></table>')), k = a(jQuery('<div class="caption"></div>').css({ opacity: "0.8", "-moz-border-radius": d.radius + "px", "-khtml-border-radius": d.radius + "px", "-webkit-border-radius": d.radius + "px", "border-radius": d.radius + "px" })); n = a(jQuery('<div class="piro_nav"></div>'));
            var o = a(jQuery('<div class="piro_close"></div>')), i = a(jQuery('<a href="#play" class="play"></a>')), s = a(jQuery('<a href="#stop" class="stop"></a>')), e = a(jQuery('<a href="#prev" class="' + d.pirobox_prev + '"></a>')), f = a(jQuery('<a href="#next" class="' + d.pirobox_next + '"></a>')); a("body").append(r).append(g); g.append('<tr><td colspan="3" class="pirobox_up"></td></tr><tr><td class="t_l"></td><td class="t_c"></td><td class="t_r"></td></tr><tr><td class="c_l"></td><td class="c_c"><span><span></span></span><div></div></td><td class="c_r"></td></tr><tr><td class="b_l"></td><td class="b_c"></td><td class="b_r"></td></tr><tr><td colspan="3" class="pirobox_down"></td></tr>');
            a(".pirobox_up").append(o); a(".pirobox_down").append(n); a(".c_c").append(i); i.hide(); n.append(e).append(f).append(k); if (e.is(".piro_prev_out") || f.is(".piro_next_out")) { a("body").append(e).append(f); e.add(f).hide() } else n.append(e).append(f); var w = e.width(); g.hide(); n = a("a[class^='pirobox_gall']"); for (var C = {}, p = 0; p < n.length; p++) { var H = a(n[p]); C["a." + H.attr("class")] = 0 } var x = []; for (var A in C) { x.push(A); a(A).length === 1 && a(A).css("border", "2px dotted red") } for (p = 0; p < x.length; p++) {
                a(x[p]).each(function(b) {
                    this.rel =
b + 1 + "&nbsp;\u0438\u0437&nbsp;" + a(x[p]).length
                }); a(x[p] + ":first").addClass("first"); a(x[p] + ":last").addClass("last")
            } a(n).each(function(b) { this.rev = b + 0 }); var l = a(n), D = a("a.pirobox"); a.fn.fixPNG = function() {
                return this.each(function() {
                    var b = a(this).css("backgroundImage"); if (b.match(/^url\(["']?(.*\.png)["']?\)$/i)) {
                        b = RegExp.$1; a(this).css({ backgroundImage: "none", filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=" + (a(this).css("backgroundRepeat") == "no-repeat" ? "crop" :
"scale") + ", src='" + b + "')"
                        }).each(function() { var j = a(this).css("position"); j != "absolute" && j != "relative" && a(this).css("position", "relative") })
                    } 
                })
            }; a(window).resize(function() { var b = a(window).height(); r.css({ visibility: "visible", height: +b + "px" }) }); e.add(f).bind("click", function(b) { b.preventDefault(); b = parseInt(a(l).filter(".item").attr("rev")); (a(this).is(".piro_prev_out,.piro_prev") ? a(l).eq(b - 1) : a(l).eq(b + 1)).click(); o.add(k).add(f).add(e).css("visibility", "hidden") }); D.each(function() {
                var b = a(this);
                b.bind("click", function(j) {
                    j.preventDefault(); E(b.attr("href")); j = b.attr("href"); var t = b.attr("title"); t == "" ? k.html("<p>" + j + "<a href=" + j + ' class="link_to" target="_blank" title="\u041e\u0442\u043a\u0440\u044b\u0442\u044c \u0444\u043e\u0442\u043e \u0432 \u043d\u043e\u0432\u043e\u043c \u043e\u043a\u043d\u0435"></a></p>') : k.html("<p>" + t + "<a href=" + j + ' class="link_to" target="_blank" title="\u041e\u0442\u043a\u0440\u044b\u0442\u044c \u0444\u043e\u0442\u043e \u0432 \u043d\u043e\u0432\u043e\u043c \u043e\u043a\u043d\u0435"></a></p>');
                    a(".c_c").addClass("unique"); f.add(e).add(o).add(k).hide(); a(".play").remove()
                })
            }); a(l).each(function() {
                var b = a(this); b.bind("click", function(j) {
                    j.preventDefault(); E(b.attr("href")); j = b.attr("href"); var t = b.attr("title"), h = b.attr("rel"); t == "" ? k.html("<p>" + j + '<em class="number">' + h + "</em><a href=" + j + ' class="link_to" target="_blank" title="\u041e\u0442\u043a\u0440\u044b\u0442\u044c \u0444\u043e\u0442\u043e \u0432 \u043d\u043e\u0432\u043e\u043c \u043e\u043a\u043d\u0435"></a></p>') : k.html("<p>" + t + '<em class="number">' +
h + "</em><a href=" + j + ' class="link_to" target="_blank" title="\u041e\u0442\u043a\u0440\u044b\u0442\u044c \u0444\u043e\u0442\u043e \u0432 \u043d\u043e\u0432\u043e\u043c \u043e\u043a\u043d\u0435"></a></p>'); b.is(".last") ? a(".number").css("text-decoration", "underline") : a(".number").css("text-decoration", "none"); if (b.is(".first")) { e.hide(); f.show() } else f.add(e).show(); if (b.is(".last")) { e.show(); f.hide() } if (b.is(".last") && b.is(".first")) { e.add(f).hide(); a(".number").hide(); i.remove() } a(l).filter(".item").removeClass("item");
                    b.addClass("item"); a(".c_c").removeClass("unique")
                })
            }); var E = function(b) { i.add(s).hide(); o.add(k).add(f).add(e).css("visibility", "hidden"); if (g.is(":visible")) a(".c_c div").children().fadeOut(300, function() { a(".c_c div").children().remove(); F(b) }); else { a(".c_c div").children().remove(); g.show(); r.fadeIn(300, function() { F(b) }) } }, F = function(b) {
                function j() {
                    clearTimeout(u); if (a(l).filter(".item").is(".last")) {
                        a(l).children().removeAttr("class"); f.add(e).css("width", w + "px"); a(".stop").remove(); a(".c_c").append(i);
                        i.hide()
                    } else a(l).children().is("." + d.slideShow) && f.click()
                } function t() { if (a(g).is(":visible")) window.onscroll = function() { var c = a(g).height(); g.css({ marginTop: parseInt(a(this).scrollTop()) - c / 1.9 - 10 }); c = a(window).height(); var m = a(".piro_prev_out").height(); a(".piro_prev_out").add(".piro_next_out").css({ marginTop: parseInt(a(document).scrollTop()) + (c / m - 125) }) } } if (!g.is(".loading")) {
                    g.addClass("loading"); var h = new Image; h.onerror = function() {
                        var c = a(g).height(); g.css({ marginTop: parseInt(a(document).scrollTop()) -
c / 1.9
                        }); a(".c_c div").append('<p class="err_mess">\u041d\u0435 \u043c\u043e\u0433\u0443 \u043d\u0430\u0439\u0442\u0438 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0443:&nbsp;<a href="#close" class="close_pirobox">\u0417\u0430\u043a\u0440\u044b\u0442\u044c</a></p>'); a(".close_pirobox").bind("click", function() { a(".err_mess").remove(); g.add(r).fadeOut(d.close_speed); g.removeClass("loading"); a(".c_c").append(i); return false })
                    }; h.onload = function() {
                        var c = h.height, m = h.width; a(g).height(); var y = a(window).height(),
z = a(window).width(); if (c + 100 > y || m + 100 > z) {
                            var q = m, v = c; z = (m + 250) / z; y = (c + 250) / y; if (y > z) { q = Math.round(m * (1 / y)); v = Math.round(c * (1 / y)) } else { q = Math.round(m * (1 / z)); v = Math.round(c * (1 / z)) } c += v; m += q; a(h).height(v).width(q).hide(); a(".c_c div").animate({ height: v + "px", width: q + "px" }, d.my_speed); g.animate({ height: v + 20 + "px", width: q + 20 + "px", marginLeft: "-" + (q / 2 + 10) + "px", marginTop: parseInt(a(document).scrollTop()) - v / 1.9 - 20 }, d.my_speed, function() {
                                a(".piro_nav,.caption").css({ width: q + "px" }); a(".piro_nav").css("margin-left",
"-" + (q + 5) / 2 + "px"); var B = k.height(); k.css({ bottom: "-" + (B + 5) + "px" }); a(".c_c div").append(h); o.css("display", "block"); f.add(e).add(o).css("visibility", "visible"); k.css({ visibility: "visible", display: "block" }); a(h).show().fadeIn(300); g.removeClass("loading"); d.slideShow == "slideshow" ? i.add(s).show() : i.add(s).hide()
                            })
                        } else {
                            a(h).height(c).width(m).hide(); a(".c_c div").animate({ height: c + "px", width: m + "px" }, d.my_speed); g.animate({ height: c + 20 + "px", width: m + 20 + "px", marginLeft: "-" + (m / 2 + 10) + "px", marginTop: parseInt(a(document).scrollTop()) -
c / 1.9 - 20
                            }, d.my_speed, function() { a(".piro_nav,.caption").css({ width: m + "px" }); a(".piro_nav").css("margin-left", "-" + (m + 5) / 2 + "px"); var B = k.height(); k.css({ bottom: "-" + (B + 5) + "px" }); a(".c_c div").append(h); o.css("display", "block"); f.add(e).add(o).css("visibility", "visible"); k.css({ visibility: "visible", display: "block" }); a(h).fadeIn(300); g.removeClass("loading"); d.slideShow == "slideshow" ? i.add(s).show() : i.add(s).hide() })
                        } 
                    }; h.src = b; b = a(window).height(); var I = a(".piro_prev_out").height(); a(".piro_prev_out").add(".piro_next_out").css({ marginTop: parseInt(a(document).scrollTop()) +
(b / I - 125)
                    }); a(".caption p").css({ "-moz-border-radius": d.radius + "px", "-khtml-border-radius": d.radius + "px", "-webkit-border-radius": d.radius + "px", "border-radius": d.radius + "px" }); s.bind("click", function(c) { c.preventDefault(); clearTimeout(u); a(l).children().removeAttr("class"); a(".stop").remove(); a(".c_c").append(i); f.add(e).css("width", w + "px") }); i.bind("click", function(c) {
                        c.preventDefault(); clearTimeout(u); if (a(h).is(":visible")) { a(l).children().addClass(d.slideShow); a(".play").remove(); a(".c_c").append(s) } f.add(e).css({ width: "0px" });
                        return j()
                    }); a(d.close_all).bind("click", function(c) { clearTimeout(u); if (a(h).is(":visible")) { c.preventDefault(); o.add(r).add(g).add(k).add(f).add(e).fadeOut(d.close_speed); g.removeClass("loading"); a(l).children().removeAttr("class"); f.add(e).css("width", w + "px").hide(); a(".stop").remove(); a(".c_c").append(i); i.hide() } }); var u = setInterval(j, d.slideSpeed * 1E3); a().bind("keydown", function(c) {
                        if (c.keyCode === 27) {
                            c.preventDefault(); if (a(h).is(":visible") || a(".c_c>div>p>a").is(".close_pirobox")) {
                                o.add(r).add(g).add(k).add(f).add(e).fadeOut(d.close_speed);
                                g.removeClass("loading"); clearTimeout(u); a(l).children().removeAttr("class"); a(".stop").remove(); a(".c_c").append(i); f.add(e).css("width", w + "px"); a(l).add(D).children().fadeTo(100, 1)
                            } 
                        } 
                    }).bind("keydown", function(c) {
                        if (!(a(".c_c").is(".unique") || a(".item").is(".first"))) if (a(".c_c").is(".c_c") && c.keyCode === 37) { c.preventDefault(); if (a(h).is(":visible")) { clearTimeout(u); a(l).children().removeAttr("class"); a(".stop").remove(); a(".c_c").append(i); f.add(e).css("width", w + "px"); e.click() } } if (!(a(".c_c").is(".unique") ||
a(".item").is(".last"))) if (a(".c_c").is(".c_c") && c.keyCode === 39) { c.preventDefault(); if (a(h).is(":visible")) { clearTimeout(u); a(l).children().removeAttr("class"); a(".stop").remove(); a(".c_c").append(i); f.add(e).css("width", w + "px"); f.click() } } 
                    }); a.browser.msie6 = a.browser.msie && /MSIE 6\.0/i.test(window.navigator.userAgent); if (a.browser.msie6 && !/MSIE 8\.0/i.test(window.navigator.userAgent)) {
                        a(".t_l,.t_c,.t_r,.c_l,.c_r,.b_l,.b_c,.b_r,a.piro_next, a.piro_prev,a.piro_prev_out,a.piro_next_out,.c_c,.piro_close,a.play,a.stop").fixPNG();
                        b = a(document).height(); r.css("height", b + "px")
                    } if (a.browser.msie) d.close_speed = 0; if (d.scrollImage == true) return t()
                } 
            } 
        } d = jQuery.extend({ my_speed: null, close_speed: 300, bg_alpha: 0.5, scrollImage: null, pirobox_next: "piro_next_out", pirobox_prev: "piro_prev_out", radius: 4, close_all: ".piro_close,.piro_overlay", slideShow: null, slideSpeed: null }, d); G()
    } 
})(jQuery);
