/* functions to control slider interactivities */

var numberOfSlides;
var sliderNum; /* updated to calculate what page slider is on */
var currSlideArr = [];
var slideAnimating = false;

var slideFocus = "";

var slidePrev = "";

$( 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','511px');
		}
	}	
	
	$(".slider_container").on('transitionend', function () {
		slideTransitionEnd();
	});

	$('.sec_rest_wrapper').find('.section').each(function(index, element) {
		
		$(this).find('.slider_txt').each(function(index, element) {
			
			element.style.order = index+1;
		});
		
	});
	
	
	$('.slide_right').click(function(event){
		if(!$(this).hasClass('disabled')) {
			
			if(!slideAnimating){
				slideAnimating = true;
				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];
				}		
				//console.log('currSlide = ' + currSlide + '   currSlideArr = ' + currSlideArr);
				var currSlideID = '#slider_' + secNum + '_' + currSlide;
				var	nextSlideID = '';
				var currFocusID = '#'+$('#temp_slide_nums_' + secNum + ' .focus').attr('id');
				var nextFocusID = '';
					
				


				currSlide++;
				if (currSlide == (numberOfSlides + 1)) {
					currSlide = numberOfSlides//1;
				}	
				currSlideArr[secNum-1] = currSlide;
				nextSlideID = '#slider_' + secNum + '_' + currSlide;
				nextFocusID = '#foc_' + secNum + '_' + currSlide;
				
				slideFocus =  nextSlideID;
				slidePrev = currSlideID;
				
				$(nextSlideID).removeClass('slider_hidden');
				$(nextSlideID).addClass('slider_current');
				
				/* 20240130 accessibility 2024 */
				$(currSlideID).find('a').each(function(index, element) {
					$(this).attr('tabindex', -1);
					$(this).attr('aria-hidden', true);
				});
				
				// 20221025 JAWs Slider Fix
				$(currSlideID).find('img').each(function(index, element) {
					if ($(this).attr('alt') != ''){
						$(this).attr('tabindex', -1);
						/* 20240130 accessibility 2024 */
						$(this).attr('aria-hidden', true);
					}
				});
				
				//document.querySelector("#slider_container_"+secNum).classList.add('slider_container_transition');
				//$("#slider_container_"+secNum).addClass('slider_container_transition');
				
				if(document.textDirection == "rtl"){
					//document.querySelector("#slider_container_"+secNum).style.transform = 'translateX('+(currSlide-1)+'00%)';
					
					if(isiPhone){
						$(currSlideID).removeClass('slider_current');	
						$(currSlideID).addClass('slider_hidden');
						
						$(nextSlideID).addClass('slider_hidden');
						/*$(nextSlideID).fadeIn(200,function(){
							$(this).focus();
							slideTransitionEnd();
						});*/
						$(nextSlideID).removeClass('slider_hidden');
						$(nextSlideID).addClass('slider_current');
						
						
						slideTransitionEnd();
						
					}else{
											
						if (currSlide == (numberOfSlides + 1)) {
							$("#slider_container_"+secNum).css('transform','translateX(100%)');
						}else{
							$("#slider_container_"+secNum).css('transform','translateX('+(currSlide-1)+'00%)');
						}
					}
				}else{
					//document.querySelector("#slider_container_"+secNum).style.transform = 'translateX(-'+(currSlide-1)+'00%)';
					$("#slider_container_"+secNum).css('transform','translateX(-'+(currSlide-1)+'00%)');
				}
				
				//$(currSlideID).removeClass('slider_current');	
				//$(currSlideID).addClass('slider_hidden');
				
				// 20221018 JAWs Slider Fix
				/* 20240130 accessibility 2024 */
				var tabInd1 = Number($(nextSlideID).find('.slider_start').attr('data-tab'));
				var tabInd2 = Number($(nextSlideID).find('.slider_rest').attr('data-tab'));
				
				$(nextSlideID).find('.slider_start').attr('aria-hidden', false);
				$(nextSlideID).find('.slider_start').attr('tabindex', tabInd1);
				$(nextSlideID).find('.slider_rest').attr('aria-hidden', false);
				$(nextSlideID).find('.slider_rest').attr('tabindex', tabInd2);
				// 20221018 JAWs Slider Fix
				
				/* 20240130 accessibility 2024 */
				$(nextSlideID).find('a').each(function(index, element) {
					$(this).attr('tabindex', $(this).attr('data-tab'));
					$(this).attr('aria-hidden', false);
				});
				
				// 20221025 JAWs Slider Fix
				$(nextSlideID).find('img').each(function(index, element) {
					if ($(this).attr('alt') != ''){
						$(this).attr('tabindex', $(this).attr('data-tab'));
						/* 20240130 accessibility 2024 */
						$(this).attr('aria-hidden', false);
					}
				});
				
				
				
				$(currFocusID).removeClass('focus');
				/* client learning design specs 2025 v4.3 */
				$(currFocusID).removeClass('slide_current_complete');
				$(currFocusID).addClass('slide_complete');
				$(nextFocusID).addClass('focus');
				if(Number($(currSlideID).attr('data')) == 0){		
					$(currSlideID).attr('data',1);
				}
				if(Number($(nextSlideID).attr('data')) == 0){
					$(nextSlideID).attr('data',1);
				}
				/* client learning design specs 2025 v4.3 */
				else {
					$(nextFocusID).removeClass('slide_complete');
					$(nextFocusID).addClass('slide_current_complete');
				}
				event.stopImmediatePropagation();
			}
		}
	})
	
	function slideTransitionEnd(){
		slideAnimating = false;
		//document.querySelector("#slider_container_"+sliderNum).classList.remove('slider_container_transition');
		
		//$("#slider_container_"+sliderNum).removeClass('slider_container_transition');
		
		/* 20240130 accessibility 2024 */
		/* slider full width slide accessibility 2025 */
		$(slideFocus).find('.slider_full').eq(0).find('.slider_start').eq(0).focus();
		$(slideFocus).find('img').each(function(index, element) {
			if ($(this).attr('alt') != ''){
				$(this).focus();
			}
			else {
				$(slideFocus).find('.slider_start').focus();
			}
		});
		
		//$(slidePrev).removeClass('slider_current');	
		//$(slidePrev).addClass('slider_hidden');
		
		// 20221018 JAWs Slider Fix
		/* 20240130 accessibility 2024 */
		$(slidePrev).find('.slider_start').attr('aria-hidden', true);
		$(slidePrev).find('.slider_rest').attr('aria-hidden', true);
		$(slidePrev).find('.slider_start').attr('tabindex', -1);
		$(slidePrev).find('.slider_rest').attr('tabindex', -1);
		$(slidePrev).find('a').each(function(index, element) {
			$(this).attr('tabindex', -1);
			$(this).attr('aria-hidden', true);
		});
		$(slidePrev).find('img').each(function(index, element) {
			$(this).attr('tabindex', -1);
			$(this).attr('aria-hidden', true);
		});
		// 20221018 JAWs Slider Fix
		// 20221018 JAWs Slider Fix
		if(currSlideArr[sliderNum-1] == 1){
			$('#slider_'+sliderNum+' .slide_left').addClass('disabled')
			$('#slider_'+sliderNum+' .slide_left').attr('title', localiseString('left_nav_locked'));
			$('#slider_'+sliderNum+' .slide_left').attr('alt', localiseString('left_nav_locked'));
		}else{
			$('#slider_'+sliderNum+' .slide_left').attr('title', localiseString('slide_left'));
			$('#slider_'+sliderNum+' .slide_left').attr('alt', localiseString('slide_left'));
		}
		if(currSlideArr[sliderNum-1] == numberOfSlides){
			$('#slider_'+sliderNum+' .slide_right').addClass('disabled')
			$('#slider_'+sliderNum+' .slide_right').attr('title', localiseString('right_nav_locked'));
			$('#slider_'+sliderNum+' .slide_right').attr('alt', localiseString('right_nav_locked'));
		}else{
			$('#slider_'+sliderNum+' .slide_right').attr('title', localiseString('slide_right'));
			$('#slider_'+sliderNum+' .slide_right').attr('alt', localiseString('slide_right'));
		}
		// 20221018 JAWs Slider Fix
		
		/* REMOVE 2023
		var tempStr = $("#sec_"+sliderNum).css("background-image").split("slider")[1];
		var tempArr = tempStr.split("_")
		var ext = tempStr.split(".")[1];
		$("#sec_"+sliderNum).css("background-image","url(images/slider_"+tempArr[1]+"_"+tempArr[2]+"_"+currSlideArr[sliderNum-1]+".png)");
		*/
		getSliderHeight();
		
		isSliderDone(sliderNum);
	}
	
	$('.slide_left').click(function(event){
		if(!$(this).hasClass('disabled')) {
			
			if(!slideAnimating){
				slideAnimating = true;
			
				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 = '';

			
				currSlide--;
				if (currSlide == 0) {
					currSlide = 0//numberOfSlides;
				}	
				currSlideArr[secNum-1] = currSlide;
				prevSlideID = '#slider_' + secNum + '_' + currSlide;
				prevFocusID = '#foc_' + secNum + '_' + currSlide;
				
				slideFocus =  prevSlideID;
				slidePrev = currSlideID;
				
				//document.querySelector("#slider_container_"+secNum).classList.add('slider_container_transition');
				//$("#slider_container_"+secNum).addClass('slider_container_transition');
				
				/* 20240130 accessibility 2024 */
				$(currSlideID).find('a').each(function(index, element) {
					$(this).attr('tabindex', -1);
					$(this).attr('aria-hidden', true);
				});
				
				// 20221025 JAWs Slider Fix
				$(currSlideID).find('img').each(function(index, element) {
					if ($(this).attr('alt') != ''){
						$(this).attr('tabindex', -1);
						/* 20240130 accessibility 2024 */
						$(this).attr('aria-hidden', true);
					}
				});
				
				if(document.textDirection == "rtl"){
					
					
					if(isiPhone){
						$(currSlideID).removeClass('slider_current');	
						$(currSlideID).addClass('slider_hidden');
						
						$(prevSlideID).addClass('slider_hidden');
						/*$(nextSlideID).fadeIn(200,function(){
							$(this).focus();
							slideTransitionEnd();
						});*/
						$(prevSlideID).removeClass('slider_hidden');
						$(prevSlideID).addClass('slider_current');
						slideTransitionEnd();
						
					}else{
				
						if(currSlide == 0){
							//document.querySelector("#slider_container_"+secNum).style.transform = 'translateX(0%)';
							$("#slider_container_"+secNum).css('transform','translateX(0%)');
						}else{
							//document.querySelector("#slider_container_"+secNum).style.transform = 'translateX('+(currSlide-1)+'00%)';
							$("#slider_container_"+secNum).css('transform','translateX('+(currSlide-1)+'00%)');
						}
					}
				}else{
					if(currSlide == 0){
						//document.querySelector("#slider_container_"+secNum).style.transform = 'translateX(0%)';
						$("#slider_container_"+secNum).css('transform','translateX(0%)');
					}else{
						//document.querySelector("#slider_container_"+secNum).style.transform = 'translateX(-'+(currSlide-1)+'00%)';
						$("#slider_container_"+secNum).css('transform','translateX(-'+(currSlide-1)+'00%)');
					}
				}
				
				// 20221018 JAWs Slider Fix
				/* 20240130 accessibility 2024 */
				var tabInd1 = Number($(prevSlideID).find('.slider_start').attr('data-tab'));
				var tabInd2 = Number($(prevSlideID).find('.slider_rest').attr('data-tab'));
				
				$(prevSlideID).find('.slider_start').attr('aria-hidden', false);
				$(prevSlideID).find('.slider_start').attr('tabindex', tabInd1);
				$(prevSlideID).find('.slider_rest').attr('aria-hidden', false);
				$(prevSlideID).find('.slider_rest').attr('tabindex', tabInd2);
				// 20221018 JAWs Slider Fix
				
				/* 20240130 accessibility 2024 */
				$(prevSlideID).find('a').each(function(index, element) {
					$(this).attr('tabindex', $(this).attr('data-tab'));
					$(this).attr('aria-hidden', false);
				});
				
				// 20221025 JAWs Slider Fix
				$(prevSlideID).find('img').each(function(index, element) {
					if ($(this).attr('alt') != ''){
						$(this).attr('tabindex', $(this).attr('data-tab'));
						/* 20240130 accessibility 2024 */
						$(this).attr('aria-hidden', false);
					}
				});
				
				$(currFocusID).removeClass('focus');
				/* client learning design specs 2025 v4.3 */
				$(currFocusID).removeClass('slide_current_complete');
				$(currFocusID).addClass('slide_complete');
				$(prevFocusID).addClass('focus');
				if(Number($(prevSlideID).attr('data')) == 0){
					$(prevSlideID).attr('data',1);
				}
				/* client learning design specs 2025 v4.3 */
				else {
					$(prevFocusID).removeClass('slide_complete');
					$(prevFocusID).addClass('slide_current_complete');
				}
				if(Number($(currSlideID).attr('data')) == 0){
					$(currSlideID).attr('data',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')
		}
	);
	
	
	/*
	$(".slider_wrapper").on("swipeleft",function(){
		//alert("hi")
		$('.slide_right').trigger('click')
  	}); 	
	
	$(".slider_wrapper").on("swiperight",function(){
		$('.slide_left').trigger('click')
  	}); 
	*/
	
})


function linearSlider(){
	if(document.sliderLinear == true){
		$('.slide_left').addClass('disabled')
	}
	
}
linearSlider()
function lockSlider(section, clicked){
	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');
			if(next == slides){
				$(section+' .slide_right').addClass('disabled');
			}
		}
		if(clicked == 'left'){
			$(section+' .slide_right').removeClass('disabled');
			if(before == 0){
				$(section+' .slide_left').addClass('disabled');
			}
		}
	}
}
	

