/*	Declaration Section  */

/*  Global Variables  */
var tracing=[];
	tracing.language=""; 
	tracing.level=0;
	tracing.store=0;
	tracing.cnt=0;
	tracing.currentI=0;
	tracing.scrollOffset=100;
	tracing.maxScroll=0;
	tracing.jsonData=[];
	tracing.displayHeight=0;
	tracing.textHeightInCanvas;
	tracing.scrollDivMargin=0;
	tracing.canvas;
	tracing.language;
	tracing.drawElement=[];
	tracing.currentElement="";
	 tracing.todayData=[];
	
var questionId=0,startTime,scoredMark=1,totalMark=1,somethingDone=false;
/*  Canvases and Inner-canvases  */
	tracing.can1=document.getElementById("canvas1");
	tracing.caninner1=document.getElementById("canvasinner1");

/*  Execution section  */
$( document ).ready(function() {
	tracing.language="hindi";
	//tracing.setFontFamilyForLang(languagewiseJSON[tracing.language].font);
	Utils={};
	Utils.Path="";
	Utils.mobileDeviceFlag=false;
	/* Comment these lines(both ifs) for ekstep intigration */
	/*  Checking the host type: Desktop/Tablet  */
     if(navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i))
		 Utils.mobileDeviceFlag=true;
	
	if(Utils.mobileDeviceFlag){
        //Utils.Path=Android.getMediaPath("Aakar_KS");
        tracing.todayData=(Android.getTodaysLetters()).split(',');
	}
	else 
		tracing.todayData=getTodaysLetters();

    tracing.setImagesThroughScript();
	tracing.allEventListenersRegistration();	  						/*Registered Event Listeners here */
	$("#intro,#outerMainpage").hide();
	$("#container").show();	
	$("#HomeImage").css("visibility","hidden");
	tracing.startlevel();
});

function getTodaysLetters(){
	var temp=[];
	temp=populate(temp,2);
	return temp;
}
function populate(array2, length)
{
	var randomIndex,array1=languagewiseJSON.hindi.imgTextMapping;
	while (array2.length !== length) 
	{
	randomIndex = Math.floor(Math.random()*array1.length);
	if($.inArray(array1[randomIndex].textToDraw,array2) === -1)
	array2.push(array1[randomIndex].textToDraw);
	}
	return array2;
}


tracing.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");	
};

tracing.setImagesThroughScript=function() {										 /* Setting images through Script rather than html/css */
   /* Setting images through Script rather than html/css */
    $("#HomeImage").prop("src",Utils.Path+"Images/home.png");
	$("#logoImage").prop("src",Utils.Path+"Images/prathamLogo.png");
    $(".soundButtons").prop("src",Utils.Path+"Images/Speaker-icon.png");
    $(".restartButtons").prop("src",Utils.Path+"Images/restart.png");
    $(".clearButtons").prop("src",Utils.Path+"Images/erase.png");
    $(".Pratham,#container").hide();
    $(".Next").prop("src",Utils.Path+"Images/next.png");
};


tracing.startlevel=function() {
	var j;
	$("#HomeImage").css("visibility","visible");
	/* Storing JSON data */ 

		$("#selectType").hide();
		$("#outerMainpage").show();
		$("#mainpage").show();
		//******** Show Data Copy 
		tracing.jsonData=languagewiseJSON[tracing.language].imgTextMapping;
	
		tracing.letterIndex=[];
		for(var i=0;i<tracing.todayData.length;i++)
		{
			for(j=0;j<tracing.jsonData.length;j++)
			{
				if(tracing.todayData[i].indexOf(tracing.jsonData[j].textToDraw) != -1)
			    {
			    	tracing.letterIndex.push(j);
			    	break;
			    }
			 }   
		}
		//console.log(tracing.letterIndex);
		tracing.displayHeight=80;
		tracing.scrollDivMargin=5;
		tracing.maxScroll=2500;
	//}
	//tracing.interval=setInterval(function(){tracing.drawText();},4000);
	$("#heading").show();
    $("#level1").show();
    $("#mainpage").hide();
    $('body').css('background-color','rgb(179, 179, 179)');
    $('body').css('background-image','none');
	tracing.canvas = document.getElementById('canvasinner1');
	tracing.fitToContainer(tracing.canvas);
    $("#canvasinner1").sketch({defaultColor: "rgb(132, 194, 37)",defaultSize: "20"});
	sketch1=$("#canvasinner1").sketch();
	//var audio=new Audio(Utils.Path+"audio/hindi/"+languagewiseJSON[tracing.language].levelSoundMapping.soundURL);
	//if(tracing.level==2){
		$("#outerScrollbar").css("visibility","visible");
		$("#canvas1").prop('height',$("#showGif1").height());
        $("#canvas1").prop('width',$("#showGif1").width());
		tracing.setScroll();
	//}
	//audio.play();
};

tracing.fitToContainer=function(canvas) {
	canvas.style.width='100%';
	canvas.style.height='100%';
	canvas.width  = canvas.offsetWidth;
	canvas.height = canvas.offsetHeight;
}

