// 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;

Array.prototype.clean = function(deleteValue) {
  for (var i = 0; i < this.length; i++) {
    if (this[i] == deleteValue) {         
      this.splice(i, 1);
      i--;
    }
  }
  return this;
};

function getFilledCount(arr)
{
	var count=0;
	
	for (var i = 0; i < arr.length; i++) {		
 	   if (arr[i] != null && arr[i] != undefined) {         
      count++;
    }
  }
  return count;	
}

function removeAllSpaces(str)
{
	return (str.replace(/ /g,''));
}


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 null;
	}

}

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

function disableSubmitBTN(idd){
	//console.log("dis sub----------");
	getElement("#submitBtn").style.pointerEvents = "none";
	getElement("#submitBtn").style.opacity = 0.5;
	
	//(idd+1) && getElement("#in"+idd).removeEventListener("keypress",onKeypress);
}

function enableSubmitBTN(idd){
	//console.log("--------- enable sub");
	getElement("#submitBtn").style.pointerEvents = "auto";
	getElement("#submitBtn").style.opacity = 1;
	
	//(idd+1) && getElement("#in"+idd).addEventListener("keypress",onKeypress);
}


function onKeypress(e)
{
	
        if(e.keyCode === 13)
		{
            e.preventDefault(); // Ensure it is only this code that rusn

            onSubmit();
        }
    
}


function disableSolution(){
	//console.log("disableSolution", disableSolution.caller);
	getElement("#answerBtn").style.pointerEvents = "none";
	getElement("#answerBtn").style.opacity = 0.5;
	
	/*getElement("#next").style.pointerEvents = "none";
	getElement("#next").style.opacity = 0.5;
	
	getElement("#prev").style.pointerEvents = "none";
	getElement("#prev").style.opacity = 0.5;*/
	
}

function enableSolution(cQ){
	//console.log("enableSolution", enableSolution.caller);
	getElement("#answerBtn").style.pointerEvents = "auto";
	getElement("#answerBtn").style.opacity = 1;
}

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

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

function getWidthOf(str, info, size, maxW)
{
	var tempDiv = createTag("div");
	var idcls;
	var cls;
	var txt="";
	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;
	}
	if(maxW)
	{
		for(var i=0;i<str.length;i++){
			txt += "W";
		}
		tempDiv.innerHTML = txt;
	}else{
		tempDiv.innerHTML = str;
	}
	tempDiv.style.display = "inline-block";
	//console.log("[[[[[[[[: ", tempDiv);
	//console.log("**********: ", tempDiv.offsetWidth, tempDiv);
	width = tempDiv.offsetWidth; 
	//tempDiv.style.border = "2px solid";
	document.body.removeChild(tempDiv);
	tempDiv = null;
	//console.log("RETURNING:  ",str, width, txt);
	return width;
}



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

function finishActivity()
{
	setTimeout(playActivityComp, makeAnimationSpaceDelay);
}


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("A ERR: ", err);
		}
	
}


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(qAttempts == 1)
			{
				wArr = wArr.concat(nArr);
			}
			//console.log("AAAAAA:  ", attempts);
			if(qAttempts == ATTEMPTS_ALLOWED)
			{
				for(var ii=0;ii<nArr.length;ii++)
				{
					if(wArr.indexOf(nArr[ii]) > -1)
					{
						wArr.splice(wArr.indexOf(nArr[ii]),1);
					}
				}				
			}		
			//console.log("*******:  ", wArr);	
			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 playActivityComp()
{
	makeAnimationSpace();
}


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";
	
	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(completedDelay, 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");
}