/* 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-clicked'))
		totalClicks = Number(clickArr[1]) + Number(clickArr[2])		
	}else{
		totalClicked = Number($('#sec_'+secNum).attr('data-clicked'))
		totalClicks = Number(clickArr[0]) + Number(clickArr[1]) + Number(clickArr[2])
	}
	
	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);
				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);
		}
	}
}

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

// fix for slider height
function getSliderHeight(){
	
	//console.log("getSliderHeight")
	var fontSize = 0;	
	$('.section').find('.slider_container').each(function(index, element) {
		
		var slideHeight = 511;
		
		$(this).find('.slider_txt').each(function(index, element) {
					
			var newHeight;
			
			var parentDiv = $(this);
			if($(this).is(":visible")){
			
				$(this).find('.slide_end').each(function(index, element) {
					newHeight = $(this).offset().top - $(parentDiv).offset().top;
					//console.log("$(this).offset().top = " + $(this).offset().top);
					//console.log("$(parentDiv).offset().top = " + $(parentDiv).offset().top)
					//console.log("newHeight newHeight = " + newHeight)

				});
			}
			//console.log('slideHeight = ' + slideHeight);
			//console.log("newHeight = " + newHeight)
			
			if(newHeight > slideHeight){
				slideHeight = newHeight;
			}
			
			
			//var sliderWidth = $(".slider_wrapper").width();
			//$(this).css("width", sliderWidth+"px")
			
		});			
		var dataHeight = Number($(this).attr('data-height'));

		$(this).css('height', slideHeight+'px');

		$(this).attr('data-height', slideHeight);
		
	});
	//setImg4Height();
}


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