(function ($) {
    $.fn.accordion = function (options) {
        var defaults = {
            controller: "h6",
            actual: "div",
            type: "sync",
            open: 1,
            speed: 300
        };
        var options = $.extend(defaults, options);

        return this.each(function () {
            var a = [];
            a[0] = $(this);             //parent object
            a[1] = options.controller;  //control selector
            a[2] = options.actual;      //actual selector
            a[3] = a[0].children(a[1]); //control object
            a[4] = a[0].children(a[2]); //actual object
            a[5] = options.type;        //accordion type
            a[6] = (options.open == "last") ? a[4].length : options.open;        //initial open
            a[7] = options.speed;

            function acMeasure(curObj) {
                curObj.attr("hite", curObj.next(a[2]).height());
            };

            function acClose(curObj) {
                curObj.addClass("acClosed").removeClass("acOpen").next(a[2]).stop().animate({ "height": "0px" }, a[7]);
            }

            function acOpen(curObj) {
                if (a[5] == "sync") {
                    a[3].filter(".acOpen").addClass("acClosed").removeClass("acOpen").next().stop().animate({ "height": "0px" }, a[7])
                }
                curObj.addClass("acOpen").removeClass("acClosed").next(a[2]).animate({ "height": curObj.attr("hite") + "px" }, a[7], function () {
                    $(this).css("height", "auto");
                });
            }

            a[3].each(function () {
                acMeasure($(this));
                $(this).bind("click", function () {
                    if ($(this).hasClass("acOpen")) {
                        acClose($(this));
                    } else {
                        acOpen($(this));
                    }
                });
            });

            a[4].css({ "overflow": "hidden", "height": (a[6] == "all") ? "auto" : "0px" })
            a[3].addClass("acClosed");

            $(window).load(function () {
                a[4].css("height", "auto").each(function () {
                    acMeasure;
                }).css("height", "0px");
                if (a[6] != 0 && a[6] != "all") {
                    a[3].filter(":eq(" + (a[6] - 1) + ")").trigger("click");
                }
            });
        });
    };
})(jQuery);
