/* 20240130 handling of src possibly returning full url from some browsers in LMS: if hostpot screentype is used in future then this code will need editing for this */

/* 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_label').addClass('hotspot_btn_label_cutoff');
			$('.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;
		//alert('totalHotspots = ' + totalHotspots)
		var hspID = $(this).attr('id').split('btn_')[1];		
		var hspSec = hspID.split('_')[0];
		var hspItem = hspID.split('_')[1];
		var hotspotIncomp = false;
		var hotspot_sec = '#sec_' + hspSec;//'#'+$(e.target).closest('.section').attr('id');
		var totalHotspots = $('#sec_' + hspSec + ' .hotspot_btn').length;
		//alert('totalHotspots = ' + totalHotspots)
		//var hspID = $(this).attr('id').split('btn_')[1];		
		//var hspSec = hspID.split('_')[0];
		//var hspItem = hspID.split('_')[1];
		//var hotspotIncomp = false;
		//var hotspot_sec = hspSec;//'#'+$(e.target).closest('.section').attr('id');
		//if(document.useTransitions){
			/* transitions: scale up/down hotspot button images */
			currHotSpotItem = hspID;
		//}	
		
		// updated to show content when more than 3 hotspots
		var hsRow = Number($(this).parent().parent().attr('class').split('_')[1]);
		
		//$('.hotspot_btn_label').addClass('hotspot_btn_label_cutoff');
		$('#hotspot_btn_label_' + hspID).removeClass('hotspot_btn_label_cutoff');
		$('#hotspot_btn_label_' + hspID).children().removeClass('hotspot_btn_label_cutoff');
		//$('#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();	
			
			// 20190610 functionality to change background reveal colour
			var hsRevealNum = $('#hotspot_reveal_'+hspID).attr('class').split('_')[3];
			var hsRevalClass = 'hotspot_reveal_'+hsRevealNum;
			$('#hotspot_reveal_' + hspSec + '_lg').removeClass('hotspot_reveal_1');
			$('#hotspot_reveal_' + hspSec + '_lg').removeClass('hotspot_reveal_2');
			$('#hotspot_reveal_' + hspSec + '_lg').removeClass('hotspot_reveal_3');
			$('#hotspot_reveal_' + hspSec + '_lg').removeClass('hotspot_reveal_4');
			$('#hotspot_reveal_' + hspSec + '_lg').removeClass('hotspot_reveal_5');
			$('#hotspot_reveal_' + hspSec + '_lg').removeClass('hotspot_reveal_6');
			$('#hotspot_reveal_' + hspSec + '_lg_2').removeClass('hotspot_reveal_1');
			$('#hotspot_reveal_' + hspSec + '_lg_2').removeClass('hotspot_reveal_2');
			$('#hotspot_reveal_' + hspSec + '_lg_2').removeClass('hotspot_reveal_3');
			$('#hotspot_reveal_' + hspSec + '_lg_2').removeClass('hotspot_reveal_4');
			$('#hotspot_reveal_' + hspSec + '_lg_2').removeClass('hotspot_reveal_5');
			$('#hotspot_reveal_' + hspSec + '_lg_2').removeClass('hotspot_reveal_6');
			// 20190610 functionality to change background reveal colour	
			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 
				/* 20240130 accessibility 2024 */
				$('#hotspot_reveal_' + hspSec + '_lg').attr('aria-hidden', false);
                $('#hotspot_reveal_' + hspSec + '_lg .hotspot_close').attr('tabindex', Number(hotTabInd) +1);  // 20190206 hotspot close functionality
				/* 20240130 accessibility 2024 */
				$('#hotspot_reveal_' + hspSec + '_lg .hotspot_close').attr('aria-hidden', false);
                $('#hotspot_reveal_' + hspSec + '_lg .hotspot_close').attr('role', 'button');
				$('#hotspot_reveal_' + hspSec + '_lg').addClass(hsRevalClass); // 20190610 functionality to change background reveal colour
				
			}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
				/* 20240130 accessibility 2024 */
				$('#hotspot_reveal_' + hspSec + '_lg_2').attr('aria-hidden', false);
                $('#hotspot_reveal_' + hspSec + '_lg_2 .hotspot_close').attr('tabindex', Number(hotTabInd) +1); // 20190206 hotspot close functionality
				/* 20240130 accessibility 2024 */
				$('#hotspot_reveal_' + hspSec + '_lg_2 .hotspot_close').attr('aria-hidden', false);
                $('#hotspot_reveal_' + hspSec + '_lg_2 .hotspot_close').attr('role', 'button');
				$('#hotspot_reveal_' + hspSec + '_lg_2').addClass(hsRevalClass); // 20190610 functionality to change background reveal colour
			}
			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);
							});
						}else{
							$('#hotspot_reveal_' + hspSec + '_lg_2').show('blind',500, function(){
								$('#hotspot_txt_'+hspSec+'_'+hspID).show('blind',500);
							});

						}
						currHotSpot = hspSec;
						lastHotSpotItem = hspID;
					}else{
						if(hsRow == 1){		
							$('#hotspot_reveal_' + hspSec + '_lg').show();
						}else{
							$('#hotspot_reveal_' + hspSec + '_lg_2').show();
						}
						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();
					}else{
						$('#hotspot_reveal_' + hspSec + '_lg_2').show();
					}
					$('#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();
				}else{
					$('#hotspot_reveal_' + hspSec + '_lg_2').show();
				}
				$('#hotspot_txt_'+hspSec+'_'+hspID).show();
			}
		/*}
		else {			
			$('#hotspot_reveal_' + hspID).show();	
			var hotTabInd = $('#hotspot_reveal_' + hspID).attr('tabindex');
			$('#hotspot_reveal_' + hspSec + '_' + hspID + ' .hotspot_close').attr('tabindex', Number(hotTabInd) +1);  // 20190206 hotspot close functionality
            $('#hotspot_reveal_' + hspSec + '_' + hspID + ' .hotspot_close').attr('role', 'button');
		}*/
		
		
		
		var btnTop = $(this).position().top;
		
		//console.log('button top = ' + btnTop);
		
		//$('#hotspot_reveal_' + hspID).show();	
		/*var hotTabInd = $('#hotspot_reveal_' + hspID).attr('tabindex');
		$('#hotspot_reveal_' + hspSec + '_' + hspID + ' .hotspot_close').attr('tabindex', Number(hotTabInd) +1);  // 20190206 hotspot close functionality
		$('#hotspot_reveal_' + hspSec + '_' + hspID + ' .hotspot_close').attr('role', 'button');*/
		
		
		if(hsRow == 1){
		
			$('#hotspot_reveal_' + hspSec + '_lg').closest('.hotspot_container').css({
				'position' : 'absolute',
				'top' : btnTop,
				'z-index':200
			});
		}else{
			$('#hotspot_reveal_' + hspSec + '_lg_2').closest('.hotspot_container').css({
				'position' : 'absolute',
				'top' : btnTop,
				'z-index':200
			});
		}
		
		
		
		$(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');
		}		
	});	
    
    // 20190206 hotspot close functionality
    
    $('.hotspot_close').click(function(e){
        
        var hotSpotNum = currHotSpotItem.split("_")[1];
        var hspSec = currHotSpotItem.split("_")[0];
        
        //$('.hotspot_btn_label').addClass('hotspot_btn_label_cutoff');

        
        $('.hotspot_btn_ind').hide();
		 $('#hotspot_reveal_' + hspSec + '_lg').hide();
			$('#hotspot_reveal_' + hspSec + '_lg_2').hide();
        
        /*if ($('.hotspot_nodiv').css('display') == "block") {
			
			// updated close funcitonality due to new functionality for more than 3 hotspots
			
            $('#hotspot_reveal_' + hspSec + '_lg').hide();
			$('#hotspot_reveal_' + hspSec + '_lg_2').hide();
            
        }else{
            $('#hotspot_reveal_' + currHotSpotItem).hide();
        }*/
		$('#hotspot_reveal_' + currHotSpotItem).hide();
        $("#hotspot_btn_label_"+hspSec+"_"+hotSpotNum).addClass("hotspot_visited");
        var imgEle, img, ext
        $('#hotspot_btn_'+hspSec+'_'+hotSpotNum).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_"+currHotSpotItem).focus(); 
		
		$('.hotspot_btn').css('cursor','pointer');
    });
	
    $('.hotspot_close').keypress(function (e) {
		if (e.keyCode == 13 || e.keyCode == 32) {
			
			$(this).trigger('click');
		}		
	});	
    
    // 20190206 hotspot close functionality
	
});

/*
	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");
}

// 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_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);
				isPageComplete(secNum);
				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);
			isPageComplete(secNum);
		}
		
		document.intComplete = true;
		//isPageComplete(document.currentSection);
		isPageComplete(secNum);
		isHotSpotScreen = false;
	}
	
	//alert('clicked 1 = ' + clicked);
	//console.log('totalClicked = ' +totalClicked + '     totalClicks = ' + totalClicks);
}
/* 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);