(function (d) {
    function K(A, p) {
        function l() {
            a.settings.shuffle && f.sort(function () {
                return 0.5 - Math.random()
            });
            if (a.settings.preloadPanels) {
                L();
                for (var b = 0, c = f.length, e = 0; e < c; e++) d("<img/>").load(function () {
                    b++;
                    if (b == c) {
                        j.find(".preloader").remove();
                        G()
                    }
                }).attr("src", f[e].path)
            } else G()
        }
        function G() {
            m = (B - (f.length - 1) * a.settings.distance) / f.length;
            C = B - (f.length - 1) * (a.settings.closedPanelSize + a.settings.distance);
            var b = d('<div class="panel"></div>').appendTo(j);
            s = b.outerWidth();
            t = b.outerHeight();
            b.remove();
            for (b = 0; b < f.length; b++) M(b);
            a.settings.slideshow && D();
            j.hover(function () {
                a.settings.slideshow && a.settings.stopSlideshowOnHover && u && clearInterval(u)
            }, function () {
                a.settings.closePanelOnMouseOut && N();
                a.settings.slideshow && a.settings.stopSlideshowOnHover && D()
            })
        }
        function M(b) {
            var c = d('<div class="panel"></div>').css("background-image", "url(" + f[b].path + ")").appendTo(j);
            d("<img/>").load(function () {
                var e = a.settings.orientation == "horizontal" ? d(this).attr("width") : d(this).attr("height");
                f[b].size = Math.min(C, e);
								//f[b].properties.closedPanelSize
            }).attr("src", f[b].path);
            if (a.settings.orientation == "horizontal") {
                c.css("left", b * (m + a.settings.distance));
                c.css("height", a.settings.height - t);
                a.settings.distance > 0 ? c.css("width", m - s) : c.css("width", "100%")
            } else if (a.settings.orientation == "vertical") {
                c.css("top", b * (m + a.settings.distance));
                c.css("width", a.settings.width - s);
                a.settings.distance > 0 ? c.css("height", m - t) : c.css("height", "100%")
            }
            if (a.settings.shadow && a.settings.distance == 0) d("<div class=" + (a.settings.orientation == "horizontal" ? "shadow-horizontal" : "shadow-vertical") + "></div>").appendTo(c).hover(function () {
                a.settings.openPanelOnMouseOver && n(b - 1)
            }, function (e) {
                if (a.settings.orientation == "horizontal" && e.pageX >= c.offset().left && a.settings.openPanelOnMouseOver || a.settings.orientation == "vertical" && e.pageY >= c.offset().top && a.settings.openPanelOnMouseOver) n(b)
            });
            c.hover(function (e) {
                if (a.settings.orientation == "horizontal" && e.pageX >= c.offset().left && a.settings.openPanelOnMouseOver || a.settings.orientation == "vertical" && e.pageY >= c.offset().top && a.settings.openPanelOnMouseOver) n(b);
                e = {
                    type: "panelMouseOver",
                    index: b,
                    data: f[b]
                };
                d.isFunction(a.settings.panelMouseOver) && a.settings.panelMouseOver.call(this, e)
            }, function () {
                var e = {
                    type: "panelMouseOut",
                    index: b,
                    data: f[b]
                };
                d.isFunction(a.settings.panelMouseOut) && a.settings.panelMouseOut.call(this, e)
            });
            f[b].link && c.css("cursor", "pointer");
            c.click(function () {
                a.settings.openPanelOnClick && n(b);
                f[b].link && window.open(f[b].link);
                var e = {
                    type: "panelClick",
                    index: b,
                    data: f[b]
                };
                d.isFunction(a.settings.panelClick) && a.settings.panelClick.call(this, e)
            });
            q.push(c)
        }
        function n(b) {
            if (!(i == b && v == "opened")) {
                v = "opened";
                i = b;
                var c = {
                    type: "openPanel",
                    index: b,
                    data: f[b]
                };
                d.isFunction(a.settings.openPanel) && a.settings.openPanel.call(this, c);
                H();
                var e = false;
                c = Math.min(C, f[b].size);
								
								
								if (f[b].caption) {
										var E = f[b].caption,
												o = f[i].properties,
												O = parseInt(o.captionFadeDuration),
												y = parseInt(o.captionWidth),
												F = parseInt(o.captionHeight),
												P = parseInt(o.captionTop);
										o = parseInt(o.captionLeft);
										y = d('<div class="caption"></div>').css({
												width: y,
												height: F,
												left: o,
												top: P //,
												//opacity: 0
										}).appendTo(q[i]);
										F = d('<div class="caption-background"></div>').css({
												width: "100%",
												height: "100%"
										}).appendTo(y);
										d("<p></p>").html(E).css({
												width: "100%",
												height: "100%",
												opacity: 1
										}).appendTo(F);
										//y.animate({
										//    opacity: 1
										//}, O)
								}
								f[b].caption = '';
								
                for (var g = (B - (f.length - 1) * a.settings.distance - c) / (f.length - 1), h = 0; h < q.length; h++) {
                    var k = q[h],
                        w = h == i ? c : g,
                        x = h * (g + a.settings.distance) + (h <= i ? 0 : c - g),
                        r = {};
                    if (a.settings.orientation == "horizontal") {
                        if (parseInt(k.css("width")) != w && a.settings.distance > 0) r.width = w - s;
                        if (parseInt(k.css("left")) != x) r.left = x
                    } else if (a.settings.orientation == "vertical") {
                        if (parseInt(k.css("height")) != w && a.settings.distance > 0) r.height = w - t;
                        if (parseInt(k.css("top")) != x) r.top = x
                    }
                    k.stop();
                    d.isEmptyObject(r) || k.animate(r, a.settings.slideDuration, function () {
                        if (!e) {
                            e = true;
                            
                            E = {
                                type: "animationComplete"
                            };
                            d.isFunction(a.settings.animationComplete) && a.settings.animationComplete.call(this, E)
                        }
                    })
                }
            }
        }
        function N() {
            v = "closed";
            H();
            for (var b = false, c = 0; c < q.length; c++) {
                var e = q[c],
                    g = c * (m + a.settings.distance),
                    h = {};
                if (a.settings.orientation == "horizontal") {
                    h.left =
                    g;
                    if (a.settings.distance > 0) h.width = m - s
                } else if (a.settings.orientation == "vertical") {
                    h.top = g;
                    if (a.settings.distance > 0) h.height = m - t
                }
                e.stop();
                d.isEmptyObject(h) || e.animate(h, a.settings.slideDuration, function () {
                    if (!b) {
                        b = true;
                        var k = {
                            type: "animationComplete"
                        };
                        d.isFunction(a.settings.animationComplete) && a.settings.animationComplete.call(this, k)
                    }
                })
            }
        }
        function I() {
            n(i == f.length - 1 ? 0 : i + 1)
        }
        function J() {
            n(i == 0 ? f.length - 1 : i - 1)
        }
        function L() {
            var b = d('<div class="preloader"></div>').hide().fadeIn(300).appendTo(j),
                c = (a.settings.width - parseInt(b.css("width"))) * 0.5,
                e = (a.settings.height - parseInt(b.css("height"))) * 0.5;
            b.css({
                left: c,
                top: e
            })
        }
        function H() {
						return;
					
            var b = j.find(".caption");
            b && b.stop().animate({
                opacity: 0
            }, 300, function () {
                b.remove()
            })
        }
        function D() {
						setTimeout(function() { I(); }, 1000);
            u = setInterval(function () {
                if (a.settings.slideshowDirection == "next") I();
                else a.settings.slideshowDirection == "previous" && J()
            }, a.settings.slideshowDelay)
        }
        this.settings = d.extend({}, d.fn.classicAccordion.defaults, p);
        var j = d(A),
            a = this,
            i = -1,
            f = [],
            q = [],
            u = 0,
            z = ["captionFadeDuration", "captionWidth", "captionHeight", "captionTop", "captionLeft", "closedPanelSize"],
            B = a.settings.orientation == "horizontal" ? a.settings.width : a.settings.height,
            m, C, v = "closed",
            s, t;
        (function () {
            j.addClass("accordion").css({
                width: a.settings.width,
                height: a.settings.height
            });
            if (a.settings.xmlSource) {
                j.empty();
                d.ajax({
                    type: "GET",
                    url: a.settings.xmlSource,
                    dataType: d.browser.msie ? "text" : "xml",
                    success: function (b) {
                        var c;
                        if (d.browser.msie) {
                            c = new ActiveXObject("Microsoft.XMLDOM");
                            c.async = false;
                            c.loadXML(b)
                        } else c = b;
                        d(c).find("panel").each(function () {
                            var e = {};
                            e.properties = {};
                            for (var g = 0; g < d(this).children().length; g++) {
                                var h = d(this).children()[g];
                                e[h.nodeName] = d(this).find(h.nodeName).text()
                            }
                            for (g in z) {
                                h = z[g];
                                var k = d(this).attr(h);
                                e.properties[h] = k || a.settings[h]
                            }
                            f.push(e)
                        });
                        l()
                    }
                })
            } else {
                j.children().each(function (b) {
                    var c = {};
                    c.properties = {};
                    for (var e = 0; e < d(this).children().length; e++) {
                        var g = d(this).children()[e];
                        if (d(g).is("a")) {
                            c.path = d(g).find("img").attr("src");
                            c.link = d(g).attr("href")
                        } else if (d(g).is("img")) c.path = d(g).attr("src");
                        else c[d(g).attr("class")] =
                        d(g).html()
                    }
                    for (e in z) {
                        g = z[e];
                        var h;
                        if (a.settings.panelProperties) if (a.settings.panelProperties[b]) h = a.settings.panelProperties[b][g];
                        c.properties[g] = h || a.settings[g]
                    }
                    f.push(c)
                });
                j.empty();
                l()
            }
        })();
        this.nextPanel = I;
        this.previousSlide = J;
        this.openPanel = n;
        this.startSlideshow = function () {
            D()
        };
        this.stopSlideshow = function () {
            u && clearInterval(u);
        };
        this.getSlideshowState = function () {
            return slideshowState
        };
        this.getCurrentIndex = function () {
            return i
        };
        this.getPanelAt = function (b) {
            return f[b]
        };
        this.getAccordionState =

        function () {
            return v
        }
    }
    d.fn.classicAccordion = function (A) {
        for (var p = [], l = 0; l < this.length; l++) if (!this[l].accordion) {
            this[l].accordion = new K(this[l], A);
            p.push(this[l].accordion)
        }
        return p.length > 1 ? p : p[0]
    };
    d.fn.classicAccordion.defaults = {
        xmlSource: null,
        width: 500,
        height: 300,
        orientation: "horizontal",
        distance: 0,
        slideshow: false,
        slideshowDelay: 5E3,
        slideshowDirection: "next",
        stopSlideshowOnHover: true,
        slideDuration: 700,
        openPanelOnMouseOver: true,
        closePanelOnMouseOut: true,
        openPanelOnClick: false,
        preloadPanels: false,
        shuffle: false,
        closedPanelSize: 30,
        captionFadeDuration: 500,
        captionWidth: 300,
        captionHeight: 100,
        captionTop: 100,
        captionLeft: 30,
        shadow: true,
        panelProperties: null,
        panelMouseOver: null,
        panelMouseOut: null,
        panelClick: null,
        animationComplete: null,
        openPanel: null
    }
})(jQuery);
