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

DO NOT MODIFY THIS FILE! See Wiki for instructions: 
http://wikid.int.thomson.com/index.php/CustomCode/RandomImages
*/

$.fn.randomize = function(params) {
	// Variables
	var randomCookie = false, // Cookie name
	num = 0, // Index
	element = $(this); // Random element
	
	// Merge default and user defined parameters 
	params = $.extend({
		type: 'image', // Default type is "image"
		numberOf: 6, // Default # of images is 1
		altTags: [], // Default alt tags array is empty
		background: false, // Background randomizing off by default
		classPrefix: 'random', // Background randomizing class prefix is "random" by default
		syncImages: false // Synchronize image numerical indexes with other randomized images
	}, params); 
	
	// Make sure ID and src have been provided for image randomization
	if (params.type == 'image') {
		if (!params.id || !params.src) return false;
	}
	
	// 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() * params.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(params.classPrefix + num);
	}
	
	// For randomizing images
	function randomizeImage() {
		var image = new Image(),
		bodyElement = $(document.body),
		syncIndex;
		// randomCooke = randomizeImage and whatever the images name is with out the last numbers and extension
		randomCookie = 'randomizeImage' + params.src.replace(/.*\/([^\d]+)\d+(\.\w+)$/g, '$1');
		// If synchronized images are enabled, load syncIndex
		if (params.syncImages) {
			syncIndex = bodyElement.data('randomImageIndex');
		}
		// If synchronized images are enabled, num = syncIndex
		if (syncIndex) {
			num = syncIndex;
		} else {
			// Get random image number that was not the last image loaded
			num = uniqueRandom(readCookie(randomCookie));
			// If synchronized images are enabled, save syncIndex into body tag data
			if (params.syncImages) bodyElement.data('randomImageIndex', num);
		}
		// Set image src
		image.src = params.src.replace(/\d+(\.\w+)$/g, num + '$1');
		// Add ID
		image.id = params.id;
		// Set image alt if it exists
		if (params.altTags[num - 1]) element.attr('alt', params.altTags[num - 1]);
		// Replace element with image
		element.replaceWith(image);
	}
	
	// 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
		params.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
	switch (params.type) {
		case 'background':
			randomizeBackground();
			break;
		case 'image':
			randomizeImage();
			break;
		case 'html':
			randomizeHTML();
			break;
	}
	
	// Save random number
	if (randomCookie) createCookie(randomCookie, num, 365);
}

