/* functions to control hotspot interactivities */

var numberOfHotspot;
/* transitions: scale up/down hotspot button images */
var currHotSpotItem = 0;
/* transitions: grow up/down hotword displays */
var lastHotSpotItem = 0;
var hotBanPos;
var isHotSpotScreen;
/* transitions: grow up/down hotword displays */
var currHotSpot = 0;

var hotSpotPos; // hotspot screen jump fix

$( document ).ready(function() {

	$(window).resize(function() {		
		/*if(window.top !== window.self) {
			$('.hotspot_btn_ind').hide();
			$('.hotspot_reveal').hide();
			$('.hotspot_btn').css('cursor','pointer');
		}else{
			$('.hotspot_btn_ind').hide();
			$('.hotspot_reveal').hide();
			$('.hotspot_btn').css('cursor','pointer');
		}*/		
		if(isiPad == false && isiPhone == false){
			$('.hotspot_btn_ind').hide();
			$('.hotspot_reveal').hide();
			$('.hotspot_btn').css('cursor','pointer');
			markOtherHotSpots(); // Updated hotspot selected states when moving to new hottspot
		}
		/*if(isiPad || isiPhone){
			//hideBanner();
			setTimeout(function(){
				alert($(parent.parent.document).scrollTop());
				showBanner();
				},200);			
		}*/
		
		
	});
	
	$('.hotspot_btn').click(function(e){
		if(document.useTransitions){
			/* transitions: scale up/down hotspot button images */
			$(this).children('img').removeClass('hotspot_scale');
		}	
		findHotspotNum();
		isHotSpotScreen = true;
		hotSpotPos = $(window).scrollTop(); // hotspot screen jump fix
		
		$('.hotspot_btn_ind').hide();
		//alert($('.hotspot_reveal').css('display'))
		$('.hotspot_reveal').hide();
		$('.hotspot_btn').css('cursor','pointer');
		var totalHotspots = $('#sec_' + document.currentSection + ' .hotspot_btn').length;
		var hspID = $(this).attr('id').split('btn_')[1];		
		var hspSec = hspID.split('_')[0];
		var hspItem = hspID.split('_')[1];
		var hotspotIncomp = false;
		var hotspot_sec = '#'+$(e.target).closest('.section').attr('id');
		if(document.useTransitions){
			/* transitions: scale up/down hotspot button images */
			currHotSpotItem = hspID;
		}
		
		var hsRow = Number($(this).parent().parent().attr('class').split('_')[1]);
		
		$('#hotspot_btn_ind_' + hspID).show();		
		if ($('.hotspot_nodiv').css('display') == "block") {	
			var hotTabInd = $('#hotspot_reveal_' + hspID).attr('tabindex'); /* 20180501 Accessibility So we can tab to hotspot content */
			var txtHeight = $('#hotspot_reveal_' + hspID).height();		
			
			if(hsRow == 1){
				$('#hotspot_reveal_' + hspSec + '_lg_txt').html('<div id="hotspot_txt_'+hspSec+'_'+hspID+'" class="hotspot_txt">'+$('#hotspot_reveal_' + hspID + '_txt').html()+'</div>');
				
				$('#hotspot_reveal_' + hspSec + '_lg').attr('tabindex', hotTabInd); /* 20180501 Accessibility So we can tab to hotspot content */
				
			}else{
				$('#hotspot_reveal_' + hspSec + '_lg_txt_2').html('<div id="hotspot_txt_'+hspSec+'_'+hspID+'" class="hotspot_txt">'+$('#hotspot_reveal_' + hspID + '_txt').html()+'</div>');
				
				$('#hotspot_reveal_' + hspSec + '_lg_2').attr('tabindex', hotTabInd); /* 20180501 Accessibility So we can tab to hotspot content */
			}
			
			
			if(isWeb){
				if(document.useTransitions){
					/* transitions: grow up/down hotword displays */
					if(hsRow == 1){
						$('#hotspot_reveal_' + hspSec + '_lg').css('height',txtHeight+'px')
					}else{
						$('#hotspot_reveal_' + hspSec + '_lg_2').css('height',txtHeight+'px')
					}
					if(currHotSpot!= hspSec){
						if(hsRow == 1){
							$('#hotspot_reveal_' + hspSec + '_lg').show('blind',500, function(){
								$('#hotspot_txt_'+hspSec+'_'+hspID).show('blind',500, function(){
									/* 2021 Accessibility Update */
									$('#hotspot_reveal_' + hspSec + '_lg').focus();
								});
							});
							
						}else{
							$('#hotspot_reveal_' + hspSec + '_lg_2').show('blind',500, function(){								
								$('#hotspot_txt_'+hspSec+'_'+hspID).show('blind',500, function(){
									/* 2021 Accessibility Update */
									$('#hotspot_reveal_' + hspSec + '_lg_2').focus();
								});
							});
						}
						currHotSpot = hspSec;
						lastHotSpotItem = hspID;
					}else{					
						if(hsRow == 1){		
							$('#hotspot_reveal_' + hspSec + '_lg').show();
							/* 2021 Accessibility Update */
							$('#hotspot_reveal_' + hspSec + '_lg').focus();							
						}else{
							$('#hotspot_reveal_' + hspSec + '_lg_2').show();
							/* 2021 Accessibility Update */
							$('#hotspot_reveal_' + hspSec + '_lg_2').focus();							
						}
						if (currHotSpotItem == lastHotSpotItem)
						{
							$('#hotspot_txt_'+hspSec+'_'+hspID).show();
						}
						else
						{
							$('#hotspot_txt_'+hspSec+'_'+hspID).show('blind',500);
						}
						lastHotSpotItem = hspID;
					}					
				}
				else{
					if(hsRow == 1){
						$('#hotspot_reveal_' + hspSec + '_lg').show();
						/* 2021 Accessibility Update */
						$('#hotspot_reveal_' + hspSec + '_lg').focus();
					}else{
						$('#hotspot_reveal_' + hspSec + '_lg_2').show();
						/* 2021 Accessibility Update */
						$('#hotspot_reveal_' + hspSec + '_lg_2').focus();						
					}
					$('#hotspot_txt_'+hspSec+'_'+hspID).show();
				}
				$('#hotspot_btn_ind_' + hspSec + '_' + hspID).focus().blur(); // hotspot screen jump fix
				$(window).scrollTop(hotSpotPos); // hotspot screen jump fix
			}else{
				if(hsRow == 1){
					$('#hotspot_reveal_' + hspSec + '_lg').show();
					/* 2021 Accessibility Update */
					$('#hotspot_reveal_' + hspSec + '_lg').focus();	
				}else{
					$('#hotspot_reveal_' + hspSec + '_lg_2').show();
					/* 2021 Accessibility Update */
					$('#hotspot_reveal_' + hspSec + '_lg_2').focus();	
				}
				$('#hotspot_txt_'+hspSec+'_'+hspID).show();
			}
		}
		else {			
			$('#hotspot_reveal_' + hspID).show();
			/* 2021 Accessibility Update */
			$('#hotspot_reveal_' + hspID).focus();				
		}
		$(this).css('cursor','default');
		
		hotSpotVisited(hspSec,totalHotspots,this); // Hotspot selected state
		
		if(Number($(this).attr('data-hotspot')) == 0){
			$(this).attr('data-hotspot',1);
		}
		isHotspotDone(hotspot_sec);		
		
		/* hotspot banner fix */
		if(isiPad){
			if(iOSApp){
				//$('.bar_robot').removeClass('displayBlock').addClass('displayNone');
			}
			setTimeout(function(){hotSpotAdjustBanner();},100);
		}		
		if(isAndroidTablet || isAndroidPhone || isMacChrome){
				$('html,body').scrollTop(windowTop -1)	
		}
		return false;		
	});	
	
	/* added keyboard accessibility */
	$('.hotspot_btn').keypress(function (e) {
		if (e.keyCode == 13 || e.keyCode == 32) {
			
			$(this).trigger('click');
		}		
	});	
	
	
});

