/* functions to control slider interactivities */

var numberOfSlides;
var sliderNum; /* updated to calculate what page slider is on */
var currSlideArr = [];
$( document ).ready(function() {	
	/* fix for slider jump on desktop, when viewed from saba cloud */
	if(isWeb || isAndroidTablet){ /* updated for android tablet */
		if(window.top !== window.self) {
			$('.slider_container').css('height','463px');
		}
	}	
	
	$('.slide_right').click(function(event){
		/* slider visited state functionality 2018 */
		if(!$(this).hasClass('disabled') && !$(this).hasClass('visited_disabled')) {
			var slider_sec = '#'+$(event.target).closest('.section').attr('id');
			sliderNum = $(this).parent().attr('id').split('_')[1]; /* updated to calculate what page slider is on */
			findSliderNum(sliderNum); /* updated to calculate what page slider is on */
			lockSlider(slider_sec, 'right')
			var currSlide = 0;
			var secNum = $(this).parent().attr('id').split('_')[1];
			if (currSlideArr[secNum-1] == undefined) {
				currSlideArr[secNum-1] = 1;
				currSlide = 1;
			}		
			else {
				currSlide = currSlideArr[secNum-1];
			}		
			var currSlideID = '#slider_' + secNum + '_' + currSlide;
			var	nextSlideID = '';
			var currFocusID = '#'+$('#temp_slide_nums_' + secNum + ' .focus').attr('id');
			var nextFocusID = '';
			$(currSlideID).removeClass('slider_current');	
			$(currSlideID).addClass('slider_hidden');
			$(currSlideID).attr('style','');
			currSlide++;
			if (currSlide == (numberOfSlides + 1)) {
				currSlide = 1;
			}	
			currSlideArr[secNum-1] = currSlide;
			nextSlideID = '#slider_' + secNum + '_' + currSlide;
			//alert('nextSlideID = ' + nextSlideID); 
			nextFocusID = '#foc_' + secNum + '_' + currSlide;
			/* 2021 Accessibility Update */
			$(nextSlideID).fadeIn(200,function(){$(this).focus();}); /* add focus to content area */
			$(nextSlideID).removeClass('slider_hidden');
			$(nextSlideID).addClass('slider_current');
			$(currFocusID).removeClass('focus');
			$(nextFocusID).addClass('focus');
			if(Number($(currSlideID).attr('data')) == 0){		
				$(currSlideID).attr('data',1);
			}
			if(Number($(nextSlideID).attr('data')) == 0){
				$(nextSlideID).attr('data',1);
			}		
			//setTimeout(function(){
				isSliderDone(slider_sec);
			//},250);
			/* hotspot banner fix */
			if(isiPad){				
				setTimeout(function(){hotSpotAdjustBanner();},100);
			}
			/* hotspot banner fix */
			if(isAndroidTablet || isAndroidPhone || isMacChrome){
				$('html,body').scrollTop(windowTop -1)
			}
			event.stopImmediatePropagation();
			}
	})
	
	$('.slide_left').click(function(event){
		/* slider visited state functionality 2018 */
		if(!$(this).hasClass('disabled') && !$(this).hasClass('visited_disabled')) {
			var slider_sec = '#'+$(event.target).closest('.section').attr('id');
			sliderNum = $(this).parent().attr('id').split('_')[1]; /* updated to calculate what page slider is on */
			findSliderNum(sliderNum); /* updated to calculate what page slider is on */
			lockSlider(slider_sec, 'left')
			var currSlide = 0;
			var secNum = $(this).parent().attr('id').split('_')[1];
			if (currSlideArr[secNum-1] == undefined) {
				currSlideArr[secNum-1] = 1;
				currSlide = 1;
			}
			else {
				currSlide = currSlideArr[secNum-1];
			}		
			var currSlideID = '#slider_' + secNum + '_' + currSlide;
			var	prevSlideID = '';
			var currFocusID = '#'+$('#temp_slide_nums_' + secNum + ' .focus').attr('id');
			var prevFocusID = '';
			$(currSlideID).removeClass('slider_current');	
			$(currSlideID).addClass('slider_hidden');
			$(currSlideID).attr('style','');
			currSlide--;
			if (currSlide == 0) {
				currSlide = numberOfSlides;
			}	
			currSlideArr[secNum-1] = currSlide;
			prevSlideID = '#slider_' + secNum + '_' + currSlide;
			//alert('prevSlideID = ' + prevSlideID)
			prevFocusID = '#foc_' + secNum + '_' + currSlide;
			/* 2021 Accessibility Update */
			$(prevSlideID).fadeIn(200,function(){$(this).focus();}); /* add focus to content area */
			$(prevSlideID).removeClass('slider_hidden');
			$(prevSlideID).addClass('slider_current');
			$(currFocusID).removeClass('focus');
			$(prevFocusID).addClass('focus');
			if(Number($(prevSlideID).attr('data')) == 0){
				$(prevSlideID).attr('data',1);
			}
			if(Number($(currSlideID).attr('data')) == 0){
				$(currSlideID).attr('data',1);	
			}
			//setTimeout(function(){
				isSliderDone(slider_sec);
			//},250);
			/* hotspot banner fix */
			if(isiPad){				
				setTimeout(function(){hotSpotAdjustBanner();},100);
			}
			/* hotspot banner fix */
			if(isAndroidTablet || isAndroidPhone || isMacChrome){
				$('html,body').scrollTop(windowTop -1)
			}
			event.stopImmediatePropagation();
		}
	})	
	
	/* added keyboard accessibility */
	$('.slide_left, .slide_right').keypress(function (e) {
		if (e.keyCode == 13 || e.keyCode == 32) {
			
			$(this).trigger('click');
		}		
	});
	
	var keyFocus = false;
    $(document).keyup(function (e) { keyFocus = true; });
    $(document).click(function (e) { keyFocus = false; });
	
	$('.slide_left, .slide_right').focus(function () {
        if (keyFocus) {
            $(this).addClass('hover')
        }
    });
	
    $('.slide_left, .slide_right').focusout(function () {
        if (keyFocus) {
            $(this).removeClass('hover')
        }
    });
	
	$('.slide_left, .slide_right').hover(
		function() {
			$(this).addClass('hover')
		}, function() {
			$(this).removeClass('hover')
		}
	);
})


