let level = 0;
let rainbow = 3;
let playerScore = 0;
let woawLevels = 	[
						{limit: 15, text: 'Bravo', bonus: 1000},
						{limit: 20, text: 'Superbe', bonus: 2000},
						{limit: 25, text: 'Formidable', bonus: 3500},
						{limit: 30, text: 'Magnifique', bonus: 6000},
						{limit: 40, text: 'Extraordinaire', bonus: 10000},
						{limit: 50, text: 'Fantastique', bonus: 25000},
					];

function loadTiles()
{
	const speed = 25;
	let delayOffset = 0;
	let count = 0;
	tiles = [];
	destroyedCells = [];
	$('.tileZone .tile').remove();
	for (let line = 0; line < 9; line++)
	{
		for (let column = 0; column < 13; column++)
		{
			(
				(line, column) => 	{
										let theColor = parseInt(1 + (Math.random() * rainbow));
										const tile = 	$('<img draggable="false" class="tile" data-anim-route="' + (line % 2 == 0 ? '-120' : '1020') + ' -200,' + (column * tileSize) + ' ' + (line * tileSize) + '" src="images/tile_' + theColor + '.png" data-id="' + line + '-' + column + '" data-index="' + count + '">')
															.css 	(
																		{
																			left: line % 2 == 0 ? '-120px' : '1020px',
																			top: '600px',
																		}
																	);
										$('.tileZone')
											.append(tile);
										let newTile = {};
										animateTile(tile, delayOffset++, speed);
										newTile.coords = {left: column, top: line};
										newTile.neighbours = {};
										newTile.color = theColor;
										newTile.tile = tile;
										newTile.index = count++;
										tiles.push(newTile);
									}
			) (line, column);
		}
		if (line % 2 == 0)
		{
			delayOffset = 1;
		}
	}
	organizeTiles();
}

function animateTile(tile, delayOffset, speed)
{
	$(tile)
		.delay(speed * delayOffset)
		.animate(
					{
						left: $(tile).attr('data-anim-route').split(',')[0].split(' ')[0] + 'px',
						top: $(tile).attr('data-anim-route').split(',')[0].split(' ')[1] + 'px',
					},
					speed * 10,
					() => 	{
								const tmp = $(tile).attr("data-anim-route").split(',');
								tmp.splice(0, 1);
								$(tile).attr("data-anim-route", tmp.join(','));
								if ($(tile).attr("data-anim-route").length > 0)
								{
									animateTile(tile, delayOffset, speed);
								}
							}
				);
}




