// JavaScript Document
const cArr=["Oops"];

var rArr = ["Amazing","Brilliant","Excellent","Good","GoodJob","Superb","Welldone","Wonderful"];
var wArr = [];
var nArr = ["ThinkAgain","LetsTryAgain","LetsGiveAnotherShot","TryOnceAgain"];

var audio;
var func;

function getElement(str){
	switch (str[0])
	{		
		case ".":
			return document.getElementsByClassName(str.substr(1,str.length-1));		
		case "#":
			return document.getElementById(str.substr(1,str.length-1));
		default:
			return document.getElementById(str);
	}

}

function createTag(str){
	return (document.createElement(str));
}

function disableSubmitBTN(){
	//console.log("-------------SUBMIT");
	submitBtn.style.pointerEvents = "none";
	submitBtn.style.opacity = 0.5;
}

function enableSubmitBTN(){
	getElement("submitBtn").style.pointerEvents = "auto";
	getElement("submitBtn").style.opacity = 1;
}

function disableSolution(){	
	getElement("answerBtn").style.pointerEvents = "none";
	getElement("answerBtn").style.opacity = 0.5;
}

function enableSolution(){
	getElement("answerBtn").style.pointerEvents = "auto";
	getElement("answerBtn").style.opacity = 1;
}

function disableObj(Obj){
	//console.log("DIS:  ", Obj);
	Obj.style.pointerEvents = "none";
	Obj.style.opacity = 0.5;
}

function enableObj(Obj){
	//console.log("ENA:  ", Obj);
	Obj.style.pointerEvents = "auto";
	Obj.style.opacity = 1;
}


function getWidthOf(str, info, size)
{
	var tempDiv = createTag("div");
	var idcls;
	var cls;
	document.body.appendChild(tempDiv);	
	switch (info[0])
	{
		
		case ".":
			cls = info.substr(1,info.length-1).toString();
			tempDiv.classList.add(cls);	
			size && (tempDiv.style.fontSize = size + "px");
			break;	
		case "#":
			tempDiv.setAttribute("id",(info.substr(1,info.length-1)));
			size && (tempDiv.style.fontSize = size + "px");
			break;
		default:
			return null;
	}
		
	tempDiv.innerHTML = str;
	tempDiv.style.display = "inline";
	//console.log("**********: ", tempDiv.offsetWidth, tempDiv);
	width = tempDiv.offsetWidth; 
	document.body.removeChild(tempDiv);
	tempDiv = null;
	//console.log("RETURNING:  ", width);
	return width;
}


function appendAtGlobal(par,kid, cPar)
{
	//var top,left;	
	//appendAtBody(kid);
	var boo=false;
	var cx,cy,px,py,ox,oy;
	//cx = parseInt(kid.style.left);
	//cy = parseInt(kid.style.top);
	//console.log("PAR:  ", par.id, "  CPAR:  ", cPar.id);	
	if(cPar.id != par.id)
	{
		//console.log("HERE: ",par.id, cPar.id);
		if(par.id == "optDv")
		{			
			//filledCount--;
			//console.log("-----------Count", par.id, cPar.id,filledCount);
			boo = cPar.id;
		}else if(cPar.id.indexOf("dropBox") == -1)
		{			
			//filledCount++;
			//console.log("++++++++++++Count", par.id, cPar.id,filledCount);
			
		}else
		{
			//console.log("SAMEEEEEEEE:  ", filledCount);
			boo = cPar.id;
		}
	}
	//console.log("COUNT:  ", filledCount);
	
	cx = (parseInt(cPar.style.left)||0) + parseInt(kid.style.left) + (parseInt(kid.getAttribute('data-x')||0));
	cy = (parseInt(cPar.style.top)||0) + parseInt(kid.style.top) + (parseInt(kid.getAttribute('data-y')||0));	
	
	//console.log("XXX:  ",parseInt(cPar.style.left),(parseInt(cPar.style.left)||0) , parseInt(kid.style.left) ,"DATAX:  ", (parseInt(kid.getAttribute('data-x')||0)));
	//console.log("YYY:  ",parseInt(cPar.style.top),(parseInt(cPar.style.top)||0) , parseInt(kid.style.top) ,"DATAY:  ", (parseInt(kid.getAttribute('data-y')||0)));
	//console.log(cx, cy, "sdfhkjdsi jfhdsjfhds kfhdsf shjf b")
	/*px = parseInt(par.style.left);
	py = parseInt(par.style.top);
	
	
	
	ox = px - cx;
	oy = py - cy;*/
	
	par.appendChild(kid);
	kid.style.left =cx + "px";
	kid.style.top = cy + "px";
	//console.log("KID: ", kid.style.left, kid.style.top);
	return(boo);
}


