var Clock={};
Clock.hours=0;
Clock.minutes=0;

//set all images
Clock.setImages=function()
{
	document.getElementById('minHand').src='images/MinuteKata.png';
	document.getElementById('hourHand').src='images/tasKata.png';
	document.getElementById('nextImg').src='images/Next.png';
	document.body.style.backgroundColor="#9EEFF2";
	document.getElementById('play').src="images/Play.png";
	document.getElementById('home').src="images/Home.png";
	document.getElementById('clock').src="images/Clock.png";
	document.getElementById('logo').src="images/pratham.png";
	document.getElementById('background').style.display="none";
	document.getElementById('footer').style.display="none";
	Clock.setQuestion();
	Clock.numberSystem();
}
//set Question
Clock.setQuestion=function()
{
	var randomHour,randomMinute,hrotate,hdegree,minutesDegree,mrotate;
	randomHour=Math.floor(Math.random() * Clock.timeArr.length); 
	randomMinute=Math.floor(Math.random() * Clock.timeArr.length); 
	
	Clock.hours =Clock.timeArr[randomHour].hour ;//to get hours in clock
	Clock.minutes =Clock.timeArr[randomMinute].minute ;//to get minutes in clock

	hdegree =Clock.hours * 30;
	if(Clock.minutes>=45)
		hdegree =Clock.hours * 30+30;
	hrotate = "rotate(" + hdegree + "deg)";
	$("#hour").css({"-moz-transform" : hrotate, "-webkit-transform" : hrotate});
	
	minutesDegree =Clock.timeArr[randomMinute].minrotate ;
	mrotate = "rotate(" + minutesDegree + "deg)";
	$("#min").css({"-moz-transform" : mrotate, "-webkit-transform" : mrotate});
}

//number system
Clock.numberSystem=function()
{
	var imgDiv,numberDiv,spanTxt;
	numberDiv=document.getElementById('numSystem');
	for(var i=0;i<=9;i++)
	{
		imgDiv=document.createElement("DIV");
		imgDiv.setAttribute("class","numDiv col-lg-1 col-md-1 col-sm-1 col-xs-1");
		imgDiv.setAttribute("id","numDiv"+i);
		spanTxt=document.createElement("span");
		spanTxt.setAttribute("class","spanClass");
		spanTxt.innerHTML=i;
		$(imgDiv).draggable({helper:"clone",revert:"invalid",zIndex:900});
		imgDiv.appendChild(spanTxt);
		numberDiv.appendChild(imgDiv);
	}
	$('.droppedElement').droppable({drop:Clock.dropNumber});	
}

//onclick of play
Clock.displayData=function()
{
	document.getElementById('background').style.display="block";
	document.getElementById('footer').style.display="block";
	document.getElementById('home').style.visibility="visible";
	document.getElementById('coverPage').style.display="none";
	document.getElementById('submitImg').style.pointerEvents="none";
}

//on click of next
Clock.Next=function()
{
	document.getElementById('playSound').pause();
	clearTimeout(Clock.nextQuestionTimeout);
	document.getElementById("clkDiv").classList.remove("flash");
	$('.droppedElement').text('');
	Clock.setQuestion();
}
//call on number dropped
Clock.dropNumber=function(event,ui)
{
	var draggedEleValue;
	document.getElementById('submitImg').style.pointerEvents="auto";
	draggedEleValue=ui.draggable.text();
	console.log(draggedEleValue);
	$(this).html(draggedEleValue);
}

//onclick of submit
Clock.submit=function()
{
	var randomSound,firstValue,secondValue,thirdValue,fourthValue,hours,minutes;

	randomSound=Math.floor(Math.random() *Clock.soundArray.length);
	firstValue=document.getElementById('number1').innerHTML;
	secondValue=document.getElementById('number2').innerHTML;
	thirdValue=document.getElementById('number3').innerHTML;
	fourthValue=document.getElementById('number4').innerHTML;
	console.log(firstValue,secondValue,thirdValue,fourthValue);
	hours=""+firstValue+secondValue;
	minutes=""+thirdValue+fourthValue;
	console.log(hours,minutes);
	if(hours==Clock.hours && minutes==Clock.minutes)
	{
		document.getElementById("clkDiv").classList.add("flash");
		document.getElementById("clkDiv").classList.remove("shake");
		document.getElementById('playSound').src=Clock.soundArray[randomSound];
		document.getElementById('playSound').play();
		if(Clock.nextQuestionTimeout)
		{
			clearTimeout(Clock.nextQuestionTimeout);
			Clock.nextQuestionTimeout=null;
		}
		Clock.nextQuestionTimeout=setTimeout(function()
		{
			Clock.Next();
		},2000);
	}
	else
	{
		document.getElementById('playSound').src="Sounds/wrong.mp3";
		document.getElementById('playSound').play();
		document.getElementById("clkDiv").classList.add("shake");
	}
}
