/*
 * jQuery shuffle
 *
 * Copyright (c) 2008 Ca Phun Ung <caphun at yelotofu dot com>
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://yelotofu.com/labs/jquery/snippets/shuffle/
 *
 * Shuffles an array or the children of a element container.
 * This uses the Fisher-Yates shuffle algorithm <http://jsfromhell.com/array/shuffle [v1.0]>
 */
 
(function($){

	$.fn.shuffle = function() {
		return this.each(function(){
			var items = $(this).children();
			return (items.length) ? $(this).html($.shuffle(items)) : this;
		});
	}
	
	// Takes into consideration the "clear" div, shuffles, and then puts clear div back in
	$.fn.shuffle_props = function() {
		return this.each(function(){
			var items = $(this).children();
			var len = items.length;
			
			// Get the clear divs
			var clears = [];
			clears.push(items[2]);
			clears.push(items[5]);
			clears.push(items[8]);
			clears.push(items[11]);
			clears.push(items[14]);
			
			// Remove the clear divs
			items.splice(2, 1);
			items.splice(4, 1);
			items.splice(6, 1);
			items.splice(8, 1);
			items.splice(10, 1);
			
			// Shuffle the properties
			var shuffled = $.shuffle(items);
			len = shuffled.length;
		
			// Add clear divs after every two properties
			shuffled.splice(2, 0, clears[0]);
			shuffled.splice(5, 0, clears[1]);
			shuffled.splice(8, 0, clears[2]);
			shuffled.splice(11, 0, clears[3]);
			shuffled.splice(14, 0, clears[4]);
			
			return $(this).html(shuffled);
			
		});
	}
	
	$.shuffle = function(arr) {
		for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
		return arr;
	}
	
})(jQuery);