var lastPage = 99;

$.setDisabledByDefault(true);

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

var debugMode = false;

if(debugMode){
	$('.main-carousel').carousel('move', 34);
}

var menuIsOpened = false;
var ABCIsOpened = false;
var referencesAreOpened = false;

var currentSlideRef = null;
var currentPopupRef = null;

var event = 'touchstart';
var pagesHistory = [];
var historyShouldBeSaved = true;
var page = 0;
var lastPage = 0;

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

if(debugMode){
	$('.btn').css('opacity', '0.5');
	$('.btn').css('background-color', 'red');
}

 $(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){
	page = newIndex;
	lastPage = previousIndex - 1;
	if(historyShouldBeSaved){
		pagesHistory.push(previousIndex);
	}
	setNavigationAndElements(page);
});

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

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

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

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

function setNavigationAndElements(page){

	var slide = page - 1;

	var headerBg = $('.main-carousel .m-item:nth-child('+page+')').attr('data-header-bg');
	var references = $('.main-carousel .m-item:nth-child('+page+')').attr('data-ref');

	if(headerBg == ''){
		$('#header').fadeOut();
		$('#header-bg-0').fadeOut();
		$('#header-bg-1').fadeOut();
	}
	else if(headerBg == '0'){
		$('#header').fadeIn();
		$('#header-bg-0').fadeIn();
		$('#header-bg-1').fadeOut();
	}
	else if(headerBg == '1'){
		$('#header').fadeIn();
		$('#header-bg-0').fadeOut();
		$('#header-bg-1').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(slide == 1){

	}
}

function clearAnimations(page, reset){
	
	closePopups(false);
	resetVideos();
}

function launchAnimations(page){

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

// Menu

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

$('#header-back-btn').on(event, function(){
	historyShouldBeSaved = false;
	if(pagesHistory.length - 1 >= 0){
		$('.main-carousel').carousel('move', pagesHistory[pagesHistory.length - 1]);
	}
	pagesHistory.splice((pagesHistory.length - 1), 1);
	historyShouldBeSaved = true;
});	

// 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(){
	if(debugMode){ console.log($(this).attr('data-move-to'));}
	$('.main-carousel').carousel('move', parseInt($(this).attr('data-move-to')));
	
	closeMenu();
});

$('.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 != ''){
			$('#'+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');
});

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(referencesAreOpened){
		closeReferences(function(){
			$('#references-content').attr('src', 'images/'+name+'.png');
		});
	}
	else{
		$('#references-content').attr('src', 'images/'+name+'.png');
	}
	
	if(debugMode){ console.log('loaded ref : ' + name); }
}

function toggleMenu(){

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

function closeMenu(){
	$('#menu').animate({'height':'0px'});
	menuIsOpened = 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(lastPage);
		launchAnimations(page);
	}
    
}, false );