function linearSlider(){
	if(document.sliderLinear == true){
		$('.slide_left').addClass('disabled')
	}
	
}
linearSlider()
function lockSlider(section, clicked){
	/* slider visited state functionality 2018 */
	var secNum = section.split('_')[1];
	if(document.sliderLinear == true){		
		var slides = ($(section+ ' .slider_txt').length) - 1;
		var next = $(section+ ' .slider_txt.slider_current').nextAll('.slider_txt').index();
		var before = $(section+ ' .slider_txt.slider_current').prevAll('.slider_txt').index();
		if(clicked == 'right'){
			$(section+' .slide_left').removeClass('disabled');
			/* slider visited state functionality 2018 */
			$(section+' .slide_left').removeClass('visited_disabled');
			/* 2021 Accessibility Update */
			$(section+' .slide_left').attr('title', localiseString('slide_left'));
			$(section+' .slide_left').attr('alt', localiseString('slide_left'));
			/* slider visited state functionality 2018 */
			if ((document.trackingArr[secNum - 1] == 1) || $(section+' .slide_right').hasClass('visited'))
			{					
				$(section+' .slide_left').addClass('visited');
				$(section+' .slide_right').addClass('visited');
			}
			if(next == slides){
				/* slider visited state functionality 2018 */
				if ((document.trackingArr[secNum - 1] == 1) || $(section+' .slide_right').hasClass('visited'))
				{					
					$(section+' .slide_right').removeClass('visited');
					$(section+' .slide_right').addClass('visited_disabled');
					/* 2021 Accessibility Update */
					$(section+' .slide_right').attr('title', localiseString('slide_right_locked'));
					$(section+' .slide_right').attr('alt', localiseString('slide_right_locked'));
				}
				else
				{
					$(section+' .slide_right').addClass('disabled');
					/* 2021 Accessibility Update */
					$(section+' .slide_right').attr('title', localiseString('slide_right_locked'));
					$(section+' .slide_right').attr('alt', localiseString('slide_right_locked'));
				}
			}
		}
		if(clicked == 'left'){			
			$(section+' .slide_right').removeClass('disabled');
			/* slider visited state functionality 2018 */
			$(section+' .slide_right').removeClass('visited_disabled');
			/* 2021 Accessibility Update */
			$(section+' .slide_right').attr('title', localiseString('slide_right'));
			$(section+' .slide_right').attr('alt', localiseString('slide_right'));
			/* slider visited state functionality 2018 */
			if ((document.trackingArr[secNum - 1] == 1) || $(section+' .slide_left').hasClass('visited'))
			{					
				$(section+' .slide_right').addClass('visited');
				$(section+' .slide_left').addClass('visited');
			}	
			if(before == 0){
				/* slider visited state functionality 2018 */
				if ((document.trackingArr[secNum - 1] == 1) || $(section+' .slide_left').hasClass('visited'))
				{					
					$(section+' .slide_left').removeClass('visited');
					$(section+' .slide_left').addClass('visited_disabled');
					/* 2021 Accessibility Update */
					$(section+' .slide_left').attr('title', localiseString('slide_left_locked'));
					$(section+' .slide_left').attr('alt', localiseString('slide_left_locked'));
				}
				else
				{
					$(section+' .slide_left').addClass('disabled');
					/* 2021 Accessibility Update */
					$(section+' .slide_left').attr('title', localiseString('slide_left_locked'));
					$(section+' .slide_left').attr('alt', localiseString('slide_left_locked'));
				}				
			}
		}
	}
}
	

/* updated to calculate what page slider is on */
function findSliderNum(slide){	
	numberOfSlides = 0;
	$('#sec_'+slide).find('.slider_txt').each(function(index, element) {
       numberOfSlides++;
    });
}