function appendAt(id, obj)
{
	var cx,cy,px,py,ox,oy;
	var tar = getElement(id);
	kid = obj;
	var pa = kid.parentNode;
	var offset;
	cx = 0;
	cy = 0;
	
	var count=0;
	
	while(pa != tar)
	{
		offset = $(kid).offset();		
		cx += offset.left;
		cy += offset.top;
		kid = pa;
		pa = kid.parentNode;		
		count++;
		
	}
	
	offset = $(tar).offset();
	px = offset.left;
	py = offset.top;
	//console.log("PAR:  ",tar, cx,cy);
	tar.appendChild(obj);
	obj.style.left = "0px";
	obj.style.top = "0px";
	obj.style.left = (cx - px) + "px";
	obj.style.top = (cy - py) + "px";
	

	//console.log("PAR: ",tar, "    obj:  ", obj);
	//console.log("FINALLY:  ", cx,cy);
	
	
}


function wait(ms){
   var start = new Date().getTime();
   var end = start;
   while(end < start + ms) {
     end = new Date().getTime();
  }
}

//================================================================================================================================================

function playWrongFeedback(id, fun)
{
	func = fun;
		sourceOGG.src = "../__audio/Right"+id+".ogg";
	sourceMP3.src = "../__audio/Right"+id+".mp3";
	audio.addEventListener("ended", onAudioEnded);
	//console.log("AAAAAAAAAAAAAAAA:  ", audio);
	audio.load();
audio.play();
}


function playRemainingWrongFeedback(id, fun)
{
	func = fun;
				sourceOGG.src = "../__audio/MoreGo"+id+".ogg";
	sourceMP3.src = "../__audio/MoreGo"+id+".mp3";
	audio.addEventListener("ended", onAudioEnded);
	//console.log("AAAAAAAAAAAAAAAA:  ", audio);
	audio.load();
audio.play();
	
}


function onAudioEnded(e)
{
	//console.log("AUDIO END===========");
	audio.removeEventListener("ended", onAudioEnded);
	try{
		func();
		}catch(err)
		{
			console.log("ERR: ", err);
		}
	
}



function finishActivity()
{
	console.log("FINISHING..............");
	disableObj(getElement("#prev"));
	disableObj(getElement("#next"));
	setTimeout(makeAnimationSpace, makeAnimationSpaceDelay);
}

function playFeedback(val, fun)
{
	var ranNum;
	func = fun;
	switch(val){
		case "right":
			ranNum = parseInt(Math.random()*(rArr.length));
			//console.log(ranNum)
									sourceOGG.src = "../__audio/" + rArr[ranNum] + ".ogg";
			sourceMP3.src = "../__audio/" + rArr[ranNum] + ".mp3";
		break;
		case "wrong":
			wArr = cArr;
			if(attempts == 1)
			{
				wArr = wArr.concat(nArr);
			}
			//console.log("AAAAAA:  ", attempts);
			if(attempts == ALLOWED)
			{
				for(var ii=0;ii<nArr.length;ii++)
				{
					if(wArr.indexOf(nArr[ii]) > -1)
					{
						wArr.splice(wArr.indexOf(nArr[ii]),1);
					}
				}				
			}				
			ranNum = parseInt(Math.random()*(wArr.length));			
									sourceOGG.src = "../__audio/" + wArr[ranNum] + ".ogg";
			sourceMP3.src = "../__audio/" + wArr[ranNum] + ".mp3";
		break
		case "correct":
									sourceOGG.src = "../__audio/ThisIsTheCorrectAns.ogg";
			sourceMP3.src = "../__audio/ThisIsTheCorrectAns.mp3"
		break
		case "completed":
						sourceOGG.src = "../__audio/completed.ogg";
			sourceMP3.src = "../__audio/completed.mp3";
		break
	}
	audio.addEventListener("ended", onAudioEnded);
	audio.load();
audio.play();
}


function makeAnimationSpace(){
	
	document.getElementById("main").style.display="none";
	var actComDiv = document.createElement("div");
	document.body.appendChild(actComDiv);
	actComDiv.style.position = "absolute";
	actComDiv.style.left = 0+"px";
	actComDiv.style.top = 0+"px";
	actComDiv.style.width = 0+"px";
	actComDiv.style.height = 720+"px";
	actComDiv.style.zIndex = "200";
	actComDiv.style.backgroundColor = "#fff";
	actComDiv.style.outline = "none";	
	animDiv = document.createElement("div");
	actComDiv.appendChild(animDiv);
	animDiv.style.position = "absolute";
	animDiv.style.width = 1280+"px";
	animDiv.style.height = 233+"px";
	animDiv.style.top = 243+"px";
	animDiv.style.overflow = "hidden";
	animDiv.style.backgroundImage = "url(../__images/activityComplete.png)";
	animDiv.style.backgroundSize = "1280px 233px";
	animDiv.style.backgroundRepeat = "no-repeat";	
	$(actComDiv).animate({width:1280},500);	
	setTimeout(function () {
		completedDelay();
		// Set SCORM status to "completed" here
		if (typeof SCO !== 'undefined') {
			SCO.set("cmi.core.lesson_status", "completed");
			SCO.save();
		}
	}, 500)
}

function completedDelay(){
	var activityText = document.createElement("img");
	animDiv.appendChild(activityText);
	activityText.src = "../__images/activity.png";
	activityText.setAttribute("id","activityText");	
	var completedText = document.createElement("img");
	animDiv.appendChild(completedText);
	completedText.src = "../__images/completed.png";	
	completedText.setAttribute("id","completedText");		
	playFeedback("completed");
}