/* functions to control hotwords */

$('.hotword').click(function(){
	/*findHotWordNum()
	$(this).attr('data',1);
	var hotword_sec = '#'+$(event.target).closest('.section').attr('id')
	isHotWordDone(hotword_sec)*/
})

function findHotWordNum(secNum){	
	numberOfHotWord = 0;
	$('#sec_'+secNum).find('.hotword').each(function(index, element) {
       numberOfHotWord++;
    });
}

function hotwordClicked(clicked,locked){
	
	$('.box_middle').focus(); /* 20180501 Accessibility Update */
	var hotword_sec = '#'+$(clicked).closest('.section').attr('id');
	var secNum = hotword_sec.split('_')[1];	
	var hotclass;
	var hotclone;
	if($(clicked).closest(".hotspot_reveal").length>0){
		hotclass = 'hotword_'+$('.hotword').index(clicked)+'_'+$(clicked).closest('div').attr('id');
		hotclone = '<div class='+hotclass+'></div>';	
	}
	if(locked){
		if(Number($(clicked).attr('data-hotword')) == 0){
			$(clicked).attr('data-hotword',1);	
		}
	}
	if($('.'+hotclass).length<1 && $(clicked).closest(".hotspot_reveal").length>0){
		$(hotword_sec).append(hotclone);
		isHotWordDone(hotword_sec,locked);
	}else{
		isHotWordDone(hotword_sec,locked);
	}
}

function isHotWordDone(hotword_sec,locked){
	var secNum = hotword_sec.split('_')[1];
	var hotWordDone = 0;
	var clicked = 0;
	$('#sec_'+secNum).find('.hotword').each(function(index, element) {				
		var data = Number($(this).attr('data-hotword'));
		if(data === Number(1)){
			hotWordDone++;
			clicked = Number($('#sec_'+secNum).attr('data-clicked'));
			clicked +=1;
			$('#sec_'+secNum).attr('data-clicked',clicked);
			$(this).attr('data-hotword','2');
		}		
    });
	
	/*if(Number(document.hotArr[Number(secNum) - 1]) != 0){
		$('#sec_'+secNum).attr('data-clicked',clicked);
	}*/
	console.log("secNum = " + secNum + "          $('#sec_'+secNum).attr('data-clicked') = " + $('#sec_'+secNum).attr('data-clicked'));
	var totalClicked = Number($('#sec_'+secNum).attr('data-clicked'))// + Number($('#sec_'+secNum).attr('data-links')) + Number($('#sec_'+secNum).attr('data-hotwords'));
	var totalClicks;
	var clickArr = [];
	if(isNaN(document.clickArr[Number(secNum) - 1])){
		clickArr = document.clickArr[Number(secNum) - 1].split(',');
		totalClicks = Number(clickArr[0]) + Number(clickArr[1]) + Number(clickArr[2])
		//alert(clickArr);
	}else{
		totalClicks = Number(document.clickArr[Number(secNum) - 1]) //+ Number(document.linkArr[Number(secNum) - 1]) + Number(document.hotArr[Number(secNum) - 1])
	}	
	if(locked){	
		if(totalClicked == totalClicks){
			if(isAndroidTablet || isAndroidPhone || isMacChrome){
				document.intComplete = true;
				clearInterval(document.lockinterval)
				document.lockinterval = setInterval(function(){
					console.log(hotword_sec)
					che = hotword_sec
					secNum = hotword_sec.split('_')[1]
					document.getnear(che);
					console.log('#sec_'+(parseInt(secNum)+1))
					isPageComplete(document.currentSection);
					if(($('#sec_'+(parseInt(secNum)+1)).css('display')=="block" || !$('#right_nav').hasClass('locked')) && document.courseLockedDown == true){
						clearInterval(document.lockinterval)
						
					} 
				},250)
			}else{
				document.intComplete = true;
				isPageComplete(document.currentSection);
			}
			isHotSpotScreen = false;			
		}
	}	
	//alert('clicked = ' + clicked);	
	//console.log('totalClicked = ' +totalClicked + '     totalClicks = ' + totalClicks);
}

$('.hotword').attr('data-hotword',0);

$('.popup_pop').attr('data-link',0);

function linkClicked(clicked, locked, contentLnk){
	/* links functionality 2019 */
	if (contentLnk)
	{
		var link_sec = '#'+$(clicked).closest('.section').attr('id');
		var secNum = link_sec.split('_')[1];
		var linkclass;
		var linkclone;
		if($(clicked).closest(".hotspot_reveal").length>0){
			 linkclass = 'link_'+$('a').index(clicked)+'_'+$(clicked).closest('div').attr('id');
			 linkclone = '<div class='+linkclass+'></div>';	
		}
		if(locked){
			if(Number($(clicked).attr('data-link')) == 0){
				$(clicked).attr('data-link',1);	
			}
		}
		if($('.'+linkclass).length<1 && $(clicked).closest(".hotspot_reveal").length>0){
			$(link_sec).append(linkclone);
			isLinkDone(link_sec,locked);
		}else{
			isLinkDone(link_sec,locked);
		}
	}
	else
	{
		whatPop(clicked);
	}
}

function isLinkDone(link_sec,locked){
	var secNum = link_sec.split('_')[1];
	var linkDone = 0;
	var clicked = 0;
	$('#sec_'+secNum).find('.popup_pop').each(function(index, element) {				
		var data = Number($(this).attr('data-link'));
		if(data === Number(1)){
			linkDone++;
			clicked = Number($('#sec_'+secNum).attr('data-clicked'));
			clicked +=1;
			$('#sec_'+secNum).attr('data-clicked',clicked);
			$(this).attr('data-link','2');
		}		
    });		
	var totalClicked = Number($('#sec_'+secNum).attr('data-clicked'))// + Number($('#sec_'+secNum).attr('data-links')) + Number($('#sec_'+secNum).attr('data-hotwords'));
	var totalClicks;
	var clickArr = [];
	if(isNaN(document.clickArr[Number(secNum) - 1])){
		clickArr = document.clickArr[Number(secNum) - 1].split(',');
		totalClicks = Number(clickArr[0]) + Number(clickArr[1]) + Number(clickArr[2])
		//alert(clickArr);
	}else{
		totalClicks = Number(document.clickArr[Number(secNum) - 1]) //+ Number(document.linkArr[Number(secNum) - 1]) + Number(document.hotArr[Number(secNum) - 1])
	}	
	if(locked){	
		if(totalClicked == totalClicks){
			if(isAndroidTablet || isAndroidPhone || isMacChrome){
				document.intComplete = true;
				clearInterval(document.lockinterval)
				document.lockinterval = setInterval(function(){
					console.log(hotword_sec)
					che = hotword_sec
					secNum = hotword_sec.split('_')[1]
					document.getnear(che);
					console.log('#sec_'+(parseInt(secNum)+1))
					isPageComplete(document.currentSection);
					if(($('#sec_'+(parseInt(secNum)+1)).css('display')=="block" || !$('#right_nav').hasClass('locked')) && document.courseLockedDown == true){
						clearInterval(document.lockinterval)
						
						} 
					},250)
			}else{
				document.intComplete = true;
				isPageComplete(document.currentSection);
			}
			isHotSpotScreen = false;
		}
	}	
	console.log('totalClicked = ' +totalClicked + '     totalClicks = ' + totalClicks);
}