(function($){
$.fn.extend({ 
hideMaxListItems: function(options) 
{
	// DEFAULT VALUES
	var defaults = {
		max: 10,
        chunk: 10,
		speed: 1000,
		moreHTML:'<p class="maxlist-more"><a href="#">Show More</a></p>',
        lessHTML:'<p class="maxlist-less"><a href="#">Show Less</a></p>'
	};
	var options =  $.extend(defaults, options);
	
	return this.each(function() {
		var op = options;
		var totalListItems = $(this).children("li").length;
		var speedPerLI = 33;
		
		
		if ( (totalListItems > 0) && (totalListItems > op.max) )
		{
			$(this).children("li").each(function(index) {
				if ( (index+1) > op.max ) {
					$(this).hide(0);
					$(this).addClass('maxlist-hidden');
				}
			});
            var newSelectionStart = op.max;
            var newSelectionEnd = parseInt(newSelectionStart) + parseInt(op.chunk);
			$(this).after(op.moreHTML);
			
			$(this).next(".maxlist-more").children("a").click(function()
			{
				var listElements = $(this).parent().parent().children("ul, ol").children("li"); 
				listElements = listElements.slice(newSelectionStart, newSelectionEnd);
				
				var i = 0; 
				(function() { $(listElements[i++] || []).slideToggle(speedPerLI,arguments.callee); })();
				newSelectionStart = newSelectionEnd;
                newSelectionEnd = newSelectionEnd + parseInt(op.chunk);
                $('.maxlist-less').show();
                if( newSelectionEnd > totalListItems ) {
                   newSelectionEnd = totalListItems;
                }
                if( newSelectionStart == totalListItems ) {
                   $('.maxlist-more').hide();
                }
			});
            $(this).after(op.lessHTML);
            $('.maxlist-less').hide();
            $(this).next(".maxlist-less").children("a").click(function()
            {
                    newSelectionEnd = newSelectionStart;
                    newSelectionStart = newSelectionEnd - parseInt(op.chunk);
                    if( newSelectionStart < op.max ) {
                       newSelectionStart = op.max;
                    }
                    var listElements = $(this).parent().parent().children("ul, ol").children("li");
                    listElements = listElements.slice(newSelectionStart, newSelectionEnd);

                    var i = listElements.length - 1;;
                    (function() { $(listElements[i--] || []).slideToggle(speedPerLI,arguments.callee); })();

                    $('.maxlist-more').show();
                    if( newSelectionStart == op.max ) {
                       $('.maxlist-less').hide();
                       newSelectionEnd = newSelectionStart + parseInt(op.chunk);
                    }
                  });
     		}
	});
}
});
})(jQuery); 

