//30 july
var Clock={};
var Utils={};
Utils.Path='';
Utils.mobileDeviceFlag=false;

var startTime=0,scoredMarks=0,totalMarks=0,resId=0,level=1;
Clock.finishTime=360;
Clock.mdegree = 0;
Clock.mins = 6.00;
Clock.correctAnsCount=0;
Clock.AttemptedCounter=0;
Clock.playedGameFlag=false;
Clock.restartFlag=false;
Clock.insideGame=false;

$(document).ready(function()
{
	$(".se-pre-con").fadeOut("slow");
	if(navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i))
		Utils.mobileDeviceFlag=true;
	/* if(Utils.mobileDeviceFlag)
		Utils.Path=Android.getMediaPath("TikTok"); */  
	setTimeout(function()
	{
		$(document).prop('title',Clock.hardCodedData['gameTitle']);
		Clock.setImages();
		if(Clock.hardCodedData['languageFont']!=null)
			setFontFamilyForLang("fonts/"+Clock.hardCodedData['languageFont']);		
	},200);
});

setFontFamilyForLang=function(fontFilePath){
  var newStyle = "<style>"+
            "@font-face {  "+
              "font-family: 'CustomFontFamily';"  +
                "src: url('"+fontFilePath+"') format('truetype');"+
                "}  </style>";
    $("head").append(newStyle);
    $("body").css("font-family","CustomFontFamily");            
}


//set all images
Clock.setImages=function()
{
	document.body.style.backgroundImage="url("+Utils.Path+Clock.hardCodedData.coverPageImg+")";
	document.getElementById('minHand').src=Utils.Path+'images/MinuteKata.png';
	document.getElementById('hourHand').src=Utils.Path+'images/tasKata.png';
	document.getElementById('nextImg').src=Utils.Path+'images/Next.png';
	document.getElementById('nextBtnImg').src=Utils.Path+'images/Next.png';
	document.getElementById('stopImg').src=Utils.Path+'images/playImg.png';
	document.getElementById('playAgain').src=Utils.Path+'images/replay.png';
	document.getElementById('credit').src=Utils.Path+'images/credits.png';
	document.getElementById('play').src=Utils.Path+"images/Play.png";
	document.getElementById('sndBtnImg').src=Utils.Path+'images/Sound.png';
	document.getElementById('home').src=Utils.Path+"images/Home.png";
	document.getElementById('clock').src=Utils.Path+"images/Clock.png";
	document.getElementById('logo').src=Utils.Path+"images/pratham.png";
	document.getElementById('questionImg').src=Utils.Path+"images/Digital-clock.png";
	document.getElementById('background').style.display="none";
	document.getElementById('endpage').style.display="none";
	document.getElementById('ansImg').src=Utils.Path+"images/Home-bird_01.png";
	document.getElementById('footer').style.display="none";
}
//to reset
Clock.reset=function()
{
	Clock.finishTime=360;
	Clock.mdegree = 0;
	Clock.mins = 6.00;
	document.getElementById('stopImg').src=Utils.Path+'images/playImg.png';
	document.getElementById('ansImg').src=Utils.Path+"images/Home-bird_01.png";
	mrotate = "rotate(" + Clock.mdegree + "deg)";
	$("#min").css({"-moz-transform" : mrotate, "-webkit-transform" : mrotate});//to set the minute hand on 12 
}

