﻿(function($) {
    $.fn.listSlider = function(options) {

        var opts = $.extend({}, $.fn.listSlider.defaults, options);
        return this.each(function() {
            var self = $(this);
            var $closedheight = 0;
            var $openheight = $(this).outerHeight(true);
            var elem;
            if ($(this).attr("rel")) {
                elem = $("li:lt(" + $(this).attr("rel") + ")", $(this));
            }else {
                elem = $("li:lt(" + opts.show_first_li_count + ")", $(this))
            }
            elem.each(function() {
                $closedheight += $(this).outerHeight(true);
            })
            $(this).height($closedheight + opts.offset);
            $(this).parent().find(".listSlider-toggle").click(function() {
                if ($(self).hasClass("open")) {
                    $(self).animate({ "height": $closedheight + opts.offset }, 500, "swing");
                    $(this).text(opts.show_more_label);
                } else {
                    $(self).animate({ "height": $openheight + opts.offset }, 500, "swing");
                    $(this).text(opts.show_less_label);
                }
                $(self).toggleClass("open");
                $(this).blur();
                return false;
            });
        });
    }
    $.fn.listSlider.defaults = {
        offset: 0,
        show_first_li_count: 3,
        show_more_label: "+ Visa fler",
        show_less_label: "- Visa färre"
    };
})(jQuery);

