;(function($) {
    // Truncate an html list to the given number. Add an indicator of how many
    // additional items are available. Allow for a trigger to show/hide additional
    // items.
    $.fn.listTruncate = function(options) {
        var opts = $.extend({}, $.fn.listTruncate.defaults, options);
        
        var $allLists = this;
    
        return this.each(function() {
            var $this = $(this);
            var showMoreButton = "<p class='truncate_list_show_container'><a href='#show_more'>Show More</a></p>";
            
            // Support for the Metadata Plugin.
            var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
            
            var $listItems = $this.find(opts.listItemSelector);
            if ($listItems.length > opts.listMax + 1) {
                // Truncate the list by hiding additional items.
                $listItems.slice(opts.listMax).hide();
                
                // Add the additional items indicator.
                var additionalItemsCount = $listItems.length - opts.listMax;
                var $additionalItemsElement = $(opts.additionalItemsElement)
                                                    .appendTo($this)
                                                    .text("+" + additionalItemsCount + " more");
                
                // Add the show more/less button.
                if ($(opts.showMoreContainerSelector).find(".truncate_list_show_container").length < 1) {
                    var $showMoreButton = $(showMoreButton);
                    var $showMoreLink = $showMoreButton.find("a").click(function(event){
                        event.preventDefault();
                        var $link = $(this);
                        if ($link.attr("href") == "#show_more") {
                            $allLists.each(function(){
                                $(this).find(opts.listItemSelector).show().end()
                                       .find(opts.additionalItemsSelector).hide();
                            });
                            $link.attr("href", "#show_less")
                                    .text("Show Less");
                        } else {
                            $allLists.each(function(){
                                if ($(this).find(opts.listItemSelector).length > opts.listMax + 1) {
                                    $(this).find(opts.listItemSelector).slice(opts.listMax).hide();
                                    $(this).find(opts.additionalItemsSelector).show();
                                }
                            });
                            $link.attr("href", "#show_more")
                                    .text("Show More");
                        }
                    });
                    
                    $(opts.showMoreContainerSelector).append($showMoreButton);
                }
            }
        });
    };

    // default options
    $.fn.listTruncate.defaults = {
        listMax: 3,
        listItemSelector: "dd",
        additionalItemsElement: "<dd class='additional_items ghost'></dd>",
        additionalItemsSelector: "dd.additional_items",
        showMoreContainerSelector: ".sidebar_profile_content"
    };

})(jQuery);
