(function (c) {
    function T(K, w) {
        function q() {
            b.settings.shuffle && f.sort(function () {
                return 0.5 - Math.random()
            });
            if (b.settings.preloadPanels) {
                U();
                for (var a = 0, d = f.length, g = 0; g < d; g++) c("<img/>").load(function () {
                    a++;
                    if (a == d) {
                        o.find(".preloader").remove();
                        P()
                    }
                }).attr("src", f[g].path)
            } else P()
        }
        function P() {
            p = f.length;
            r = (L - (p - 1) * b.settings.distance) / f.length; //856
            A = L - (p - 1) * (b.settings.closedPanelSize + b.settings.distance);
            var a = c('<div class="panel"></div>').appendTo(o);
            B = (isNaN(parseInt(a.css("borderLeftWidth"))) ? 0 : parseInt(a.css("borderLeftWidth"))) + (isNaN(parseInt(a.css("borderRightWidth"))) ? 0 : parseInt(a.css("borderRightWidth")));
            C = (isNaN(parseInt(a.css("borderTopWidth"))) ? 0 : parseInt(a.css("borderTopWidth"))) + (isNaN(parseInt(a.css("borderBottomWidth"))) ? 0 : parseInt(a.css("borderBottomWidth")));
            a.remove();
            for (a = 0; a < p; a++) V(a);
            b.settings.slideshow && M();
            o.hover(function () {
                b.settings.slideshow && b.settings.stopSlideshowOnHover && D && clearInterval(D)
            }, function () {
                b.settings.closePanelOnMouseOut && W();
                b.settings.slideshow && b.settings.stopSlideshowOnHover && M()
            })
        }
        function V(a) {
            var d = c('<div class="panel"></div>').appendTo(o);
            h.push(d);
            f[a].size = A;
            c("<img/>").load(function () {
                h[a].css("background-image", "url(" + c(this).attr("src") + ")");
                switch (b.settings.alignType) {
                case "leftTop":
                    h[a].css("background-position", "left top");
                    break;
                case "leftCenter":
                    h[a].css("background-position", "left center");
                    break;
                case "leftBottom":
                    h[a].css("background-position", "left bottom");
                    break;
                case "centerTop":
                    h[a].css("background-position", "center top");
                    break;
                case "centerCenter":
                    h[a].css("background-position", "center center");
                    break;
                case "centerBottom":
                    h[a].css("background-position", "center bottom");
                    break;
                case "rightTop":
                    h[a].css("background-position", "right top");
                    break;
                case "rightCenter":
                    h[a].css("background-position", "right center");
                    break;
                case "rightBottom":
                    h[a].css("background-position", "right bottom");
                    break;
                case "default":
                    h[a].css("background-position", "left top")
                }
                if (b.settings.shadow) c("<div class=" + (b.settings.orientation == "horizontal" ? "shadow-horizontal" : "shadow-vertical") + "></div>").appendTo(d);
                var e = b.settings.orientation == "horizontal" ? c(this).attr("width") : c(this).attr("height");
                f[a].size = Math.min(A, e);
                e = {
                    type: "panelLoaded",
                    index: a,
                    data: f[a]
                };
                c.isFunction(b.settings.panelLoaded) && b.settings.panelLoaded.call(this, e)
            }).attr("src", f[a].path);
            if (b.settings.orientation == "horizontal") {
                d.css("left", a * (r + b.settings.distance));
                d.css("height", b.settings.height - C);
                d.css("width", r - B)
            } else if (b.settings.orientation == "vertical") {
                d.css("top", a * (r + b.settings.distance));
                d.css("width", b.settings.width - B);
                d.css("height", r - C)
            }
            d.hover(function () {
                E && clearTimeout(E);
                E = setTimeout(function () {
                    F(a)
                }, b.settings.openPanelDelay);
                var e = {
                    type: "panelMouseOver",
                    index: a,
                    data: f[a]
                };
                c.isFunction(b.settings.panelMouseOver) && b.settings.panelMouseOver.call(this, e)
            }, function () {
                var e = {
                    type: "panelMouseOut",
                    index: a,
                    data: f[a]
                };
                c.isFunction(b.settings.panelMouseOut) && b.settings.panelMouseOut.call(this, e)
            });
            f[a].link && d.css("cursor", "pointer");
            d.click(function () {
                b.settings.openPanelOnClick && F(a);
                f[a].link && window.open(f[a].link, f[a].properties.linkTarget);
                var e = {
                    type: "panelClick",
                    index: a,
                    data: f[a]
                };
                c.isFunction(b.settings.panelClick) && b.settings.panelClick.call(this, e)
            });
            var g = {
                type: "panelCreated",
                index: a,
                data: f[a]
            };
            c.isFunction(b.settings.panelCreated) && b.settings.panelCreated.call(this, g);
            if (a == f.length - 1) {
                g = {
                    type: "allPanelsCreated"
                };
                c.isFunction(b.settings.allPanelsCreated) && b.settings.allPanelsCreated.call(this, g)
            }
        }
        function F(a) {
            if (!(j == a && H == "opened")) {
                H = "opened";
                j != -1 && h[j].stop();
                j = a;
                var d = {
                    type: "openPanel",
                    index: a,
                    data: f[a]
                };
                c.isFunction(b.settings.openPanel) && b.settings.openPanel.call(this, d);
                Q();
                var g = false,
                    e = b.settings.orientation == "horizontal" ? "width" : "height",
                    l = b.settings.orientation == "horizontal" ? "left" : "top",
                    s = b.settings.orientation == "horizontal" ? B : C;
                d = h[j];
                for (var t = Math.min(A, f[j].size), x = (L - (p - 1) * b.settings.distance - t) / (p - 1), y = parseFloat(d.css(e)), G = {}, z = [], u = [], m = [], i = [], N, n = 0; n < p; n++) {
                    u[n] = parseFloat(h[n].css(e));
                    z[n] = (n == j ? Math.min(A, f[a].size) : x) - s;
                    i[n] = parseFloat(h[n].css(l));
                    m[n] = n * (x + b.settings.distance) + (n <= j ? 0 : t - x)
                }
                G[e] = t - s;
                d.stop();
                d.animate(G, {
                    duration: b.settings.slideDuration,
                    complete: function () {
                        if (!g) {
                            g = true;
                            if (f[a].caption) {
                                var k = f[a].caption,
                                    v = f[j].properties,
                                    X = parseInt(v.captionFadeDuration),
                                    I = parseInt(v.captionWidth),
                                    O = parseInt(v.captionHeight),
                                    Y = parseInt(v.captionTop);
                                v = parseInt(v.captionLeft);
                                I = c('<div class="caption"></div>').css({
                                    width: I,
                                    height: O,
                                    left: v,
                                    top: Y,
                                    opacity: 0
                                }).appendTo(h[j]);
                                O = c('<div class="caption-background"></div>').css({
                                    width: "100%",
                                    height: "100%"
                                }).appendTo(I);
                                c("<p></p>").html(k).css({
                                    width: "100%",
                                    height: "100%",
                                    opacity: 1
                                }).appendTo(O);
                                I.animate({
                                    opacity: 1
                                }, X)
                            }
                            k = {
                                type: "animationComplete"
                            };
                            c.isFunction(b.settings.animationComplete) && b.settings.animationComplete.call(this, k)
                        }
                    },
                    step: function (k) {
                        N = (k - y) / (t - s - y);
                        for (k = 0; k < p; k++) {
                            k != j && h[k].css(e, N * (z[k] - u[k]) + u[k]);
                            h[k].css(l, N * (m[k] - i[k]) + i[k])
                        }
                    }
                })
            }
        }
        function W() {
            H = "closed";
            E && clearTimeout(E);
            Q();
            for (var a = false, d = b.settings.orientation == "horizontal" ? "width" : "height", g = b.settings.orientation == "horizontal" ? "left" : "top", e = b.settings.orientation == "horizontal" ? B : C, l = h[j], s = parseFloat(l.css(d)), t = {}, x = [], y = [], G = [], z = [], u, m = 0; m < p; m++) {
                y[m] = parseFloat(h[m].css(d));
                x[m] = r - e;
                z[m] = parseFloat(h[m].css(g));
                G[m] = m * (r + b.settings.distance)
            }
            t[d] = r - e;
            l.stop();

        }
        function R() {
            F(j == f.length - 1 ? 0 : j + 1)
        }
        function S() {
            F(j == 0 ? f.length - 1 : j - 1)
        }
        function U() {
            var a = c('<div class="preloader"></div>').hide().fadeIn(300).appendTo(o),
                d = (b.settings.width - parseInt(a.css("width"))) * 0.5,
                g = (b.settings.height - parseInt(a.css("height"))) * 0.5;
            a.css({
                left: d,
                top: g
            })
        }
        function Q() {
            var a = o.find(".caption");
            a && a.stop().animate({
                opacity: 0
            }, 300, function () {
                a.remove()
            })
        }
        function M() {
            D = setInterval(function () {
                if (b.settings.slideshowDirection == "next") R();
                else b.settings.slideshowDirection == "previous" && S()
            }, b.settings.slideshowDelay)
        }
        this.settings = c.extend({}, c.fn.classicAccordion.defaults, w);
        var o = c(K),
            b = this,
            j = -1,
            f = [],
            h = [],
            D = 0,
            J = ["captionFadeDuration", "captionWidth", "captionHeight", "captionTop", "captionLeft", "linkTarget"],
            L = b.settings.orientation == "horizontal" ? b.settings.width : b.settings.height,
            r, A, H = "closed",
            B = 0,
            C = 0,
            p = 0,
            E;
        (function () {
            o.addClass("accordion").css({
                width: b.settings.width,
                height: b.settings.height
            });
            if (b.settings.xmlSource) {
                o.empty();
                c.ajax({
                    type: "GET",
                    url: b.settings.xmlSource,
                    dataType: c.browser.msie ? "text" : "xml",
                    success: function (a) {
                        var d;
                        if (c.browser.msie) {
                            d = new ActiveXObject("Microsoft.XMLDOM");
                            d.async = false;
                            d.loadXML(a)
                        } else d = a;
                        c(d).find("panel").each(function () {
                            var g = {};
                            g.properties = {};
                            for (var e = 0; e < c(this).children().length; e++) {
                                var l = c(this).children()[e];
                                g[l.nodeName] = c(this).find(l.nodeName).text()
                            }
                            for (e = 0; e < J.length; e++) {
                                l = J[e];
                                var s = c(this).attr(l);
                                g.properties[l] = s || b.settings[l]
                            }
                            f.push(g)
                        });
                        q()
                    }
                })
            } else {
                o.children().each(function (a) {
                    var d = {};
                    d.properties = {};
                    for (var g = 0; g < c(this).children().length; g++) {
                        var e = c(this).children()[g];
                        if (c(e).is("a")) {
                            d.path = c(e).find("img").attr("src");
                            d.link = c(e).attr("href");
                            if (c(e).attr("target")) d.properties.linkTarget = c(e).attr("target")
                        } else if (c(e).is("img")) d.path = c(e).attr("src");
                        else d[c(e).attr("class")] = c(e).html()
                    }
                    for (g = 0; g < J.length; g++) {
                        e = J[g];
                        var l;
                        if (b.settings.panelProperties) if (b.settings.panelProperties[a]) l = b.settings.panelProperties[a][e];
                        d.properties[e] || (d.properties[e] = l || b.settings[e])
                    }
                    f.push(d)
                });
                o.empty();
                q()
            }
        })();
        this.nextPanel = R;
        this.previousSlide = S;
        this.openPanel = F;
        this.startSlideshow = function () {
            M()
        };
        this.stopSlideshow = function () {
            D && clearInterval(D)
        };
        this.getSlideshowState = function () {
            return slideshowState
        };
        this.getCurrentIndex = function () {
            return j
        };
        this.getPanelAt = function (a) {
            return f[a]
        };
        this.getAccordionState = function () {
            return H
        }
    }
    c.fn.classicAccordion = function (K) {
        for (var w = [], q = 0; q < this.length; q++) if (!this[q].accordion) {
            this[q].accordion = new T(this[q], K);
            w.push(this[q].accordion)
        }
        return w.length > 1 ? w : w[0]
    };
    c.fn.classicAccordion.defaults = {
        xmlSource: null,
        width: 500,
        height: 300,
        orientation: "horizontal",
        alignType: "leftTop",
        distance: 0,
        slideshow: false,
        slideshowDelay: 5E3,
        slideshowDirection: "next",
        stopSlideshowOnHover: true,
        slideDuration: 500,
        openPanelOnMouseOver: true,
        closePanelOnMouseOut: true,
        openPanelOnClick: false,
        preloadPanels: false,
        shuffle: false,
        closedPanelSize: 30,
        captionFadeDuration: 500,
        captionWidth: 300,
        captionHeight: 100,
        captionTop: 100,
        captionLeft: 30,
        shadow: true,
        linkTarget: "_blank",
        openPanelDelay: 100,
        panelProperties: null,
        panelMouseOver: null,
        panelMouseOut: null,
        panelClick: null,
        panelLoaded: null,
        panelCreated: null,
        allPanelsCreated: null,
        animationComplete: null,
        openPanel: null
    }
})(jQuery);
