{"size": 1979, "ext": "js", "max_stars_repo_path": "js/app.js", "max_stars_repo_name": "deadcoder0904/countdown-clock", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "js/app.js", "max_issues_repo_name": "deadcoder0904/countdown-clock", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "js/app.js", "max_forks_repo_name": "deadcoder0904/countdown-clock", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.6811594203, "max_line_length": 115, "alphanum_fraction": 0.6705406771}
document.addEventListener('DOMContentLoaded',function() {
	const input = document.querySelector('input');
	const links = document.querySelectorAll('.links');
	const alarm = document.querySelector('audio');
	const timer = document.querySelector('#timer');
	const countdown = timer.querySelector('#countdown');
	const returnAt = timer.querySelector('#return');
	let interval;

	function startingCountDown(seconds) {
		displayTimeLeft(seconds);
		interval = setInterval(() => {
			seconds--;
			if(seconds === 5)	alarm.play();
			 if(seconds < 0) {
				clearInterval(interval);
				return;
			}
			displayTimeLeft(seconds);
		},1000);
	}

	function displayTimeLeft(seconds) {
		const hours = Math.floor(seconds / 3600);
		const minutes = Math.floor(seconds / 60);
		const time = `${twoDigit(hours % 24)}:${twoDigit(minutes % 60)}:${twoDigit(seconds % 60)}`;
		countdown.innerHTML = time;
		document.title = time;
	}

	function calcTime(time) {
		let [hours,minutes,seconds] = [Math.round(time / 3600  % 24),Math.round(time / 60  % 60),Math.round(time  % 60)];
		const now = new Date();
		startingCountDown(time);
		seconds = seconds + now.getSeconds();
		minutes = minutes + now.getMinutes() + parseInt(seconds / 60) ;
		hours = hours + now.getHours() + parseInt(minutes / 60);
		return [hours % 24,minutes % 60,seconds % 60];
	}

	function twoDigit(str) {
		let newStr = '00' + str;
		return newStr.slice(-2);
	}

	function handleInput(e) {
		e.preventDefault();
		let time = e.target.dataset.time || e.target.value  *  60;
		const [hours,minutes,seconds] = calcTime(time);
		returnAt.innerHTML = `Return at ${twoDigit(hours)}:${twoDigit(minutes)}:${twoDigit(seconds)}`;
	}

	links.forEach(link => link.addEventListener('click', (e) => {
		clearInterval(interval);
		handleInput(e);
	}));

	input.addEventListener('change', (e) => {
		clearInterval(interval);
		handleInput(e);
	});

	input.addEventListener('keyup', (e) => {
		clearInterval(interval);
		handleInput(e);
	});

});