/* hotspot visited state functionality 2018 */
/*
	hotSpotVisited function sets visited state as follows:
	1.	Before selecting the hotspot: Image appears in full colour, text label in black.
	2.	Selected hotspot: Image appears in full colour, text label in black.
	3.	Visited hotspot (unselected): Image appears in visited state, text label in jewel tone of course.
	4.	Visited hotspot (selected): Image appears in full colour, text label in black.

*/

/*function hotSpotVisited(sec,total,btn){	
	for(var i = 1; i!=total+1; i++){
		if(Number($('#hotspot_btn_'+sec+'_'+i).attr('data-hotspot')) == 2){
			var imgEle, img, ext
			$('#hotspot_btn_'+sec+'_'+i).find('img').each(function(index, element) {
				imgEle = $(this);
				img = $(this).attr('src').split('.')[0];
				ext = $(this).attr('src').split('.')[1];
			});

			if(img.indexOf('_visited') == -1){
				$(imgEle).attr('src', img + '_visited.' + ext);	
			}
			
			$("#hotspot_btn_label_"+sec+"_"+i).addClass("hotspot_visited");
			
		}
	}
	
	markOtherHotSpots();
	
	var imgEle, img, ext
	$(btn).find('img').each(function(index, element) {
		imgEle = $(this);
		img = $(this).attr('src').split('.')[0];
		img = img.split('_visited')[0]
		ext = $(this).attr('src').split('.')[1];
	});

	if(img.indexOf('_visited') == -1){
		$(imgEle).attr('src', img + '.' + ext);	
	}
	var hspID = $(btn).attr('id').split('btn_')[1];
	var hspItem = hspID.split('_')[1];
	$("#hotspot_btn_label_"+sec+"_"+hspItem).removeClass("hotspot_visited");
}*/

