﻿(function (jQuery) {
    jQuery.fn.quickAccordian = function (options) {

        var defaults = {
            times: 100,
            single: true,
            firstOpen: false,
            triggerClass: 'header',
            contentClass: 'content'
        };
        var options = jQuery.extend(defaults, options);
        var jQSelector = this.selector;
        return this.each(function () {
            jQuery(this).find('.' + options.triggerClass).click(function () {

                if (options.single) {
                    var current = this;
                    jQuery(this).parents(jQSelector).find('.' + options.triggerClass).each(function () {
                        var header = this;
                        var element = jQuery(this).next('.' + options.contentClass);
                        if (jQuery(element).is(":visible") && this != current) {
                            var height = jQuery(element).height();
                            jQuery(element).slideUp(Math.log(height) * options.times, function () {
                                jQuery(header).removeClass('collapsed');
                            });

                        }
                    });
                }
                if (jQuery(this).next('.' + options.contentClass).is(":visible")) {
                    var header = this;
                    var height = jQuery(this).next('.' + options.contentClass).height();
                    jQuery(this).next('.' + options.contentClass).slideUp(Math.log(height) * options.times / 2, function () {
                        jQuery(header).removeClass('collapsed');
                    });
                } else {
                    var height = jQuery(this).next('.' + options.contentClass).height();
                    jQuery(this).next('.' + options.contentClass).css('visibility', 'visible').slideDown(Math.log(height) * options.times);
                    jQuery(this).addClass('collapsed');
                }
                return false;
            }).next().hide();
            if (options.firstOpen) {
                jQuery(this).find('.' + options.triggerClass + ':first').addClass('collapsed');
                jQuery(this).find('.' + options.triggerClass + ':first').next('.' + options.contentClass).css('visibility', 'visible').show();
            }
        });
    };
})(jQuery);
