$(document).ready(function() {
	var currentStep = 1;
	var freeSpins = 1;
	var spinWin = 0;
	var tryIteration = 0;
	musicBg = $(".mus");
	musicClick = $(".click")[0];
	musicFinal = $(".final")[0];
	var baseURL = $('.logo img').attr('src').split('images/logo_1.png')[0];
	var isMobile = /Android|webOS|iPhone|iPad|iPod|pocket|psp|kindle|avantgo|blazer|midori|Tablet|Palm|maemo|plucker|phone|BlackBerry|symbian|IEMobile|mobile|ZuneWP7|Windows Phone|Opera Mini/i.test(navigator.userAgent);
	var isIOS = /iPhone|iPad|iPod/i.test(navigator.userAgent);
	var isAndroid = /Android/i.test(navigator.userAgent);
	var screenSize = $(window).width();
	var screenHeight = $(window).height();
	var calcParam = (13/screenHeight*screenHeight)*2;

	if(isMobile) {
		$('body').addClass('is--Mobile');
		calcParam = (8/screenHeight*screenHeight)*2;
	}
	else {
		$('body').addClass('is--Desk');
	}
	if(isIOS) {
		$('body').addClass('is--iOs');
	}
	else if(isAndroid) {
		$('body').addClass('is--Android');
	}


	// Add sound on click button
	$('.btn').on('click', function() {
		musicClick.currentTime = 0;
		musicClick.volume = .2;
		musicClick.play();
	});

	// Click Btn Play
	$('.btn-play').on('click', function() {
		musicBg[0].volume = .2;
		$(this).parents('.popUp').removeClass('popUp-zoomIn');
		$(".roulette-inner-wrap").addClass("roulette-spin");
		musicBg[0].play();

		setTimeout(function() {
			$('.roulette-wrap').addClass('roulette-zoom');

			setTimeout(function() {
				$('.popUp-2').addClass('popUp-fadeInUp');
			}, 2000)
		}, 5000);
	});
	
	// Change Step
	$('.btn-next').on('click', function() {
		currentStep++;
		$('#step' + (currentStep - 1)).hide().next().fadeIn().css('display', 'flex');
		$(this).parents('.popUp').removeClass('popUp-fadeInUp');

	});
	
	// Click spin slots
	$('.btn-spin').on('click', function() {
		tryIteration++;
		$('.btn-spin').addClass('btn-disabled');
		$('.spin-block').removeClass('active-block');
		$('.win-block').addClass('active-block');
		
		if(tryIteration <= 2 || tryIteration > 3) {
			iterationSlotsFN(tryIteration);

			var iterationInterval = setInterval(function(){
				tryIteration++;
				
				iterationSlotsFN(tryIteration);
				if(tryIteration >= 2 && tryIteration < 3) {
					clearInterval(iterationInterval);

					setTimeout(function () {
						$('.win-block').removeClass('active-block').next().addClass('active-block');
						$('.btn-spin').removeClass('btn-disabled');
					}, 2200);
				}
				else if(tryIteration >= 6) {
					clearInterval(iterationInterval);
				}
			}, 3000);
		}
		else {
			iterationSlotsFN(tryIteration);
		}

		function iterationSlotsFN() {
			var heightsPerColumn = [];

			$('.slots-column').each(function() {
				var $thisColumn = $(this);
				var untilWinElements = $thisColumn.find('.win-' + tryIteration + ':last').prevUntil('.slot-final').addBack();
				var $winElements = $thisColumn.find('.win-' + tryIteration);
				var itemHeights = 0;
				$('.slots-inner-wrap').removeClass('slots-iteration-' + (tryIteration - 1));
	
				// Remove old style
				$thisColumn.find('.win-' + (tryIteration - 1)).remove();
				$thisColumn.find('img').css({
					transition: "top 0s ease",
					top: "0",
				});
	
				$winElements.each(function() {
					itemHeights += $(this).outerHeight(true);
				});
				
				heightsPerColumn.push(itemHeights);
				heightsPerColumn.forEach(function(height, index) {
					setTimeout(function () {
						$winElements.addClass('zoom-out');

						if(tryIteration == 2) {
							// Increase Balance
							changeNumber('increase', 1, 900);
						}
						else if(tryIteration == 3) {
							setTimeout(function () {
								$('.win-block').removeClass('active-block').next().addClass('active-block').find('#win').text('x1');
								$('.btn-spin').removeClass('btn-disabled');
							}, 1500);
						}
						else if(tryIteration == 4) {
							// Increase Balance
							setTimeout(function () {
								changeNumber('increase', 300, 900);
							}, 3200);
						}
						else if(tryIteration == 5) {
							// Increase Balance
							setTimeout(function () {
								changeNumber('increase', 200, 1000);
								
								setTimeout(function () {
									var increaseVal = $('#balance').text();
									$('.gradient-text, .stroke-text').text(increaseVal);
									$('.popUp-3').addClass('popUp-fadeInUp');

									//musicBg[0].pause();
									musicFinal.currentTime = 0;
									musicFinal.volume = .2;
									musicFinal.play();
								}, 3500);
							}, 3200);
						}
					}, 200);
	
					setTimeout(function () {
						$('.slots-inner-wrap').addClass('slots-iteration-' + tryIteration);
						untilWinElements.each(function(index) {
							untilWinElements.css({
								transition: "top 0.5s ease",
								top: (height - calcParam) + "px",
							});
	
							setTimeout(function() {
								$thisColumn.find('.win-' + tryIteration).remove();
								$thisColumn.find('img').css({
									transition: "top 0s ease",
									top: "0",
								});
							}, 400);
						});
					}, 800);
				});
			});
		}
	});

	// Info Content Height 
	contentHeight();
});

// Change Number
function changeNumber(direction, amount, duration) {
	var numberElement = $("#balance");
	var currentValue = parseFloat(numberElement.text().replace(/,/g, ''));
	var newValue;

	if (direction === 'decrease') {
		newValue = currentValue - amount;
	} else if (direction === 'increase') {
		newValue = currentValue + amount;
	}

	var increment = Math.abs(amount) / (duration / 10);
	
	var interval = setInterval(function(){
		if (direction === 'decrease') {
			currentValue -= increment;
			if (currentValue <= newValue) {
				if(currentValue < 0) {
					currentValue = 0;
				}
				else {
					currentValue = newValue/100;
				}
				clearInterval(interval);
			}
		} else if (direction === 'increase') {
			currentValue += increment;
			if (currentValue >= newValue) {
				currentValue = newValue;
				clearInterval(interval);
			}
		}
		numberElement.text(currentValue.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ","));
	}, 10);
}

function contentHeight() {
	var windowHeight = $(window).innerHeight();
	$('body').attr('data-window-height', windowHeight);
}
$(window).on('resize', function(){
	contentHeight();
});