/**
 * jquery plugin to show/hide any number of child elements
 *
 * author: Dariusz Pobożniak
 * website: http://pobozniak.pl
 */

(function($) {
    $.fn.lessMore = function(customOptions) {
        var opt = $.extend({}, $.fn.lessMore.defaults, customOptions);
        
        return this.each(function() {
            var $obj = $(this),
                $rows = $obj.children(),
                $quantity = $rows.length - opt.limit,
                $moreTxt = '';

            if ($quantity > 0) {
                $moreTxt = opt.numbers===true ? opt.moreTxt + ' ('+$quantity+')' : opt.moreTxt;
                if (opt.collapsed===true) {
                    $rows.slice(opt.limit).hide();
                    $class = opt.moreClass;
                } else {
                    $class = opt.lessClass;
                };
                
                $('<span />', {
                    'class': $class,
                    text: opt.collapsed===true ? $moreTxt : opt.lessTxt,
                    click: function() {
                        var $link = $(this);
                        if ($link.hasClass(opt.moreClass)) {
                            $rows.slice(opt.limit).slideDown();
                            $link.removeClass().addClass(opt.lessClass).text(opt.lessTxt);
                        } else {
                            $rows.slice(opt.limit).slideUp();
                            $link.removeClass().addClass(opt.moreClass).text($moreTxt);
                        }
                    }
                }).appendTo($obj);
            }
        })
    };
    $.fn.lessMore.defaults = {
        moreTxt : 'więcej',
        moreClass: 'more',
        lessTxt : 'mniej',
        lessClass: 'less',
        limit: 3,
        numbers: true,
        collapsed: true
    }
})(jQuery);
