/**
 * 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 = /* opt.element ? $obj.children(element) :  */$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;
                };
                
                /*
$content = $('<span />', {
                    class: $class,
                    text: opt.collapsed===true ? $moreTxt : opt.lessTxt
                })
*/
                var $spantxt = opt.collapsed===true ? $moreTxt : opt.lessTxt;
                $('<span class="'+$class+'">' + $spantxt + '</span>')
                .appendTo($obj)
                .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);
                    }
                });
            }
        })
    }
    $.fn.lessMore.defaults = {
        element: null,
        moreTxt : 'więcej',
        moreClass: 'more',
        lessTxt : 'mniej',
        lessClass: 'less',
        limit: 3,
        numbers: true,
        collapsed: true
    }
})(jQuery);
