function getOS() {
	const userAgent = window.navigator.userAgent,
		platform = window.navigator?.userAgentData?.platform || window.navigator.platform,
		macosPlatforms = ['macOS', 'Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'],
		windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'],
		iosPlatforms = ['iPhone', 'iPad', 'iPod'];
	let os = null;

	if (macosPlatforms.indexOf(platform) !== -1) {
		os = 'macos';
	} else if (iosPlatforms.indexOf(platform) !== -1) {
		os = 'ios';
	} else if (windowsPlatforms.indexOf(platform) !== -1) {
		os = 'windows';
	} else if (/Android/.test(userAgent)) {
		os = 'android';
	} else if (/Linux/.test(platform)) {
		os = 'linux';
	}

	return os;
	//return null;
}

function getArch() {
	const userAgent = window.navigator.userAgent,
		platform = window.navigator?.userAgentData?.platform || window.navigator.platform,
		x8664Platforms = ['macOS', 'Win64'],
		x86Platforms = ['Win32', 'WinCE', 'MacIntel'];
	let arch = null;

	if (x8664Platforms.indexOf(platform) !== -1) {
		arch = 'x86_64';
	} else if (x86Platforms.indexOf(platform) !== -1) {
		arch = 'x86';
	} else if (/aarch64/.test(userAgent)) {
		arch = 'aarch64';
	} else if (/arm/.test(userAgent)) {
		arch = 'arm32';
	} else if (/x86_64/.test(platform) || /x8664/.test(platform) || /amd64/.test(platform)) {
		arch = 'x86_64';
	}

	return arch;
	//return null;
}

function startDownloadTimer(t) {
	if (t < 0) {
		stopDownloadTimer();
		window.location.href = $('#downloadLink').attr('href');
	}
	else {
		$('#downloadCounter').html('&nbsp;' + t);
		timerHandle = window.setTimeout(function() {
			startDownloadTimer(t - 1);
		}, 1000);
	}
}

function stopDownloadTimer() {
	$('#downloadLink').prop('disabled', true);
	$('#downloadCounter').html('');
	$('#downloadCounter').hide();
}

function showAllDownloads() {
	$('#showOthers').hide();
	window.clearTimeout(timerHandle);
	stopDownloadTimer();	
	$('.os-list').each(function() { $(this).show(); });
}

var os = getOS();
var timerHandle;

$('#downloadCounter').hide();
$('input[name=selected]').on('change', function() {
	$('#downloadLink').attr('href', $(this).data('download-url'));
	if(timerHandle) {
		window.clearTimeout(timerHandle);
		stopDownloadTimer();		
	}
});

$('#showOthers').on('click', function() {
	showAllDownloads();
});

if(os == null) {
	$('#showOthers').hide();
	var win = $('.os-windows input[type=radio]:nth(0)');
	if(win.length) {
		win.click();
	}
	else {
		$('input[type=radio]:nth(0)').click();
	}
}
else {
	$('.os-list').each(function() { $(this).hide(); });
	$('.os-' + os).show();
	
	var arch = getArch();
	if(arch == null) {
		// TODO show all for OS, first selected
		$('.os-' + os + ' input[type=radio]:nth(0)').click();
	}
	else {
		/* Select first for known arch */
		if($('#operatingSystems').children().length == 1) {
			showAllDownloads();
			$('.form-check-input:nth(0)').click();	
		}
		else {
			$('.os-' + os + ' .arch-download-' + arch + ':nth(0)').click();
		}
	}
	
	/*$('#downloadCounter').show();
	startDownloadTimer(5);*/
}