/*
 * Accordion 1.5 - jQuery menu widget
 *
 * Copyright (c) 2007 Jörn Zaefferer, Frank Marcia
 *
 * http://bassistance.de/jquery-plugins/jquery-plugin-accordion/
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Revision: $Id: jquery.accordion.js 2880 2007-08-24 21:44:37Z joern.zaefferer $
 *
 */
(function ($) {
    $.Accordion = {};
    $.extend($.Accordion, {
        defaults: {
            selectedClass: "selected",
            alwaysOpen: true,
            animated: 'slide',
            event: "click"
        },
        Animations: {
            slide: function (c, a) {
                c = $.extend({
                    easing: "swing",
                    duration: 300
                }, c, a);
                if (!c.toHide.size()) {
                    c.toShow.animate({
                        height: "show"
                    }, {
                        duration: c.duration,
                        easing: c.easing,
                        complete: c.finished
                    });
                    return
                }
                var b = c.toHide.height();
                c.toShow.css({
                    height: 0,
                    overflow: 'hidden'
                }).show();
                c.toHide.filter(":hidden").each(c.finished).end().filter(":visible").animate({
                    height: "hide"
                }, {
                    step: function (n) {
                        c.toShow.height(Math.ceil(b - ($.fn.stop ? n * b : n)))
                    },
                    duration: c.duration,
                    easing: c.easing,
                    complete: c.finished
                })
            },
            bounceslide: function (a) {
                this.slide(a, {
                    easing: a.down ? "bounceout" : "swing",
                    duration: a.down ? 1000 : 200
                })
            },
            easeslide: function (a) {
                this.slide(a, {
                    easing: "easeinout",
                    duration: 700
                })
            }
        }
    });
    $.fn.extend({
        nextUntil: function (b) {
            var a = [];
            this.each(function () {
                for (var i = this.nextSibling; i; i = i.nextSibling) {
                    if (i.nodeType != 1) continue;
                    if ($.filter(b, [i]).r.length) break;
                    a.push(i)
                }
            });
            return this.pushStack(a)
        },
        Accordion: function (j) {
            if (!this.length) return this;
            j = $.extend({}, $.Accordion.defaults, {
                header: $(':first-child', this)[0].tagName
            }, j);
            if (j.navigation) {
                var h = this.find("a").filter(function () {
                    return this.href == location.href
                });
                if (h.length) {
                    if (h.filter(j.header).length) {
                        j.active = h
                    } else {
                        j.active = h.parent().parent().prev();
                        h.addClass("current")
                    }
                }
            }
            var k = this,
                headers = k.find(j.header),
                active = findActive(j.active),
                running = 0;
            if (j.autoheight) {
                var i = 0;
                headers.nextUntil(j.header).each(function () {
                    i = Math.max(i, $(this).height())
                }).height(i)
            }
            //headers.not(active || "").nextUntil(j.header).hide();
            active.addClass(j.selectedClass);

            function findActive(a) {
                return a != undefined ? typeof a == "number" ? headers.eq(a) : headers.not(headers.not(a)) : a === false ? $("<div>") : headers.eq(0)
            }
            function toggle(g, e, d, f, b) {
                var c = function (a) {
                        running = a ? 0 : --running;
                        if (running) return;
                        k.trigger("change", d)
                    };
                running = e.size() == 0 ? g.size() : e.size();
                if (j.animated) {
                    if (!j.alwaysOpen && f) {
                        g.slideToggle(j.animated);
                        c(true)
                    } else {
                        $.Accordion.Animations[j.animated]({
                            toShow: g,
                            toHide: e,
                            finished: c,
                            down: b
                        })
                    }
                } else {
                    if (!j.alwaysOpen && f) {
                        g.toggle()
                    } else {
                        e.hide();
                        g.show()
                    }
                    c(true)
                }
            }
            function clickHandler(a) {
                if (!a.target && !j.alwaysOpen) {
                    active.toggleClass(j.selectedClass);
                    var c = active.nextUntil(j.header);
                    var e = active = $([]);
                    toggle(e, c);
                    return
                }
                var b = $(a.target);
                if (b.parents(j.header).length) while (!b.is(j.header)) b = b.parent();
                var d = b[0] == active[0];
                if (running || (j.alwaysOpen && d) || !b.is(j.header)) return;
                active.toggleClass(j.selectedClass);
                if (!d) {
                    b.addClass(j.selectedClass)
                }
                var e = b.nextUntil(j.header),
                    c = active.nextUntil(j.header),
                    data = [b, active, e, c],
                    down = headers.index(active[0]) > headers.index(b[0]);
                active = d ? $([]) : b;
                toggle(e, c, data, d, down);
                return !e.length
            };

            function activateHandler(a, b) {
                if (arguments.length == 1) return;
                clickHandler({
                    target: findActive(b)[0]
                })
            };
            return k.bind(j.event, clickHandler).bind("activate", activateHandler)
        },
        activate: function (a) {
            return this.trigger('activate', [a])
        }
    })
})(jQuery);
