// JavaScript Random Images originally developed by Matt Ditter (April 2009)
// jQueryized/Updated to support unique random numbers by Derrick Gall (May 22, 2009)

$.fn.randomize = function(options) {
	// Variables
	var randomCookie = false, // Cookie name
	num = 0, // Index
	element = $(this); // Random element
	
	// Extension options
	if (!options) options = {}; // Extension options object
	if (!options.numberOf) options.numberOf = 1; // Total number of random images/nodes
	if (!options.altTags) options.altTags = []; // Array of altTags
	if (!options.background) options.background = false; // Enables/Disables random background
	if (!options.classPrefix) options.classPrefix = 'random'; // Background class prefix
	
	// Used to generate a random number that was not used last time
	function uniqueRandom(cookie) {
		// Generate random number not equal to cookie
		do var num = Math.ceil(Math.random() * options.numberOf);
		while (num == cookie)
		return num;
	}
	
	// Used to store cookies
	function createCookie(name, value, days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime() + (days * 24 * 60 * 60 *1000));
			var expires = '; expires=' + date.toGMTString();
		} else expires = '';
		document.cookie = name + '=' + value + expires + '; path=/';
	};
	
	// Used to read cookies
	function readCookie(name) {
		var nameEQ = name + '=',
		ca = document.cookie.split(';');
		for(var i=0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0) == ' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}
	
	// For randomizing backgrounds
	function randomizeBackground() {
		// randomCooke = randomizeBackground and ID if it has one
		randomCookie = 'randomizeBackground';
		if (element.attr('id')) randomCookie += element.attr('id');
		// Get random child number that was not the last index loaded
		num = uniqueRandom(readCookie(randomCookie));
		// Add class to element
		element.addClass(options.classPrefix + num);
	}
	
	// For randomizing images
	function randomizeImage() {
		// randomCooke = randomizeImage and whatever the images name is with out the last numbers and extension
		randomCookie = 'randomizeImage' + element.attr('src').replace(/.*\/([^\d]+)\d+(\.\w+)$/g, '$1');
		// Get random image number that was not the last image loaded
		num = uniqueRandom(readCookie(randomCookie));
		// Set image src
		element.attr('src', element.attr('src').replace(/\d+(\.\w+)$/g, num + '$1'));
		// Set image alt if it exists
		if (options.altTags[num - 1]) element.attr('alt', options.altTags[num - 1]);
	}
	
	// For randomizing divs
	function randomizeHTML() {
		// randomCooke = randomizeParent and ID if it has one
		randomCookie = 'randomizeParent';
		if (element.attr('id')) randomCookie += element.attr('id');
		// Set numberOf = to the number of children
		options.numberOf = element.children().length;
		// Get random child number that was not the last index loaded
		num = uniqueRandom(readCookie(randomCookie));
		// Hide every child that is not at the index of num
		element.children(':eq(' + (num - 1) + ')').siblings().hide();
	}
	
	// Decide what to randomize
	if (options.background) randomizeBackground();
	else if (element.is('img')) randomizeImage();
	else if (element.children()[0]) randomizeHTML();
	
	// Save random number
	if (randomCookie) createCookie(randomCookie, num, 365);
}

