/*
 * jQuery shuffle
 *
 * Copyright (c) 2044 Tolpeit Stefan <www.tolpeit.it>
 */
 
(function($){
	$.fn.shuffle = function(opts) {
	opts = jQuery.extend({
		orderby:'',
		descending:false
	},opts||{});		
		return this.each(function(){
			var items = $(this).children().clone(true);
			return (items.length) ? $(this).html($.shuffle(items, opts)) : this;
		});
	}
	
	$.shuffle = function(arr,opts) {
		for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
		arr.sort(function compare(a, b){
			if(opts.descending)
				return $(a).attr(opts.orderby) < $(b).attr(opts.orderby) ? 1 : -1;
			return $(a).attr(opts.orderby) > $(b).attr(opts.orderby) ? 1 : -1;
			});
		return arr;
	}
})(jQuery);
