
var counter, count, playbuttonState=0,intervalTriggered=0;
var min , sec;
var mutebuttonState=0;

function launchtimer() {
	
	if (count > 0 && count != NaN && playbuttonState==0){
		
		if(intervalTriggered==0){
			counter=setInterval(timer, 1000);
			intervalTriggered=1;
		}

		document.getElementById('audioSrc').play();
		playbuttonState=1;
		document.getElementById("playButton").src="pause.png";
		return;
	}

	if(playbuttonState==1 ){
	
		document.getElementById("playButton").src="play.png";
		playbuttonState=0;
		document.getElementById('audioSrc').pause();
		return;
	}

}

function timer()
{

	if(playbuttonState==1){
	 	count=count-1;

		// Covert count to min and sec and update display
		min=parseInt(count/60);
		sec=parseInt(count%60);
		document.getElementById("timerDisplay").innerHTML=min+":"+sec;
		

		if (min<10){
			document.getElementById("timerDisplay").innerHTML="0"+min+":"+sec;
		}
		if (sec<10) {
			document.getElementById("timerDisplay").innerHTML=min+":0"+sec;
		}
		if(min<10 && sec<10){
			document.getElementById("timerDisplay").innerHTML="0"+min+":0"+sec;
		} 
		if (min>10 && sec>10) {
			document.getElementById("timerDisplay").innerHTML=min+":"+sec;
		}
	}

	if (count <= 0)
	{
    	clearInterval(counter);
    	document.getElementById('audioSrc').pause();
    	document.getElementById('audioSrc').currentTime=0;
    	document.getElementById("timerDisplay").innerHTML="00:00";
    	playbuttonState=0;
    	document.getElementById("playButton").src="play.png";
    	intervalTriggered=0;
    	window.navigator.vibrate(2000);
    	return;
	}
}

function settingsScreen(){

	count=prompt('Set timer for how many minutes?\n');
	count=count*60;

	min=parseInt(count/60);
	sec=parseInt(count%60);

	if(min<10){
		document.getElementById("timerDisplay").innerHTML="0"+min+":0"+sec;
	} else {
		document.getElementById("timerDisplay").innerHTML=min+":0"+sec;
	}
}

function info(){
	alert("Set the timer and press play.\nUse it with headphones!\n\nIf you like this software please purchase the donate version");
}

function mute(){

	if(mutebuttonState==0){
		document.getElementById('audioSrc').muted=true;
		mutebuttonState=1;
		document.getElementById("muteButton").src="mute2.png";
		return;
	}
	
	if(mutebuttonState==1){
		document.getElementById('audioSrc').muted=false;
		mutebuttonState=0;
		document.getElementById("muteButton").src="mute1.png";
		return;
	}	

}