/* functions to control expando interactivities */

var numberOfExpando;
var lastExpID = "";
/* transitions: grow up and down expando displays */
var expandoOpening = false;
$( document ).ready(function() {	
	 //$('.expando_btn').attr('tabindex', 1);
	 //$('.expando_reveal').attr('tabindex', 2);	
	$('.expando_btn').click(function(e){		
		/* transitions: grow up and down expando displays */
		if (!expandoOpening)
		{
			if(document.useTransitions){
				/* transitions: grow up and down expando displays */
				expandoOpening = true;
			}	
			findExpandoNum();
			var expID = $(this).attr('id').split('btn_')[1];	
			var expando_sec = '#'+$(e.target).closest('.section').attr('id')		
			if (lastExpID != "") {
				$('#expando_btn_' + lastExpID).addClass('expando_btn_visited');
			}
			if(Number($(this).attr('data')) == 0){ 
				$(this).attr('data',1);
			}
			if(isWeb){
				/*if(document.useTransitions){
					// transitions: grow up and down expando displays 
					if(lastExpID == ""){
						$(this).hide();	
						$('#expando_reveal_' + expID).show();
						$('#expando_reveal_' + expID).children('.expando_reveal_content').hide();					
						$('#expando_reveal_' + expID).children('.expando_reveal_content').show('blind',500,function(){
							$(this).focus(); // 20180501 Accessibility 
							lastExpID = expID;
						});					
					}else if(lastExpID != expID){					
						$('#expando_reveal_' + lastExpID).children('.expando_reveal_content').hide('blind',500,function(){
							$('#expando_btn_' + lastExpID).show();
							$(this).parent().hide();						
							$('#expando_btn_' + expID).hide();	
							$('#expando_reveal_' + expID).show();
							$('#expando_reveal_' + expID).children('.expando_reveal_content').hide();						
							$('#expando_reveal_' + expID).children('.expando_reveal_content').show('blind',500,function(){	
								$(this).focus(); // 20180501 Accessibility 
								lastExpID = expID;
							});						
						});										
					}else{
						$(this).hide();	
						$('#expando_reveal_' + expID).show();
						$('#expando_reveal_' + expID).children('.expando_reveal_content').hide();					
						$('#expando_reveal_' + expID).children('.expando_reveal_content').show('blind',500,function(){
							$(this).focus(); // 20180501 Accessibility 
							lastExpID = expID;
						});
					}				
				}else{*/
					// close previously opened expando 
					if(lastExpID != expID)
					{
						$('#expando_btn_' + lastExpID).show();
						$('#expando_reveal_' + lastExpID).children('.expando_reveal_content').parent().hide();						
					}
					$(this).hide();	
					$('#expando_reveal_' + expID).show();
					$('#expando_reveal_' + expID).children('.expando_reveal_content').focus(); /* 20180501 Accessibility */
					lastExpID = expID;
				/*}*/
			}else{
				// close previously opened expando 
				if(lastExpID != expID)
				{
					$('#expando_btn_' + lastExpID).show();
					$('#expando_reveal_' + lastExpID).children('.expando_reveal_content').parent().hide();						
				}
				$(this).hide();	
				$('#expando_reveal_' + expID).show();
				$('#expando_reveal_' + expID).children('.expando_reveal_content').focus(); // 20180501 Accessibility 
				lastExpID = expID;
			}
			isExpandoDone(expando_sec);		
			/* hotspot banner fix */
			if(isiPad){
				if(iOSApp){
					//$('.bar_robot').removeClass('displayBlock').addClass('displayNone');
				}
				setTimeout(function(){hotSpotAdjustBanner();},100);
			}
			/* hotspot banner fix */
			if(isAndroidTablet || isAndroidPhone || isMacChrome){
				$('html,body').scrollTop(windowTop -1)
			}
			if(document.useTransitions){
				/* transitions: grow up and down expando displays */
				setTimeout(function(){ expandoOpening = false; }, 1000);
			}
			return false;
		}	
	});
	$('.expando_reveal_heading').click(function(){
		var expID = $(this).parent().attr('id').split('reveal_')[1];
		$('#expando_btn_' + expID).addClass('expando_btn_visited');		
		if(isWeb){
			/*if(document.useTransitions){
				// transitions: grow up and down expando displays 				
				$('#expando_reveal_' + expID).children('.expando_reveal_content').hide('blind',500,function(){
					$('#expando_btn_' + expID).show();
					$(this).parent().hide();
					/ 2021 Accessibility Update
					$('#expando_btn_' + expID).focus();
				});
				
			}else{*/
				$('#expando_btn_' + expID).show();
				$(this).parent().hide();
				/* 2021 Accessibility Update */
				$('#expando_btn_' + expID).focus();
			/*}*/
		}else{		
			$('#expando_btn_' + expID).show();
			$(this).parent().hide();	
			/* 2021 Accessibility Update */
			$('#expando_btn_' + expID).focus();
		}		
		/* hotspot banner fix */
		if(isiPad){
			if(iOSApp){
				//$('.bar_robot').removeClass('displayBlock').addClass('displayNone');
			}
			setTimeout(function(){hotSpotAdjustBanner();},100);
		}		
		return false;
	});	
	/* accessibility control */
	$('.expando_btn').keypress(function (e) {
        if (e.keyCode == 13 || e.keyCode == 32) {
            $(this).trigger('click');
			var expID = $(this).attr('id').split('btn_')[1];
			$('#expando_reveal_' + expID).focus();
        }
    });
	/* accessibility control */
	$('.expando_reveal_heading').keypress(function (e) {
        if (e.keyCode == 13 || e.keyCode == 32) {
            $(this).trigger('click');
			var expID = $(this).parent().attr('id').split('reveal_')[1];
			$('#expando_btn_' + expID).focus();
        }
    });	
	/*var keyFocus = false;
    $(document).keyup(function (e) { keyFocus = true; });
    $(document).click(function (e) { keyFocus = false; });
	
	$('.expando_reveal').focus(function () {
        if (keyFocus) {
            $(this).css('border', 'solid 1px #990000');
        }
    });
    $('.expando_reveal').focusout(function () {
        if (keyFocus) {
            $(this).css('border', 'none');
            
        }
    });*/	
});