//onclick of play
Clock.displayData=function()
{
	document.getElementById('instructionDiv').style.display="block";
	document.getElementById('instruction').style.display="block";
	document.getElementById('coverPage').style.display="none";
	document.getElementById('nextBtnDiv').style.visibility="visible";
	document.body.style.backgroundImage="url("+Utils.Path+'images/BG.jpg'+")";
	document.getElementById('playInstruction').innerHTML=Clock.hardCodedData['howToPlayMsg'];
	document.getElementById('instruction').innerHTML=Clock.hardCodedData['instruction'];
	document.getElementById('home').style.visibility="visible";
	Clock.playInstruction();
}
Clock.startGame=function()
{
	document.getElementById('playInstr').pause();
	Clock.insideGame=true;
	document.getElementById('instructionDiv').style.display="none";
	document.getElementById('background').style.display="block";
	document.getElementById('footer').style.display="block";
	document.getElementById('question').style.visibility="visible";
	document.getElementById('coverPage').style.display="none";
	Clock.setQuestion();
}
//set Question
Clock.setQuestion=function()
{
	var random,randomHour,Minute,hours,hdegree,hrotate,minute,d;
	d=new Date();
	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
	random=Math.floor((Math.random() * 11) + 1);
	randomHour=Math.floor(Math.random() * Clock.timeArr.length);
	Clock.questionId=Clock.timeArr[randomHour].questionId;
	Minute=random*5;
	Clock.index=Minute/5;
	Clock.hours =Clock.timeArr[randomHour].hour ;
	Clock.hdegree =Clock.hours * 30; //+ (Clock.mins / 2);
	console.log(Clock.hdegree);
	hrotate = "rotate(" + Clock.hdegree + "deg)";
	$("#hour").css({"-moz-transform" : hrotate, "-webkit-transform" : hrotate});
	if(Minute==5)    //to calculate minutes in question
		document.getElementById('question').innerHTML=Clock.timeArr[randomHour].hour+":0"+Minute;
	else
		document.getElementById('question').innerHTML=Clock.timeArr[randomHour].hour+":"+Minute;
}

Clock.playInstruction=function(){
	$('#playInstr').prop('src','Sounds/Marathi.mp3');
	document.getElementById('playInstr').play();
 }

//onclick of button to play game
Clock.start=function()
{	
	var mrotate,hrotate;
	document.getElementById('playSound').src=Utils.Path+"Sounds/Clock_tik_tik.mp3";
	document.getElementById('playSound').play();
	document.getElementById('stopImg').src=Utils.Path+'images/Stop.png';
	if(Clock.startTimeInterval!=null)
	{
		clearInterval(Clock.startTimeInterval);
		Clock.startTimeInterval=null;
		Clock.playedGameFlag=false;
		Clock.restartFlag=false;
		document.getElementById('stop').style.pointerEvents="none";
		if(Clock.restartFlag==false)
			Clock.checkAnswer();
	}
	else
	{
		Clock.AttemptedCounter++;
		console.log(Clock.AttemptedCounter);
		Clock.playedGameFlag=true;
		if(Clock.startTimeInterval){
			clearInterval(Clock.startTimeInterval);
			Clock.startTimeInterval=null;
		}
		Clock.startTimeInterval= setInterval( function() 
		{
			Clock.mdegree++;
			Clock.hdegree=Clock.hdegree+0.08;
			mrotate = "rotate(" + Clock.mdegree + "deg)";
			$("#min").css({"-moz-transform" : mrotate, "-webkit-transform" : mrotate});
			hrotate = "rotate(" + Clock.hdegree + "deg)";
			$("#hour").css({"-moz-transform" : hrotate, "-webkit-transform" : hrotate});
			if(Clock.finishTime<Clock.mdegree)
			{
				document.getElementById('stopImg').src=Utils.Path+"images/playImg.png";
				Clock.mdegree=0;
				Clock.AttemptedCounter--;
				clearInterval(Clock.startTimeInterval);
				Clock.startTimeInterval=null;
				document.getElementById('playSound').pause();
				Clock.hdegree =Clock.hours * 30;
				console.log(Clock.mdegree);
				hrotate = "rotate(" + Clock.hdegree + "deg)";
				$("#hour").css({"-moz-transform" : hrotate, "-webkit-transform" : hrotate});
				Clock.restartFlag=true;
			}
		}, 30);
	}
 }