function isSliderDone(slider_sec){
	var secNum = document.currentSection; //slider_sec.split('_')[1]
	var sliderDone = 0;
	var clicked = 0;
	var isSlider = false;
	$('#sec_'+secNum).find('.slider_txt').each(function(index, element) {
		isSlider = true;
		var data = Number($(this).attr('data'));
		if(data === Number(1)){
			isSlider = true;
			sliderDone++;
			clicked = Number($('#sec_'+document.currentSection).attr('data-clicked'));
			clicked +=1;
			if(isSlider){
				$('#sec_'+secNum).attr('data-clicked',clicked);
			}
			$(this).attr('data','2');
		}		
    });	
	
	var totalClicked = 0; 
	var totalClicks = 0; 
	var clickArr = [];
	if(isNaN(document.clickArr[Number(secNum) - 1])){
		clickArr = document.clickArr[Number(secNum) - 1].split(',');
		//alert(clickArr);
	}
	
	if($('#sec_'+secNum).find('.slider_txt').length !== 0 && $('#sec_'+secNum).find('.temp_slide_nums').is(':hidden') && $('.sec_rest_wrapper').is(':visible')){
		numberOfSlides = 0;
		//totalClicked = Number($('#sec_'+secNum).attr('data-links')) + Number($('#sec_'+secNum).attr('data-hotwords'));
		//totalClicks = Number(document.linkArr[Number(secNum) - 1]) + Number(document.hotArr[Number(secNum) - 1]);
		//totalClicks = 0;		
		totalClicked = Number($('#sec_'+secNum).attr('data-clicked'))
		totalClicks = Number(clickArr[1]) + Number(clickArr[2])		
	}else{
		totalClicked = Number($('#sec_'+secNum).attr('data-clicked'))// + Number($('#sec_'+secNum).attr('data-links')) + Number($('#sec_'+secNum).attr('data-hotwords'));
		totalClicks = Number(clickArr[0]) + Number(clickArr[1]) + Number(clickArr[2])// + Number(document.linkArr[Number(secNum) - 1]) + Number(document.hotArr[Number(secNum) - 1]);
	}
	
	if(totalClicked == totalClicks && document.trackingArr[secNum - 1] !== 1 && $('#sec_'+secNum).find('.slider_txt').length !== 0){
		if((isAndroidTablet || isAndroidPhone) && typeof slider_sec !== "undefined"){
			document.intComplete = true;
			clearInterval(document.lockinterval)
			document.lockinterval = setInterval(function(){
				che = slider_sec
				secNum = slider_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)
					console.log('slide')
				} 
			},250)
		}else{
			document.intComplete = true;
			isPageComplete(document.currentSection);
		}
	}
	getSliderHeight();			   
}

$('.slider_txt').attr('data',0);

/* slider visited state functionality 2018 */
function markSliderVisited(sNum)
{
	if ($('#sec_'+sNum+' .slide_left').hasClass('disabled'))
	{
		$('#sec_'+sNum+' .slide_left').addClass('visited_disabled');
	}
	if (!$('#sec_'+sNum+' .slide_left').hasClass('visited_disabled'))
	{
		$('#sec_'+sNum+' .slide_left').addClass('visited');
	}	
	if ($('#sec_'+sNum+' .slide_right').hasClass('disabled'))
	{
		$('#sec_'+sNum+' .slide_right').addClass('visited_disabled');
	}
	if (!$('#sec_'+sNum+' .slide_right').hasClass('visited_disabled'))
	{
		$('#sec_'+sNum+' .slide_right').addClass('visited');
	}	
}		

// fix for slider height
function getSliderHeight(){
	
	var fontSize = 0;	
	$('.section').find('.slider_container').each(function(index, element) {
		var slideHeight = 463;
		$(this).find('.slider_txt').each(function(index, element) {
					
			var elem =  document.getElementById($(this).attr('id'));
			var theCSSprop = window.getComputedStyle(elem,null).getPropertyValue("font-size"); /*$(elem).css('font-size');*/
			theCSSprop = Number(theCSSprop.split('px')[0]);
			
			if(theCSSprop > fontSize){
				fontSize = theCSSprop;
			}	
			
			var newHeight = $(this).innerHeight();
			if(newHeight > slideHeight){
				
				slideHeight = newHeight;
			}
		});
		//alert('slideHeight =' + slideHeight);
		$(this).css('height', slideHeight+'px');
	});
	
	/*var containHeight = $('.slider_container').css('height');
	containHeight = Number(containHeight.split('px')[0]);
	if(fontSize > 18){
		var newContainHeight = 	(containHeight/2) + containHeight;
		$('.slider_container').css('height', Math.round(newContainHeight)+'px');
	}*/
}


// if slider has no locked hotspots or links unlock that screen for phones
function sliderPhone(){
	
	for(var i = 0; i!=document.clickArr.length; i++){
	
		var clickArr = [];
		var totalClicks = 0;
		if(isNaN(document.clickArr[i])){
			clickArr = document.clickArr[i].split(',');
			totalClicks = Number(clickArr[1]) + Number(clickArr[2])	;
			if(totalClicks == 0){
				document.lockedArr[i] = 0;
			}
		}
	}
	//alert('document.lockedArr = ' + document.lockedArr);
}
if(isiPhone || isAndroidPhone){
	sliderPhone();
}