$.setDisabledByDefault(true);

$('.main-carousel').carousel();

var debugMode = false;

if(debugMode){
	moveTo('slide-21');
}

var mainCarousel = null;
var shownCarousel = 'main-carousel';

var navigationIsOpened = false;
var menuIsOpened = false;
var submenu0IsOpened = false;
var ABCIsOpened = false;
var referencesAreOpened = false;

var currentSlideRef = null;
var currentPopupRef = null;

var event = 'touchstart';
var mainCarouselPage = 0;
var mainCarouselLastPage = 99;

var otherCarouselPage = 0;
var otherCarouselLastPage = 0;

var pendingAction = null;

[].forEach(function(url){
    new Image().src = url;
});

if(debugMode){
	$('.btn').css('opacity', '0.5');
	$('.btn:nth-child(even)').css('background-color', 'red');
	$('.btn:nth-child(odd)').css('background-color', 'blue');

}

$(window).on('onDrag', function(){
		 
	jQuery.fx.off = true;
});
 
$(window).on('onRelease', function(){

	jQuery.fx.off = false;
});

$('.main-carousel').carousel().on('afterSlide', function(event, previousIndex, newIndex){
	mainCarouselPage = newIndex;
	mainCarouselLastPage = previousIndex;
	setNavigationAndElements(mainCarouselPage);
});

setNavigationAndElements(1);

function setNavigationAndElements(page, carousel = 'main-carousel'){

	var slide = page - 1;

	var slideId = $('#'+carousel+' .m-item:nth-child('+(page)+')').attr('id');
	var lastSlideId = $('#'+carousel+' .m-item:nth-child('+(mainCarouselLastPage)+')').attr('id');
	
	if(debugMode){ console.log('setNavigationAndElements [page='+page+' lastpage='+mainCarouselLastPage+' slideid='+slideId+' lastslideid='+lastSlideId+' carousel='+carousel+']'); }
	
	if(carousel == shownCarousel){
		
		var headerBg = $('#'+carousel+' .m-item:nth-child('+page+')').attr('data-header-bg');
		var references = $('#'+carousel+' .m-item:nth-child('+page+')').attr('data-ref');
		
		//$('#header').fadeOut();
		console.log($('.header-bg.enabled').attr('data-header') + headerBg);
		if(parseInt($('.header-bg.enabled').attr('data-header')) != parseInt(headerBg)){
			console.log('oups');
			$('.header-bg.enabled').fadeOut().removeClass('enabled');

			if(headerBg >= 0){
				$('#header-bg-'+headerBg).fadeIn();
				$('#header-bg-'+headerBg).addClass('enabled');
			}
		}
			
		if(references.length > 0){
			$('#references').fadeIn();
			$('#references-toggle-btn').fadeIn();

			if(page > 1){
				$('#abc-toggle-btn').fadeIn();
			}
					
			loadReferences(references);
		}
		else{
			
			if(referencesAreOpened){
				closeReferences();
			}
			
			$('#abc-toggle-btn').fadeOut();
			$('#references').fadeOut();
			$('#references-toggle-btn').fadeOut();
		}
		
		if(carousel == 'main-carousel'){

			if(slide == 1){
			
			}
		}
	}
}

function clearAnimations(lastPage, page, carousel = 'main-carousel'){
	
	if(debugMode){ console.log('clearAnimations [page='+page+' carousel='+carousel+']'); }
	
	lastVisitedSlide = lastPage;
	var slide = page - 1;

	if(carousel == shownCarousel){
		
		if(carousel == 'main-carousel'){
			closePopups(false);
		}
	}

	resetVideos();
}

function launchAnimations(page){

	if(page == 1){
		
	}
	
	lastPage = page;
}

// Navigation

$('#navigation-toggle-btn').on(event, function(){
	toggleNavigation();
});	

// Menu

$('#header-back-btn').on(event, function(){
	if(mainCarouselLastPage != null){
		$('.main-carousel').carousel('move', mainCarouselLastPage);
	}
});	

$('#header-menu-toggle-btn').on(event, function(){
	toggleMenu();
});	

// Menu buttons

$('#menu-btn-6').on(event, function(){
	toggleSubmenu0();
});

// Mentions

// $('#mentions-toggle-btn').on(event, function(){
// 	openPDF('mentions-legales', 'Mentions légales', 'documents/ml.pdf', '1.4');
// });	

// ABC

$('#abc-toggle-btn').on(event, function(){
	toggleABC();
});	

// References

$('#references-toggle-btn').on(event, function(){
	toggleReferences();
});	


// Special buttons

$('.move-to-btn').on(event, function(){
	
	moveTo($(this).attr('data-move-to'));

	closeMenu();
});

function moveTo(moveTo, carousel = 'main-carousel'){
	
	pendingAction = null;
	
	if(Number.isInteger(parseInt(moveTo))){
		if(debugMode){ console.log('Move to : '+moveTo);}
		$('.'+carousel).carousel('move', parseInt(moveTo));
	}
	else{
		var index = $('.main-carousel .m-item').index($('.main-carousel .m-item#'+moveTo));
		
		if(Number.isInteger(index) && index >= 0){
			if(debugMode){ console.log('Move to : '+(index + 1));}
			$('.'+carousel).carousel('move', (index + 1));
		}
	}
}

$('.show-btn').on(event, function(){
	if(debugMode){ console.log('show slide '+$(this).attr('data-show'));}

	$('#'+$(this).attr('data-show')).show();
});

$('.fade-in-btn').on(event, function(){
	$('#'+$(this).attr('data-fade-in')).fadeIn();
	
	if($('#'+$(this).attr('data-fade-in')).hasClass('full-size')){
		$('#header').fadeOut();
		$('#menu').fadeOut();
	}
});

