jQuery.fn.shuffle = (function(){

    function fisherYatesShuffle(arr) {

        // Fisher-Yates shuffle has been proven
        // to be more random than the conventional
        // arr.sort(function(){return Math.random()-.5})
        // [url]http://www.robweir.com/blog/2010/02/microsoft-random-browser-ballot.html[/url]

        var i = arr.length,
        r, tempI, tempR;

        if ( i === 0 ) {
            return arr;
        }

        while ( i-- ) {

            // Generate random index:
            r = Math.floor(Math.random() * (i + 1));

            // Shuffle:
            tempI = arr[i];
            tempR = arr[r];
            arr[i] = tempR;
            arr[r] = tempI;

        }

        return arr;

    }

    return function(s) {

        return this.each(function(){

            var container = jQuery(this);

            // Append shuffled children
            container.append(
                fisherYatesShuffle(container.find(s))
            );

        });

    };

})();