/* hotspot visited state functionality 2018 */
/*
	hotSpotVisited function sets visited state as follows:
	1.	Before selecting the hotspot: 
	a) Icon-based hotspots will use the white-on-black appearance
	b) Photoreal hotspots will include a click target of white-on-black	
	2. Selected hotspot:
	a) Icon-based hotspots will become black-on-white with black border
	b) Photoreal hotspots will include a click target of black-on-white	
	3.	Visited hotspot (unselected and selected): Same as selected state	
	Text label will be black in all instances.
*/

function hotSpotVisited(sec,total,btn){		
	markOtherHotSpots();	
	var imgEle, img, ext
	$(btn).find('img').each(function(index, element) {
		imgEle = $(this);
		img = $(this).attr('src').split('.')[0];
		ext = $(this).attr('src').split('.')[1];
	});
	if (img.indexOf('_visited') == -1)
	{
		$(imgEle).attr('src', img + '_visited.' + ext);
	}	
	var hspID = $(btn).attr('id').split('btn_')[1];
	var hspItem = hspID.split('_')[1];
}

// Updated hotspot selected states when moving to new hottspot
function markOtherHotSpots(){
	
	$('.sec_rest_wrapper').find('.section').each(function(index, element) {
		
		if($(this).children('.hotspot_grid').hasClass('hotspot_grid')){
			
			var sec = Number($(this).attr('id').split('_')[1]);
			$(this).find('.hotspot_btn').each(function(index, element){
	
				if(Number($(this).attr('data-hotspot')) == 2){
					var btnNum = Number($(this).attr('id').split('_')[3]);
					var imgEle, img, ext
					$(this).find('img').each(function(index, element) {
						imgEle = $(this);
						img = $(this).attr('src').split('.')[0];
						ext = $(this).attr('src').split('.')[1];
					});

					if(img.indexOf('_visited') == -1){
						$(imgEle).attr('src', img + '_visited.' + ext);	
					}

					/* hotspot visited state functionality 2018 */
					/*$("#hotspot_btn_label_"+sec+"_"+btnNum).addClass("hotspot_visited");*/

				}
			});
			
		}
		
	});
	
}