$('.load-ref-btn').on(event, function(){
	loadReferences($(this).attr('data-ref'));
});

$('.fade-out-btn').on(event, function(){
	
	var carousel = $(this).attr('data-carousel');
		
	$('#'+$(this).attr('data-fade-out')).fadeOut(function(){
		if(carousel != ''){
			shownCarousel = 'main-carousel';
			$('#'+carousel).carousel('move', 0);
		}
	});
	
	$('#header').fadeIn();
	$('#menu').fadeIn();
})

$('.play-video-btn').on(event, function(){
	$('#'+$(this).attr('data-video')).get(0).play();
})

$('.reset-video-btn').on(event, function(){
	$('#'+$(this).attr('data-video')).get(0).pause();
	$('#'+$(this).attr('data-video')).get(0).currentTime = 0;
})

// Slide 1

$('#slide-1-btn-0').on(event, function(){
	document.location.href = 'mailto:privacyoffice@amgen.com';
});	

$('#slide-1-btn-1').on(event, function(){
	document.location.href = 'https://www.amgen.fr/protection-donnees-professionnels-sante/';
});	

$('#slide-1-btn-2').on(event, function(){
	document.location.href = 'https://www.amgen.fr/protection-donnees-professionnels-sante/';
});	

$('#slide-1-btn-3').on(event, function(){
	document.location.href = 'mailto:eu-fr-pcms@amgen.com';
});	

$('#slide-1-btn-4').on(event, function(){
	document.location.href = 'http://www.signalement-sante.gouv.fr';
});	

$('#slide-1-btn-5').on(event, function(){
	document.location.href = 'mailto:pharmacovigilance@amgen.com';
});	


// Slide 21

$('#slide-21-btn-0').on(event, function(){
	document.location.href = 'http://www.ema.europa.eu/ema/index.jsp?curl=pages/medicines/human/medicines/003746/human_med_001835.jsp&mid=WC0b01ac058001d124';
});	

$('#slide-21-btn-1').on(event, function(){
	document.location.href = 'http://www.ansm.sante.fr';
});

$('#slide-21-btn-2').on(event, function(){
	document.location.href = 'mailto:pharmacovigilance@amgen.com';
});	

function closePopups(animated = true){
	
	if(animated){
		$('.layer.popup').fadeOut();
	}
	else{
		$('.layer.popup').hide();
	}
	
	$('#header').fadeIn();
	$('#menu').fadeIn();
}

function resetVideos(){
	$('video').each(function(){
		$(this).get(0).pause();
		$(this).get(0).currentTime = 0;
	});
}

function loadReferences(name){
	
	if(name != ''){
		
		if(referencesAreOpened){
			$('#references-toggle-btn').fadeIn();
		
			closeReferences(function(){
				$('#references-content').attr('src', 'images/'+name+'.png');
			});
		}
		else{
			$('#references-toggle-btn').fadeIn();
			
			$('#references-content').attr('src', 'images/'+name+'.png');
		}
		
		if(debugMode){console.log('loaded ref : ' + name);}

	}
	else{
		$('#references-toggle-btn').fadeOut();
	}
}

function toggleNavigation(){

	if(!navigationIsOpened){
		$('#navigation').animate({'top':'685px'});
		navigationIsOpened = true;
	}
	else{
		$('#navigation').animate({'top':'735px'});
		navigationIsOpened = false;
	}
}

function toggleMenu(){

	if(!menuIsOpened){
		$('#menu').animate({'height':'575px'});
		menuIsOpened = true;
	}
	else{
		closeMenu();
	}
}

function closeMenu(){
	$('#menu').animate({'height':'0px'});
	closeSubmenu0(false);

	menuIsOpened = false;
}

function toggleSubmenu0(){
	if(!submenu0IsOpened){
		$('#submenu-0').animate({'width':'112px'});
		submenu0IsOpened = true;
	}
	else{
		closeSubmenu0();
	}	
}

function closeSubmenu0(animated = true){
	
	if(animated){
		$('#submenu-0').animate({'width':'0px'});
	}
	else{
		$('#submenu-0').css('width','0px');
	}
	
	submenu0IsOpened = false;
}

function toggleABC(){
	
	if(!ABCIsOpened){
		$('#abc-toggle-btn').animate({'bottom':($('#abc-content').height()-1)+'px'});
		$('#abc').animate({'bottom':($('#abc-content').height()-45-1)+'px'});
		ABCIsOpened = true;
	}
	else{
		closeABC();
	}
}

function closeABC(callback = function(){}){
	$('#abc-toggle-btn').animate({'bottom':'0px'});
	$('#abc').animate({'bottom':'-45px'}, function(){
		callback();
	});
	
	ABCIsOpened = false;
}

function toggleReferences(){
	
	if(!referencesAreOpened){
		$('#references-toggle-btn').animate({'bottom':($('#references-content').height()-1)+'px'});
		$('#references').animate({'bottom':($('#references-content').height()-1-45)+'px'});
		referencesAreOpened = true;
	}
	else{
		closeReferences();
	}
}

function closeReferences(callback = function(){}){
	$('#references-toggle-btn').animate({'bottom':'0px'});
	$('#references').animate({'bottom':'-45px'}, function(){
		callback();
	});
	
	referencesAreOpened = false;
}

window.addEventListener('transitionend', function(event) {

	element = $(event.srcElement.closest('.m-carousel'));

	if(element.hasClass('main-carousel')){
		clearAnimations(mainCarouselLastPage, mainCarouselPage, element.attr('id'));
		launchAnimations(mainCarouselPage);
	}
	else{
		clearAnimations(otherCarouselLastPage, otherCarouselPage, element.attr('id'));
		launchAnimations(otherCarouselPage);
	}
    
}, false );