//check Answer
Clock.checkAnswer=function()
{
	var randomSound=0,wrongAns=0;;
	if(Clock.mdegree-10<=parseInt(Clock.timeArr[Clock.index-1].minrotate) && Clock.mdegree+10>=parseInt(Clock.timeArr[Clock.index-1].minrotate))
	{	
		Clock.correctAnsCount=Clock.correctAnsCount+10;
		randomSound=Math.floor(Math.random() *Clock.soundArray.length);
		Clock.answerwiseMessage('correct.png',Clock.soundArray[randomSound]);
		console.log(resId,Clock.questionId,10,10,level,startTime);
		if(Utils.mobileDeviceFlag)
			Android.addScore(resId,Clock.questionId,10,10,level,startTime);
	}
	else
	{
		console.log(resId,Clock.questionId,0,10,level,startTime);
		randomSound=Math.floor(Math.random() *Clock.WrongSoundArray.length);
		Clock.answerwiseMessage('wrong.gif',Clock.WrongSoundArray[randomSound]);
		if(Utils.mobileDeviceFlag)
			Android.addScore(resId,Clock.questionId,0,10,level,startTime);
	} 
	if(Clock.nextQuestionTimeout)
	{
		clearTimeout(Clock.nextQuestionTimeout);
		Clock.nextQuestionTimeout=null;
	}
	Clock.nextQuestionTimeout=setTimeout(function()
	{
		Clock.Next();
	},3000);
}

Clock.answerwiseMessage=function(imageName,soundName)
{
	document.getElementById('ans').style.display="block";
	document.getElementById('ansImg').src=Utils.Path+"images/"+imageName;
	document.getElementById('playSound').src=Utils.Path+soundName;
	document.getElementById('playSound').play();
};

//on click of next
Clock.Next=function()
{
	if(Clock.playedGameFlag && Utils.mobileDeviceFlag)
		Android.addScore(resId,Clock.questionId,0,10,level,startTime);
	clearInterval(Clock.startTimeInterval);
	clearInterval(Clock.nextQuestionTimeout);
	Clock.startTimeInterval=null;
	Clock.playedGameFlag=false;
	document.getElementById('playSound').pause();
	document.getElementById('stop').style.pointerEvents="auto";
	if(Clock.AttemptedCounter==5)
	{
		document.getElementById('endpage').style.display="block";
		document.getElementById('background').style.display="none";
		document.getElementById('footer').style.display="none";
		document.getElementById('gameOver').innerHTML=Clock.hardCodedData.gameOver;
		document.getElementById('gameName').innerHTML=Clock.hardCodedData.gameName;
		document.getElementById('conceptDiv').innerHTML=Clock.hardCodedData.ConceptBy;
		document.getElementById('ArtWorkDiv').innerHTML=Clock.hardCodedData.ArtWork;
		document.getElementById('AudioRecordingDiv').innerHTML=Clock.hardCodedData.AudioEditing;
		document.getElementById('developerNameDiv').innerHTML=Clock.hardCodedData.DevelopedBy;
		document.getElementById('createdOn').innerHTML=Clock.hardCodedData.CreatedOn;
	}
	else{
		Clock.reset();
		Clock.setQuestion();
	}
	
}

//onclick of home
Clock.callHome=function()
{
	if(Clock.insideGame)
	{
		Clock.insideGame=false;
		if(Clock.playedGameFlag)
		{
			console.log(resId,Clock.questionId,0,10,level,startTime);
			if(Utils.mobileDeviceFlag)
				Android.addScore(resId,Clock.questionId,0,10,level,startTime);
		}
		document.getElementById('instructionDiv').style.display="block";
		document.getElementById('instruction').style.display="block";
		document.getElementById('background').style.display="none";
		document.getElementById('footer').style.display="none";
		document.getElementById('endpage').style.display="none";
		Clock.playInstruction();
	}
	else
		location.reload(true);
	
}
Clock.credits=function()
{
	document.getElementById('acknowledgementDiv').style.visibility="visible";
}

Clock.playAgain=function()
{
	Clock.reset();
	Clock.AttemptedCounter=0;
	document.getElementById('endpage').style.display="none";
	document.getElementById('background').style.display="block";
	document.getElementById('footer').style.display="block";
	document.getElementById('acknowledgementDiv').style.visibility="hidden";
	Clock.setQuestion();
}