/* 
	findHotspotNum function 
	
	find hotspot buttons in this section and update numberOfHotspot variable
	called when hotspot clicked
*/

function findHotspotNum(){
	
	numberOfHotspot = 0;
	$('#sec_'+document.currentSection).find('.hotspot_btn').each(function(index, element) {
       numberOfHotspot++;
    });
}

/* 
	isHotspotDone function 
	
	mark each hotspot as viewed when clicked
	called when hotspot clicked
*/

function isHotspotDone(hotspot_sec){
	secNum = hotspot_sec.split('_')[1]
	var hotspotDone = 0;
	var clicked = 0;
	$(hotspot_sec).find('.hotspot_btn').each(function(index, element) {
				
		var data = Number($(this).attr('data-hotspot'));
		if(data === Number(1)){
			hotspotDone++;
			clicked = Number($('#sec_'+secNum).attr('data-clicked'));
			clicked +=1;
			$('#sec_'+secNum).attr('data-clicked',clicked);
			$(this).attr('data-hotspot','2');
		}
    });
	
	//$('#sec_'+secNum).attr('data-clicked',clicked);
				
	var totalClicked = Number($('#sec_'+secNum).attr('data-clicked'))// + Number($('#sec_'+secNum).attr('data-links')) + Number($('#sec_'+secNum).attr('data-hotwords'));
	var totalClicks = Number(document.clickArr[Number(secNum) - 1])// + Number(document.linkArr[Number(secNum) - 1]) + Number(document.hotArr[Number(secNum) - 1])
	console.log('#sec_' + secNum + '         totalClicked = ' +totalClicked + '     totalClicks = ' + totalClicks);
	if(totalClicked == totalClicks){	
		if(isAndroidTablet || isAndroidPhone || isMacChrome){
			document.intComplete = true;
			clearInterval(document.lockinterval)
			document.lockinterval = setInterval(function(){
				che = hotspot_sec
				secNum = hotspot_sec.split('_')[1]
				document.getnear(che);
				isPageComplete(document.currentSection);
				if(($('#sec_'+(parseInt(secNum)+1)).is(':visible') || !$('#right_nav').hasClass('locked')) && document.courseLockedDown == true){
					clearInterval(document.lockinterval)
				} 
			},250)
		}else{
			document.intComplete = true;
			isPageComplete(document.currentSection);
		}
		
		document.intComplete = true;
		isPageComplete(document.currentSection);
		isHotSpotScreen = false;
	}
	
	//alert('clicked 1 = ' + clicked);
	console.log('totalClicked = ' +totalClicked + '     totalClicks = ' + totalClicks);
}

/* hotspot visited state functionality 2018 */
function markHotspotsVisited(sNum)
{	
	if($('#hotspot_btn_'+sNum+'_1').length != 0) {
		for(var i = 1; i<6; i++){
			var imgEle, img, ext
			$('#hotspot_btn_'+sNum+'_'+i).find('img').each(function(index, element) {
				imgEle = $(this);
				img = $(this).attr('src').split('.')[0];
				ext = $(this).attr('src').split('.')[1];
			});

			if(img.indexOf('_visited') == -1){
				$(imgEle).attr('src', img + '_visited.' + ext);	
			}			
		}
	}	
}	

/* hotspot banner fix */
function hotSpotAdjustBanner(){
		
	if(iOSApp){
		$('.bar_robot').removeClass('displayNone').addClass('displayBlock');
		var pageTop = $(parent.parent.document).scrollTop();
		var bannerTop = parseInt($('.bar_robot').css('top'));
		var posDiff = bannerTop - pageTop;
		var newBanPos = bannerTop - posDiff;
		if(posDiff > 0){
			$('.bar_robot').css('top',(newBanPos)+'px');
			$('.bar_robot').removeClass('displayNone').addClass('displayBlock');
		}
	}
}

$('.hotspot_btn').attr('data-hotspot',0);