$.setDisabledByDefault(true);

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

var debugMode = false;

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

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

var navigationIsOpened = false;
var menuIsOpened = false;
var submenu0IsOpened = false;
var submenu1IsOpened = 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);

$('#inclusion-popup-carousel').carousel().on('afterSlide', function(event, previousIndex, newIndex){
	if(newIndex == 1){
		$('#inclusion-popup-steps').css('background-image', 'url(\'images/steps-0.png\')');
		loadReferences('inclusion-popup-slide-0-ref');
	}
	else if(newIndex == 2){
		$('#inclusion-popup-steps').css('background-image', 'url(\'images/steps-1.png\')');
		loadReferences('inclusion-popup-slide-1-ref');
	}
});

$('#slide-27-popup-1-carousel').carousel().on('afterSlide', function(event, previousIndex, newIndex){
	if(newIndex == 1){
		$('#slide-27-popup-1-steps').css('background-image', 'url(\'images/steps-0.png\')');
		if(shownCarousel == 'slide-27-popup-1-carousel'){
			loadReferences('slide-27-popup-1-slide-0-ref');
		}
	}
	else if(newIndex == 2){
		$('#slide-27-popup-1-steps').css('background-image', 'url(\'images/steps-1.png\')');
		if(shownCarousel == 'slide-27-popup-1-carousel'){
			loadReferences('slide-27-popup-1-slide-1-ref');
		}
	}
});

$('#slide-39-popup-2-carousel').carousel().on('afterSlide', function(event, previousIndex, newIndex){
	if(newIndex == 1){
		$('#slide-39-popup-2-steps').css('background-image', 'url(\'images/steps-0.png\')');
		if(shownCarousel == 'slide-39-popup-2-carousel'){
			loadReferences('slide-39-popup-2-slide-0-ref');
		}
	}
	else if(newIndex == 2){
		$('#slide-39-popup-2-steps').css('background-image', 'url(\'images/steps-1.png\')');
		if(shownCarousel == 'slide-39-popup-2-carousel'){
			loadReferences('slide-39-popup-2-slide-1-ref');
		}
	}
});

$('#slide-44-popup-0-carousel').carousel().on('afterSlide', function(event, previousIndex, newIndex){
	if(newIndex == 1){
		$('#slide-44-popup-0-steps').css('background-image', 'url(\'images/steps-0.png\')');
		if(shownCarousel == 'slide-44-popup-0-carousel'){
			loadReferences('slide-44-popup-0-slide-0-ref');
		}
	}
	else if(newIndex == 2){
		$('#slide-44-popup-0-steps').css('background-image', 'url(\'images/steps-1.png\')');
		if(shownCarousel == 'slide-44-popup-0-carousel'){
			loadReferences('slide-44-popup-0-slide-1-ref');
		}
	}
});

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');
			
		if(headerBg == ''){
			$('#header').fadeOut();
			$('#header-bg-0').fadeOut();
			$('#header-bg-1').fadeOut();
			$('#header-bg-2').fadeOut();
		}
		else if(headerBg == '0'){
			$('#header').fadeIn();
			$('#header-bg-0').fadeIn();
			$('#header-bg-1').fadeOut();
			$('#header-bg-2').fadeOut();
		}
		else if(headerBg == '1'){
			$('#header').fadeIn();
			$('#header-bg-0').fadeOut();
			$('#header-bg-1').fadeIn();
			$('#header-bg-2').fadeOut();
		}
		else if(headerBg == '2'){
			$('#header').fadeIn();
			$('#header-bg-0').fadeOut();
			$('#header-bg-1').fadeOut();
			$('#header-bg-2').fadeIn();
		}
			
		if(references.length > 0){
			$('#mentions-toggle-btn').fadeIn();
			$('#references').fadeIn();
			$('#references-toggle-btn').fadeIn();
			$('#abc-toggle-btn').fadeIn();
					
			loadReferences(references);
		}
		else{
			
			if(referencesAreOpened){
				closeReferences();
			}
			
			$('#mentions-toggle-btn').fadeOut();
			$('#abc-toggle-btn').fadeOut();
			$('#references').fadeOut();
			$('#references-toggle-btn').fadeOut();
		}
		
	
		if(carousel == 'main-carousel'){
			if(slide == 1){
			
			}
			else if(slideId == 'slide-26' && lastSlideId == 'slide-10'){
				moveTo('slide-6');
			}
			else if(pendingAction == 'go_back_to_acrm20_screen' && lastSlideId == 'slide-9'){
				moveTo('slide-16');
				pendingAction = null;
			}
			else if(pendingAction == 'go_back_to_first_synthesis_screen' && (lastSlideId == 'slide-46' || lastSlideId == 'slide-26')){
				moveTo('slide-43');
				pendingAction = null;
			}
			else if(pendingAction == 'go_back_to_second_synthesis_screen' && (lastSlideId == 'slide-46' || lastSlideId == 'slide-30')){
				moveTo('slide-44');
				pendingAction = null;
			}
		}
	}
}

function clearAnimations(lastPage, page, carousel = 'main-carousel'){
	
	if(debugMode){ console.log('clearAnimations [page='+page+' carousel='+carousel+']'); }
	
	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(){
	$('.main-carousel').carousel('prev');
});	

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

// Menu buttons

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

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

