
		var listActiveSquares = [];
		var listEmptySquares = [];
		
		var listActivePics = [];
		
		var unavailableSquares = [];
		
		var unavailableNumbers = [];
	
		$(function(e) {
			if(picsQty > 11) {
				initDamier();
			}
		});
		
		function initDamier() {
			var randomNumber;
			
			while(listEmptySquares.length < 6) {
				randomNumber = mt_rand(1, 18);
				
				if(!in_array(randomNumber, listEmptySquares, true) && !in_array(randomNumber, unavailableSquares, true)) {
					listEmptySquares.push(randomNumber);
					$('#carre'+randomNumber).addClass('empty');
					unavailableSquares.push(randomNumber);
					/*unavailableSquares.push(randomNumber-1);
					unavailableSquares.push(randomNumber+1);
					unavailableSquares.push(randomNumber-6);
					unavailableSquares.push(randomNumber+6);*/
				}
			}
			
			for(var i = 1; i < 19; i++) {
				if(!in_array(i, listEmptySquares)) {
				
					randomNumber = mt_rand(1, picsQty);
					while(in_array(randomNumber, listActivePics, true)) {
						randomNumber = mt_rand(1, picsQty);
					}
					
					$('#carre'+i).attr('currentpic', randomNumber).html('<img src="'+listPics['photo'+randomNumber]+'" />');
					
					listActivePics.push(randomNumber);
					listActiveSquares.push(i);
				}
			}
						
			setInterval(reloadDamier, 3300);
		}
		
		function reloadDamier() {
		
			// find a square to fade in to
			var sqToFadeInto = mt_rand(0, listEmptySquares.length-1);
			
			// find a square to fade out
			var sqToFadeOut = mt_rand(0, listActiveSquares.length-1);
			
			// find an unused pic
			var newPic = mt_rand(1, picsQty);
			while(in_array(newPic, listActivePics, true)) {
				newPic = mt_rand(1, picsQty);
			}
			
			// replace pic with new pic in list of active pics
			var picid = $('#carre'+listActiveSquares[sqToFadeOut]).attr('currentpic');
			var key = array_search(picid, listActivePics);
			listActivePics[key] = newPic;
					
			$('#carre'+listEmptySquares[sqToFadeInto]).removeClass('empty').attr('currentpic', newPic).html('<img class="fadein" style="display: none;" src="'+listPics['photo'+newPic]+'" />');
			$('#carre'+listActiveSquares[sqToFadeOut]).addClass('empty').attr('currentpic', 0).children('img').addClass('fadeout');
			
			$('img.fadeout').removeClass('fadeout').fadeOut(1500, function() {
				$('img.fadein').fadeIn(1500).removeClass('fadein');
			});
			
			// update empty square list
			var tmp = listEmptySquares[sqToFadeInto];
			listEmptySquares[sqToFadeInto] = listActiveSquares[sqToFadeOut];
			listActiveSquares[sqToFadeOut] = tmp;
			
		}
		
		function mt_rand(min, max) {
			// Returns a random number from Mersenne Twister  
			// 
			// version: 905.1001
			// discuss at: http://phpjs.org/functions/mt_rand
			// +   original by: Onno Marsman
			// *     example 1: mt_rand(1, 1);
			// *     returns 1: 1
			var argc = arguments.length;
			if (argc === 0) {
				min = 0;
				max = 2147483647;
			} else if (argc === 1) {
				throw new Error('Warning: mt_rand() expects exactly 2 parameters, 1 given');
			}
			return Math.floor(Math.random() * (max - min + 1)) + min;
		}
		
		function array_search( needle, haystack, argStrict ) {
			// Searches the array for a given value and returns the corresponding key if successful  
			// 
			// version: 905.3122
			// discuss at: http://phpjs.org/functions/array_search
			// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
			// +      input by: Brett Zamir (http://brett-zamir.me)
			// +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
			// *     example 1: array_search('zonneveld', {firstname: 'kevin', middle: 'van', surname: 'zonneveld'});
			// *     returns 1: 'surname'

			var strict = !!argStrict;
			var key = '';

			for(key in haystack){
				if( (strict && haystack[key] === needle) || (!strict && haystack[key] == needle) ){
					return key;
				}
			}

			return false;
		}

		
		function in_array(needle, haystack, argStrict) {
			// Checks if the given value exists in the array  
			// 
			// version: 905.3120
			// discuss at: http://phpjs.org/functions/in_array
			// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
			// +   improved by: vlado houba
			// *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
			// *     returns 1: true
			// *     example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
			// *     returns 2: false
			// *     example 3: in_array(1, ['1', '2', '3']);
			// *     returns 3: true
			// *     example 3: in_array(1, ['1', '2', '3'], false);
			// *     returns 3: true
			// *     example 4: in_array(1, ['1', '2', '3'], true);
			// *     returns 4: false
			var key = '', strict = !!argStrict;

			if (strict) {
				for (key in haystack) {
					if (haystack[key] === needle) {
						return true;
					}
				}
			} else {
				for (key in haystack) {
					if (haystack[key] == needle) {
						return true;
					}
				}
			}

			return false;
		}
	