tracing.setTextForLevel=function(){
	var d=new Date();
   	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
   	
	var cntx=tracing.can1.getContext("2d");
	cntx.setLineDash([5,5]);
	tracing.setCanvasFontSizeAndLineHeightAsPerWidth(cntx);
    cntx.textAlign = "center";
	/**chng**/
	var position = Math.floor(Math.random() * tracing.letterIndex.length) + 0;
	while(position==tracing.store){
		position=Math.floor(Math.random() * tracing.letterIndex.length) + 0;
	}
	tracing.store=tracing.letterIndex[position];
	/**chng**/var img="url("+Utils.Path+"Images/hindi/"+tracing.jsonData[tracing.letterIndex[position]].imgURL+")";
	tracing.currentI=tracing.letterIndex[position];
	questionId=tracing.jsonData[tracing.currentI].questionId;
	//console.log(questionId);
	//tracing.drawElement
	$("#showGif1").css('background-image',img);
	cntx.strokeStyle="#FFFFFF";
	/**chng**/
	cntx.strokeText(tracing.jsonData[tracing.letterIndex[position]].textToDraw, tracing.can1.width/2, (tracing.textHeightInCanvas+tracing.displayHeight));
	
	tracing.currentElement=tracing.jsonData[tracing.letterIndex[position]].textToDraw;
	//console.log(tracing.currentElement);
	tracing.QuestionId=position+1;  	
};

tracing.clearLevel=function(){
    tracing.caninner1.getContext("2d").clearRect(0,0,tracing.caninner1.width,tracing.caninner1.height);sketch1.actions=[];
};

tracing.setScroll=function(){
	var temp="";
	for(i=0;i<tracing.letterIndex.length;i++){
		/*chng*/temp+="<div id='div"+i+"' class='scrollElementCls' style='margin-right:"+tracing.scrollDivMargin+"%; font-family:Century Gothic; color:rgb(256, 256, 256);' onclick='tracing.genericTextLevel("+tracing.letterIndex[i]+")'><h2 class='scrollElementCls' >"+tracing.jsonData[tracing.letterIndex[i]].textToDraw+"</h2></div>";
	}
	$("#scrollbar").html(""+temp);
	/*chng*/tracing.genericTextLevel(tracing.letterIndex[0]);
};

tracing.genericTextLevel=function(i){
	var d=new Date();
   	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();

    tracing.clearLevel();
	tracing.can1.getContext("2d").clearRect(0,0,tracing.can1.width,tracing.can1.height);
	tracing.currentI=i;
    var cntx=tracing.can1.getContext("2d");
	cntx.setLineDash([5,5]);
	tracing.setCanvasFontSizeAndLineHeightAsPerWidth(cntx);
	cntx.textAlign = "center";
	$("#showGif1").css('background-image','url('+Utils.Path+"Images/hindi/"+tracing.jsonData[i].imgURL+')');
	cntx.strokeStyle="#FFFFFF";
	cntx.strokeText(tracing.jsonData[i].textToDraw, tracing.can1.width/2, (tracing.textHeightInCanvas+tracing.displayHeight));
	
	tracing.currentElement=tracing.jsonData[i].textToDraw;
	questionId=tracing.jsonData[tracing.currentI].questionId;
	//console.log(questionId);
	tracing.QuestionId=i+1; 

	var audio=new Audio(Utils.Path+"audio/hindi/"+tracing.jsonData[tracing.currentI].soundURL);
	//	audio.play(); 	
};

tracing.setCanvasFontSizeAndLineHeightAsPerWidth=function(cntx){
	var w = window.innerWidth;
	if(w<=400){
		cntx.font = "100px Century Gothic";
		tracing.textHeightInCanvas=	tracing.can1.height/4; 
	}
	else if(w>400 && w<=600){
		cntx.font = "130px Century Gothic";
		tracing.textHeightInCanvas=	tracing.can1.height/4; 
	}
	else if(w>600 && w<=700){
		cntx.font = "140px Century Gothic";
		tracing.textHeightInCanvas=	tracing.can1.height/4; 
	}
	else if(w>700 && w<=800){
		cntx.font = "150px Century Gothic";
		tracing.textHeightInCanvas=	tracing.can1.height/3; 
 	}
 	else if(w>800 && w<=900){
		cntx.font = "160px Century Gothic";
		tracing.textHeightInCanvas=	tracing.can1.height/3; 
 	}
 	else if(w>900 && w<=1000){
		cntx.font = "170px Century Gothic";
		tracing.textHeightInCanvas=	tracing.can1.height/3; 
 	}
 	else if(w>1000 && w<=1200){
		cntx.font = "170px Century Gothic";
		tracing.textHeightInCanvas=	tracing.can1.height/2; 
 	}
	else if(w>1200){
		cntx.font = "280px Century Gothic";
		tracing.textHeightInCanvas=	tracing.can1.height/2; 
	}
}

tracing.setMarker=function(r,g,b,element){
    $(".description>a").removeClass("blink_me");
    $(element).addClass("blink_me");
	sketch1.color="rgb("+r+","+g+","+b+")";
};

tracing.resetGame=function(){
	$("#outerScrollbar").css("visibility","hidden");
	$("#showGif1").css("background-image","");
	tracing.can1.getContext("2d").clearRect(0,0,tracing.can1.width,tracing.can1.height);
	try{
		sketch1.actions=[];	
	}
	catch(err){
		console.log(err);
	}
	$("#outerMainpage").hide();
	$("#selectType").show();
};

/*  Event listeners registration  */
tracing.allEventListenersRegistration=function(){
	$("#forClear").on("click", tracing.clearLevel);
	$("#forRestart").on("click", function() {
		tracing.drawText(); 		
    });
    $("#forNext").on("click", function() {
		var d=new Date();
   	    startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
   
  		tracing.clearLevel();
		tracing.can1.getContext("2d").clearRect(0,0,tracing.can1.width,tracing.can1.height);
	
			tracing.setTextForLevel();
	});
	$("#forSound").on("click", function() {
  		var audio=new Audio(Utils.Path+"audio/hindi/"+tracing.jsonData[tracing.currentI].soundURL);
		audio.play();
	});
	
};

tracing.drawText=function(){
	$('#showGif1').css('background-image', 'url("' + Utils.Path+"Images/hindi/"+tracing.jsonData[tracing.currentI].imgURL + '?x=' + Date.now() + '")' );
}