/* Submenu 0 */

$('#submenu-0-btn-1').on(event, function(){
	closeSubmenu0();
	toggleSubmenu1();
});

/* Submenu 1 */

// 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:qualite-visitemedicale@celgene.com';
});	

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

$('#slide-1-btn-2').on(event, function(){
	document.location.href = 'http://www.celgene.fr/politique-de-confidentialite-de-celgene/notice-dinformation-pour-les-professionnels-de-sante/';
});	

// Slide 5

$('#slide-5-btn-0').on(event, function(){
	$('#references-toggle-btn').fadeOut();
});

$('#slide-5-popup-0-close-btn').on(event, function(){
	$('#references-toggle-btn').fadeIn();
});

// Slide 9

$('#slide-9-btn-1').on(event, function(){
	$('#inclusion-popup-close-btn').attr('data-ref', 'slide-9-ref');
});

// Slide 12

$('#slide-12-btn-0').on(event, function(){
	$('#inclusion-popup-close-btn').attr('data-ref', 'slide-12-ref');
});

// Slide 13

$('#slide-13-btn-1').on(event, function(){
	$('#inclusion-popup-close-btn').attr('data-ref', 'slide-13-ref');
});

// Slide 14

$('#slide-14-btn-1').on(event, function(){
	$('#inclusion-popup-close-btn').attr('data-ref', 'slide-14-ref');
});

// Slide 15

$('#slide-15-btn-1').on(event, function(){
	$('#inclusion-popup-close-btn').attr('data-ref', 'slide-15-ref');
});

// Slide 16

$('#slide-16-btn-1').on(event, function(){
	pendingAction = 'go_back_to_acrm20_screen';
});

// Slide 17

$('#slide-17-btn-0').on(event, function(){
	$('#inclusion-popup-close-btn').attr('data-ref', 'slide-17-ref');
});

// Slide 18

$('#slide-18-btn-0').on(event, function(){
	$('#inclusion-popup-close-btn').attr('data-ref', 'slide-18-ref');
});

// Slide 19

$('#slide-19-btn-0').on(event, function(){
	$('#inclusion-popup-close-btn').attr('data-ref', 'slide-19-ref');
});

// Slide 20

$('#slide-20-btn-0').on(event, function(){
	$('#inclusion-popup-close-btn').attr('data-ref', 'slide-20-ref');
});

// Slide 21

$('#slide-21-btn-0').on(event, function(){
	$('#inclusion-popup-close-btn').attr('data-ref', 'slide-21-ref');
});

// Slide 22

$('#slide-22-btn-0').on(event, function(){
	$('#inclusion-popup-close-btn').attr('data-ref', 'slide-22-ref');
});

// Slide 23

$('#slide-23-btn-0').on(event, function(){
	$('#inclusion-popup-close-btn').attr('data-ref', 'slide-23-ref');
});

// Slide 24

$('#slide-24-btn-0').on(event, function(){
	$('#inclusion-popup-close-btn').attr('data-ref', 'slide-24-ref');
});

// Slide 27

$('#slide-27-btn-1').on(event, function(){
	shownCarousel = 'slide-27-popup-1-carousel';
	$('#slide-27-popup-1-close-btn').attr('data-ref', 'slide-27-ref');
});

// Slide 29

$('#slide-29-btn-0').on(event, function(){
	$('#slide-29-popup-0-close-btn').attr('data-ref', 'slide-29-ref');
});

$('#slide-39-btn-2').on(event, function(){
	shownCarousel = 'slide-39-popup-2-carousel';
});


// Slide 43

$('#slide-43-btn-0').on(event, function(){
	$('#inclusion-popup-close-btn').attr('data-ref', 'slide-43-ref');
});

$('#slide-43-btn-1').on(event, function(){
	pendingAction = 'go_back_to_first_synthesis_screen';
});


$('#slide-43-btn-2').on(event, function(){
	pendingAction = 'go_back_to_first_synthesis_screen';
});


// Slide 44

$('#slide-44-btn-0').on(event, function(){
	$('#inclusion-popup-close-btn').attr('data-ref', 'slide-44-ref');
});

$('#slide-44-btn-1').on(event, function(){
	pendingAction = 'go_back_to_second_synthesis_screen';
});


$('#slide-44-btn-2').on(event, function(){
	pendingAction = 'go_back_to_second_synthesis_screen';
});


$('#slide-44-btn-3').on(event, function(){
	shownCarousel = 'slide-44-popup-0-carousel';
	$('#slide-44-popup-0-close-btn').attr('data-ref', 'slide-44-ref');
});

$('#slide-44-btn-4').on(event, function(){
	$('#slide-29-popup-0-close-btn').attr('data-ref', 'slide-44-ref');
});

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':'521px'});
		menuIsOpened = true;
	}
	else{
		closeMenu();
	}
}

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

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

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

function toggleSubmenu1(){
	if(!submenu1IsOpened){
		$('#submenu-1').animate({'width':'349px'});
		submenu1IsOpened = true;
	}
	else{
		closeSubmenu1();
	}	
}

function closeSubmenu1(animated = true){
	
	if(animated){
		$('#submenu-1').animate({'width':'0px'});
	}
	else{
		$('#submenu-1').css('width','0px');
	}
	
	submenu1IsOpened = 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 );