/* 
	findExpandoNum function 
	
	find expando buttons in this section and update numberOfExpando variable
	called when expando clicked
*/
function findExpandoNum(){
	numberOfExpando = 0;
	$('#sec_'+document.currentSection).find('.expando_btn').each(function(index, element) {
       numberOfExpando++;
    });
}

/* 
	isExpandoDone function 
	
	mark each expando as viewed when clicked
	called when expando clicked
*/
function isExpandoDone(expando_sec){
	var secNum = expando_sec.split('_')[1]
	var expandoDone = 0;
	var clicked = 0;
	$('#sec_'+secNum).find('.expando_btn').each(function(index, element) {
		var data = Number($(this).attr('data'));
		if(data === Number(1)){
			expandoDone++;
			clicked = Number($('#sec_'+secNum).attr('data-clicked'));
			clicked +=1;
			$('#sec_'+secNum).attr('data-clicked',clicked);	
			$(this).attr('data','2');
		}		
    });
			
	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])
	if(totalClicked == totalClicks){	
		if(isAndroidTablet || isAndroidPhone || isMacChrome){
			document.intComplete = true;
			clearInterval(document.lockinterval)
			document.lockinterval = setInterval(function(){
				console.log(expando_sec)
				che = expando_sec
				secNum = expando_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);
		}
	}else{
        if(document.useTransitions && Number(document.lockedArr[Number(secNum) - 1])==1 && Number(document.trackingArr[Number(secNum) - 1])!=1 && isWeb){
            setTimeout(function(){$("html, body").animate({ scrollTop: $(document).height() }, 500);},500)
            
        }
    }
}

/* expando visited state functionality 2018 */
function markExpandoVisited(sNum)
{
	$('#sec_'+sNum+' .expando_btn').addClass('expando_btn_visited');
}

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