// Copyright (c) 2011 Russ Cam
// -------------------------------------------------------
// Dual licensed under the MIT and GPL licenses.
//   - http://www.opensource.org/licenses/mit-license
//   - http://www.opensource.org/licenses/gpl-3.0
(function($){
    $.fn.wrapChildren = function(options) {
 
        options = $.extend({
                              childElem : undefined,
                              groupSize : 1,
                              wrapper : '<div>'
                            }, options || {});
                             
        if (options.childElem === undefined) return this;
 
        return this.each(function() {
            var elems = $(this).children(),
                len = pos = 0;
             
            elems.each(function(i,value) {
                len += $(value).is(options.childElem)? 1 : 0;                    
                if (len > 0 && (len % options.groupSize === 0) || (i === elems.length - 1)) {          
                  elems.slice(pos,i + 1).wrapAll(options.wrapper);
                  len = 0;
                  pos = i + 1;
                }
            });
        }); 
         
    }
})(jQuery);
