;(function($) {
	$.cellbanner = {   
		init: function(settings) {
			this.settings = settings;
			this.settings.totalCells = this.settings.rows * this.settings.columns; 
			this.settings.activeGrids = this.settings.cellCombinations[this.selectGrid(this.settings.cellCombinations.length-1)].slice();
			this.settings.previousGrids = this.settings.activeGrids.slice();
			this.settings.backgroundImageCount = this.settings.backgroundImages.length;
			this.settings.activeImageCount = this.settings.activeImages.length;
			this.settings.currentActiveImage = 0;
			this.settings.previousBackroundImage = null;

			var ul = $('<ul></ul>');
			$(this.settings.containerClass).append(ul); 
			 
			for (var i = 0; i < this.settings.totalCells; i++) {
				var li = $('<li></li>');
				var img = $.cellbanner.getRandomImage('background');
				this.settings.previousBackroundImage = img; //keep track of the last background so that we can ensure the next is different
				for (var a = 0; a < this.settings.activeGrids.length; a++) {
					var html = '&nbsp;'; //default
					if (!this.inArray(i,this.settings.excludedGrids)) {  
						if (i == this.settings.activeGrids[a]) {
							//Grab an active image 
							var img = this.getRandomImage('foreground');
						}  
						var html = $.cellbanner.generateCellHTML(img);
					}
				}  
				ul.append('<li><span>'+html+'</span></li>'); 
			}  
			$(this.settings.containerClass + ' li').css({'width' : this.calculateCellWidth() + '%', 'margin' : '0', 'padding' : '0' }); //reset noscript props
			this.setTimer = setTimeout(function() { $.cellbanner.fadeOutPreviousSet(); }, this.settings.timeBetweenSets);
		},
		getRandomImage: function(type) {
			switch(type) {
				case "background":
					var rndImg = $.cellbanner.selectGrid($.cellbanner.settings.backgroundImageCount-1);
					//If the background image is the same as the last recall the function to generate a different image
					if ($.cellbanner.settings.previousBackroundImage != null) {
						if ($.cellbanner.settings.previousBackroundImage.src == $.cellbanner.settings.backgroundImages[rndImg].src) {
							return $.cellbanner.getRandomImage('background');	
						} else {
							return $.cellbanner.settings.backgroundImages[rndImg];
						}
					} else {
						return $.cellbanner.settings.backgroundImages[rndImg];
					}
				break;
				
				case "foreground":
					return this.getNextActiveImage();
				break;
			}
		},
		fadeOutPreviousSet: function() {  
			if (typeof $.cellbanner.settings.previousGrids !== 'undefined' && $.cellbanner.settings.previousGrids.length > 0) {
				var obj = $($.cellbanner.settings.containerClass + ' li span').eq($.cellbanner.settings.previousGrids[0]);
				obj.fadeOut($.cellbanner.settings.fadeTime, function() { $.cellbanner.fadeInNewImage(obj, 'background'); });
				$.cellbanner.settings.previousGrids.shift();
			} else {
				$.cellbanner.setupPreviousGrids();
				$.cellbanner.fadeToNextSet();
			}
		},
		fadeToNextSet: function() {
			if (typeof $.cellbanner.settings.activeGrids !== 'undefined' && $.cellbanner.settings.activeGrids.length > 0) {
				var obj = $($.cellbanner.settings.containerClass + ' li span').eq($.cellbanner.settings.activeGrids[0]);
				obj.fadeOut($.cellbanner.settings.fadeTime, function() { $.cellbanner.fadeInNewImage(obj, 'foreground'); });
				$.cellbanner.settings.activeGrids.shift();
			} else {
				$.cellbanner.setupActiveGrids();
				
				clearTimeout($.cellbanner.settings.setTimer);
				$.cellbanner.settings.setTimer = setTimeout(function() { $.cellbanner.fadeOutPreviousSet(); }, $.cellbanner.settings.timeBetweenSets);
			}
		},
		setupPreviousGrids: function() {
			$.cellbanner.settings.previousGrids = $.cellbanner.settings.activeGrids.slice(); 	
		},
		setupActiveGrids: function() {
			var rNumber = $.cellbanner.selectGrid($.cellbanner.settings.cellCombinations.length);
			$.cellbanner.settings.activeGrids = $.cellbanner.settings.cellCombinations[rNumber].slice();
		},
		selectGrid: function(maxCount) {
			return Number(Math.floor(Math.random() * maxCount));
		},
		fadeInNewImage: function(obj, type) {
			if (type == 'background') {
				var img = this.getRandomImage(type);
				obj.html('<img src="'+img.src+'" />');
				obj.fadeIn($.cellbanner.settings.fadeTime, function () { $.cellbanner.fadeOutPreviousSet(); });
			} else {
				var img = $.cellbanner.getNextActiveImage();
				var html = $.cellbanner.generateCellHTML(img);
				obj.html(html);
				
				obj.fadeIn($.cellbanner.settings.fadeTime, function () { $.cellbanner.fadeToNextSet(); });
			}
		},
		calculateCellWidth: function() {
			return 100 / this.settings.columns;
		},
		generateCellHTML: function(obj) {
			var html = (obj.href != '') ? '<a href="'+obj.href+'">' : ''; 
				html += '<img src="'+obj.src+'" ';
				html += (obj.alt != '') ? 'alt="'+obj.alt+'" ' : ''; 
				html += ' />';
				html += (obj.href != '') ? '</a>' : '';
				
			return html;
		},
		inArray: function(needle, haystack) {
			var length = haystack.length;
			for(var i = 0; i < length; i++) {
				if(haystack[i] == needle) return true;
			}
			return false;
		},
		getNextActiveImage: function() {
			if ($.cellbanner.settings.currentActiveImage >= $.cellbanner.settings.activeImages.length) {
				$.cellbanner.settings.currentActiveImage = 0;
			} 
			var img = $.cellbanner.settings.activeImages[$.cellbanner.settings.currentActiveImage++];
			return img;
		}
	}
	
	$.fn.extend({
		cellbanner: function(options) {
			this.settings = {
				timeBetweenSets			: 1750,
				fadeTime				: 4500,
				containerClass     		: ".cellbanner",
				rows					: 2,
				columns					: 5,
				excludedGrids			: [0,9],
				cellCombinations		: [ 
										     [1,3,7],
											 [1,5,7],
											 [2,4,8],
											 [2,6,8],
											 [7,3,1],
											 [7,5,1],
											 [8,4,2],
											 [8,6,2],
											 [3,1,7],
											 [5,1,7],
											 [4,2,8],
											 [6,2,8]
										  ], //List of cells images should randomly populate
				activeImages			: [
											  {
												src: 'breastscreen/images/banner/Anna_1562_banner_c_132x132.png',
												href: 'anna-v-story.asp',
												alt: 'Anna V\'s story'
											  },
											  {
												src: 'breastscreen/images/banner/Anne_7449_banner_c_132x132.png',
												href: 'anne-story.asp',
												alt: 'Anne\'s Story'
											  },
											  {
												src: 'breastscreen/images/banner/Anne-Marie_banner_c_132x132.png',
												href: 'anne-marie-story.asp',
												alt: 'Anne Marie\'s story'
											  },
											  {
												src: 'breastscreen/images/banner/Carol_1669_banner_c_132x132.png',
												href: 'carol-a-story.asp',
												alt: 'Carol A\'s story'
											  },
											  {
												src: 'breastscreen/images/banner/Catherine_2164_banner_c_132x132.png',
												href: 'catherine-story.asp',
												alt: 'Catherine\'s story'
											  },
											  {
												src: 'breastscreen/images/banner/Charmaine_7521_banner_c_132x132.png',
												href: 'charmaine-story.asp',
												alt: 'Charmaine\'s story'
											  },
											  {
												src: 'breastscreen/images/banner/Deb_1877_banner_c_132x132.png',
												href: 'deb-z-story.asp',
												alt: 'Deb Z\'s story'
											  },
											  {
												src: 'breastscreen/images/banner/Gloria_6972_banner_c_132x132.png',
												href: 'gloria-story.asp',
												alt: 'Gloria\'s story'
											  },
											  {
												src: 'breastscreen/images/banner/Jo_1829_banner_c_132x132.png',
												href: 'jo-story.asp',
												alt: 'Jo\'s story'
											  },
											  {
												src: 'breastscreen/images/banner/Julie_2425_banner_c_132x132.png',
												href: 'julie-story.asp',
												alt: 'Julie\'s story'
											  },
											  {
												src: 'breastscreen/images/banner/Karen_7094_banner_c_132x132.png',
												href: 'karen-story.asp',
												alt: 'Karen\'s story'
											  },
											  {
												src: 'breastscreen/images/banner/Marian_7176_banner_c_132x132.png',
												href: 'marion-story.asp',
												alt: 'Marion\'s story'
											  },
											  {
												src: 'breastscreen/images/banner/Mercy_6886_banner_c_132x132.png',
												href: 'mercy-story.asp',
												alt: 'Mercy\'s story'
											  },
											  {
												src: 'breastscreen/images/banner/Mimi_7264_banner_c_132x132.png',
												href: 'mimi-story.asp',
												alt: 'Mimi\'s story'
											  },
											  {
												src: 'breastscreen/images/banner/Peta_2826_banner_c_132x132.png',
												href: 'peta-story.asp',
												alt: 'Peta\'s story'
											  },
											  {
												src: 'breastscreen/images/banner/Raima_IMG_2022_banner_c_132x132.png',
												href: 'raima-story.asp',
												alt: 'Raima\'s story'
											  },
											  {
												src: 'breastscreen/images/banner/Sheila_1508_banner_c_132x132.png',
												href: 'sheila-story.asp',
												alt: 'Sheila\'s story'
											  },
											  {
												src: 'breastscreen/images/banner/Shelley_2744_banner_c_132x132.png',
												href: 'shelly-story.asp',
												alt: 'Shelly\'s story'
											  },
											  {
												src: 'breastscreen/images/banner/Vicki_2319_banner_c_132x132.png',
												href: 'vicki-story.asp',
												alt: 'Vicki\'s story'
											  }
										   ],
				backgroundImages		: [
											  {
												src: 'breastscreen/images/square-pink.png',
												href: '',
												alt: ''
											  },
											  {
												src: 'breastscreen/images/square-dark-green.png',
												href: '',
												alt: ''
											  },
											  {
												src: 'breastscreen/images/square-light-purple.png',
												href: '',
												alt: ''
											  },
											  {
												src: 'breastscreen/images/square-light-green.png',
												href: '',
												alt: ''
											  },
											  {
												src: 'breastscreen/images/square-dark-purple.png',
												href: '',
												alt: ''
											  },
										   ]
			}	
			
			if(options){
				$.extend(this.settings, options);
			}	
			$.cellbanner.init(this.settings);
		}
	});	
})(jQuery);	
