var HT = 0;
var niveauY = [0,0,0]
var Hauteur = [0];
var zone = [13965.8, 17425.95, 8435.1,6766,4121.1];
var niveau = 0;
var oldniv,nextniv,prevniv, Hmax, dim, nbclic, CameraObject, StageInfos, tweenpers, tweentxtpers;
var changzone = 0;
var adapt = 0;
var playanimation =0;
var minimum,maximum,lecturevertical,ecran_1,ObjAnim,ecart,min,max;
var hit = 0;
var trig, initY;
var persoB = [0,0,0];
var revers = 0;
var retour = 0;
var txtdefil = 0;
var route = 0;
var paral = 0;
/*
var StageInfos = [lib.properties.width, lib.properties.height]; //récupération des infos turbo
var CameraObject = AdobeAn.VirtualCamera.getCamera(exportRoot); //variable caméra
//CameraObject.camera.y.y= StageInfos[1]/2;  //position de départ*/

function paralax() //encrage
{

console.log(ObjAnim.currentFrame);
ObjAnim.gotoAndStop(ObjAnim.currentFrame-delta2);
}

function initcontrol() {

var touchesupported = createjs.Touch.isSupported();	
	
if (isMobile.any()){
console.log(isMobile.any());

if (touchesupported) {
	createjs.Touch.enable(stage, false, true);
	canvas.addEventListener("touchstart", touchst);
	canvas.addEventListener("touchmove", touchmv);
	canvas.addEventListener("touchend", touchstp);
}

}else{

if (touchesupported) {
	createjs.Touch.enable(stage, false, true);
	canvas.addEventListener("touchstart", touchst);
	canvas.addEventListener("touchmove", touchmv);
	canvas.addEventListener("touchend", touchstp);
	}
	
canvas.addEventListener("DOMMouseScroll", fl_ClickToPosition);
canvas.addEventListener("mousewheel", fl_ClickToPosition);
}
}




function handleTick2(event) {
	
	//console.log(CameraObject.camera.y)
	
	if(niveau1 != null){
	
	if (CameraObject.camera.y < StageInfos[1]/2) {
		delta = 0;
		tweencam = TweenLite.to(CameraObject.camera, 0.3, {
			y: StageInfos[1]/2,
			ease: Bounce.easeOut
		});
	}
	
	if (CameraObject.camera.y > Hmax - StageInfos[1]/2) {
		delta = 0;
		tweencam = TweenLite.to(CameraObject.camera, 0.3, {
			y:  Hmax - StageInfos[1]/2 ,
			ease: Bounce.easeOut
		});
	}
		
	if (CameraObject.camera.x < initX -2){
	tweencam = TweenLite.to(CameraObject.camera, 0, {
				x: initX,
				y: CameraObject.camera.y,
				ease: Power1.easeInOut
			});	
	}
	if ((CameraObject.camera.x > niveau1.nominalBounds.width - StageInfos[0]/2 + 4.8 )){
		if (paral == 1){paral = 0;}
		tweencam = TweenLite.to(CameraObject.camera, 0, {
				x: niveau1.nominalBounds.width - StageInfos[0]/2 + 4.8,
				y: CameraObject.camera.y,
				ease: Power1.easeInOut
			});	
		}
	
	if (route == 1){ecransA();}
	
	
	//Actions carried out each tick (aka frame)
	/*if (!event.paused) {
		// Actions carried out when the Ticker is not paused.
	}*/
	if (retour == 1 && CameraObject.camera.y < niveauY[6]){
	retour = 0;
	tweencam = TweenLite.to(CameraObject.camera, 0.1, {
				x: initX,
				ease: Power1.easeInOut
			});
	
	
	
	}
}
}

function paralaxx(){
	if ((CameraObject.camera.x <= initX )&& (delta > 0)){
	paral = 0;
	persoB[0] = 0;	
	
	tweencam = TweenLite.to(CameraObject.camera, 0.2, {
				x:initX,
				y: CameraObject.camera.y - 400,
				ease: Power1.easeInOut, onComplete: fin
			});
	
	tweenpers = TweenLite.to(thiss.perso, 0.2, {
				x: persoB[1],
				y: persoB[2],
				ease: Power1.easeInOut
			});	
	tweentxtpers = TweenLite.to(thiss.txt_pers, 0.2, {
				x: persoB[3],
				y: persoB[4],
				ease: Power1.easeInOut
			});			
			
	
	
	
		scrollLVL = 1;
		delta = 0;
	}
	
	
	if ((CameraObject.camera.x >= niveau1.nominalBounds.width - StageInfos[0]/2 + 4.8 ) && (delta <0)){
		paral = 0;
		scrollLVL = 1;
		delta = 0;
		initY = CameraObject.camera.y
		
		
	
	/*TweenLite.to(CameraObject.camera, 0.2, {
				y: CameraObject.camera.y + 300,
				ease: Power1.easeInOut, onComplete: fin
			});	*/
		}
	
	
	
	
	if ( paral == 1 && hit == 0 && CameraObject.camera.y < 23379 ){
	tr()
	if(revers == 0 ) {mouveperso()}
	
	}

function fin() //encrage
{
paral = 0;
scrollLVL = 1;
delta = 0;
}

}

////////////////////////////////////////////////////////////////////////////// Route A /////////////////////////////////////////////////////////////////////
function ecransA()
{

//// anim 1 -> Déclanchement sur scroll avec blocage
if ((CameraObject.camera.y < niveauY[1] -1100) && (CameraObject.camera.y > niveauY[1] -1350) && delta > 0) //réinit de l'anim
{
console.log ("reset anim 1")
ObjAnim.nxt.x = 970.1;
ObjAnim.x = ObjAnim.nominalBounds.width / 2;
}
if ((CameraObject.camera.y < niveauY[1] -150) && (CameraObject.camera.y > niveauY[1] -350) && delta < 0)
{
console.log ("anim 1")
nbclic = 1;
ObjAnim = niveau1.fx1
animation();
}

//// anim 2 -> Déclanchement sur scroll avec blocage
if ((CameraObject.camera.y < niveauY[2] -1100) && (CameraObject.camera.y > niveauY[2] -1350) && delta > 0) //réinit de l'anim
{
console.log ("reset anim 2")
ObjAnim.nxt.x = 970.1;
ObjAnim.x = ObjAnim.nominalBounds.width / 2;
}
if ((CameraObject.camera.y < niveauY[2] -150) && (CameraObject.camera.y > niveauY[2] -350) && delta < 0)
{
console.log ("anim 2")
nbclic = 2;
ObjAnim = niveau1.fx2
animation();
}

//// anim 3 -> Déclanchement sur scroll avec blocage
if ((CameraObject.camera.y < niveauY[3] -1100) && (CameraObject.camera.y > niveauY[3] -1350) && delta > 0) //réinit de l'anim
{
console.log ("reset anim 3")
ObjAnim.nxt.x = 970.1;
ObjAnim.x = ObjAnim.nominalBounds.width / 2;
}
if ((CameraObject.camera.y < niveauY[3] -150) && (CameraObject.camera.y > niveauY[3] -350) && delta < 0)
{
console.log ("anim 3")
nbclic = 5;
ObjAnim = niveau1.fx3
animation();
}
}


function scrol(e) //encrage
{

if (scrollLVL == 1){ //init ecran
delta = 0;
scrollLVL = 0; // bloque le scroll
tweencam = TweenLite.to(CameraObject.camera, 0.3, {y: e,ease: Power1.easeOut,onComplete: cs});
}
function cs()
{
delta = 0;
persoB[0] = 1;
paral = 1;
}
}


function mouveperso(){
if (persoB[0] == 1){
			tweenpers = TweenLite.to(thiss.perso, 0.1, {
				y: CameraObject.camera.y + 68.8,
				x: CameraObject.camera.x + 66.2,
				ease: Power1.easeInOut
			});	
			tweentxtpers = TweenLite.to(thiss.txt_pers, 0.1, {
				y: CameraObject.camera.y - 365,
				x: CameraObject.camera.x - 43,
				ease: Power1.easeInOut
			});	
}
}


function tr() //encrage
{
for (var i = 0; i < 3; i++) {
var tmpPipe = thiss.tri.getChildAt(i);
var pt = thiss.perso.localToLocal(0, 0, tmpPipe);
var az;
if (delta > 0){az = 0}else{az = 0}
if  (tmpPipe.hitTest(pt.x + az, pt.y  )){
	if (tmpPipe.name == "trig1")
		{
			if (delta > 0) {
				thiss.perso.gotoAndStop(0)
				thiss.txt_pers.gotoAndStop(0)
				}
			if (delta < 0) {thiss.perso.gotoAndStop(1)
				thiss.txt_pers.gotoAndStop(1)}
		}
	if (tmpPipe.name == "trig2")
		{
			if (delta > 0) {thiss.perso.gotoAndStop(1)
				thiss.txt_pers.gotoAndStop(1)}
			if (delta < 0) {thiss.perso.gotoAndStop(2)
				thiss.txt_pers.gotoAndStop(2)}
		}
	if (tmpPipe.name == "trig3")
		{
			console.log(camY)
			if (delta > 0) {thiss.perso.gotoAndStop(2)
			thiss.txt_pers.gotoAndStop(2)
			thiss.txt_pers.visible=true;
			camX = 22
			camY = 6
			}
			if (delta < 0) {thiss.perso.gotoAndStop(3)
			thiss.txt_pers.visible=false;
			camX = 22
			camY = 20
			}
		}
	} 
	
}
}




function animation(e) //encrage
{

if(e >= 0){
objy = e;
}
else{
objy=ObjAnim.y;
}
delta = 0;
if (scrollLVL == 1){ //init ecran
scrollLVL = 0; // bloque le scroll
tweencam = TweenLite.to(CameraObject.camera, 0.5, {y: objy,ease: Power1.easeOut,onComplete: playa});
}

function playa()
{
ii = 0;
console.log(ObjAnim)
changecranX = ObjAnim.nominalBounds.width / (nbclic + 1);
ObjAnim.nxt.addEventListener("click", next);
//ObjAnim.gotoAndPlay(1)
}



}
function next(e) //encrage
{
	console.log (ii);
if (ii < nbclic){
console.log (changecranX);
TweenLite.to(ObjAnim, 0.5, {x: ObjAnim.x - changecranX,ease: Power1.easeOut,onComplete: nxt});
if (ii < nbclic -1 ){
TweenLite.to(ObjAnim.nxt, 0.5, {x: ObjAnim.nxt.x + changecranX, ease: Power1.easeOut});	
}
ii++;
}

function nxt() //encrage
{
if (ii == nbclic){
ObjAnim.nxt.removeAllEventListeners("click");
//TweenLite.to(ObjAnim.nxt.nxt, 0.5, {rotation:90});
scrollLVL = 1
}
}
}
////////////////////////////////////////////////////////////////////////////// Init Route A /////////////////////////////////////////////////////////////////////

function initrouteA(){
ecart = 200;
route = 1;
scrollLVL = 1;
niveau1 = thiss.ecr1
Hmax = niveau1.nominalBounds.height;
niveauY[1] = niveau1.fx1.y;
niveauY[2] = niveau1.fx2.y;
niveauY[3] = niveau1.fx3.y;
//btn_av.removeAllEventListeners("click");
niveau1.likeFB.addEventListener("click", fl_likeFB.bind(this));

createjs.Ticker.addEventListener("tick", handleTick2);
createjs.Ticker.addEventListener("tick", handleTick2);
scrollLVL = 1;
}

function initcam()
{
StageInfos = [lib.properties.width, lib.properties.height]; //récupération des infos turbo
CameraObject = AdobeAn.VirtualCamera.getCamera(exportRoot); //variable caméra
CameraObject.camera.y = StageInfos[1]/2
}

function initecran()
{
scrollLVL = 0;
initX = CameraObject.camera.x;
initcontrol();
initrouteA()
}





function ecran1() //encrage
{

if ( ( (minimum - ecart) < niveau1.y && niveau1.y < minimum && delta < 0)  /* scroll bas */ 
 || 
 ( (maximum - ecart)  < niveau1.y && niveau1.y < maximum && delta > 0) )  /* scroll haut */    
{
if (scrollLVL == 1){ //init ecran
btn_bas.visible=false; // masque indicateur scroll bas
btn_av.visible=true; // affiche l'indicateur de clic à droite
//btn_av.removeAllEventListeners("click");
btn_av.addEventListener("click", fl_slide.bind(this));
lecturevertical.x= lib.properties.width / 2 ; // largeur de la case / 2
scrollLVL = 0; // bloque le scroll

TweenLite.to(niveau1, 0.5, {y: niveauY[ecran_1],ease: Power1.easeOut});
}

function fl_slide()
{
	console.log("click")
	dim = lecturevertical.nominalBounds.width;
	console.log(dim)
	console.log(-(dim)+(dim/nbclic ))
	console.log(lecturevertical.x );
	console.log(niveau1.x )
	if (lecturevertical.x > -(dim)+(dim/nbclic )){
		console.log("click2")
	TweenLite.to(lecturevertical, 0.5, {x:lecturevertical.x - (dim/(nbclic+1)),ease: Power1.easeOut,onStart: disableclick ,onComplete: checkclic});
	}
}

function  disableclick()
{
niveau1.btn_av.removeAllEventListeners("click");

}

function  checkclic()
{
	niveau1.btn_av.addEventListener("click", fl_slide.bind(this));
if (niveau1.ecr1.x == -(dim)+(3*lib.properties.width / 2) ){
	scrollLVL = 1;
	niveau1.btn_bas.visible=true;
	niveau1.btn_av.visible=false;
	delta = 0;
	}
}

}
}


function ecran3() //parallax
{
var max= -2440 //position cran dans le scroll
var min=-2130 //position cran dans le scroll
if (max < niveau1.y && niveau1.y < min)
{
var parmin=2687.8 // 
var parmax=2783.4 //
var deltapar = (parmax - parmin)*((min - niveau1.y)/(min - max))
TweenLite.to(niveau1.paral1, 0.3, {y: parmin + deltapar ,ease: Power1.easeOut});
}
}

function ecran4() //encrage
{
var niveauY = -3276;
var min= -2740 //position cran dans le scroll
var max=-3486 //position cran dans le scroll
var ecart = 400;
if ( ( (min - ecart) < niveau1.y && niveau1.y < min && delta < 0)  || ( (max - ecart)  < niveau1.y && niveau1.y < max && delta > 0) )       //zone haute
{
scrollLVL = 0;
//canvas.removeEventListener("DOMMouseScroll", fl_ClickToPosition);
//canvas.removeEventListener("mousewheel", fl_ClickToPosition);

TweenLite.to(niveau1, 0.5, {y: niveauY,ease: Power1.easeOut,onComplete: afterencrage});
}
}

function ecran5() //encrage + anim
{
niveauY = -4100;
var min= -3900 //position cran dans le scroll
var max=-4100 //position cran dans le scroll
var ecart = 200;	
var minreplay= -3020;
if ( (minreplay - ecart) < niveau1.y && niveau1.y < minreplay && delta < 0) 
{
niveau1.ec5.gotoAndStop(0);
}

if ((niveau1.y > (niveauY -100) && niveau1.y < (niveauY +100)) && (niveau1.ec5.currentFrame == 0 && delta > 0))
{
scrollLVL = 1;
}

if ((niveau1.y > (niveauY -100) && niveau1.y < (niveauY +100)) && ((!touchesupported && delta != 0) || (touchesupported && deltatact != 0)) && scrollLVL == 0)
{
if (touchesupported){
//console.log ("ecran5 tact : " + Math.max(-1, Math.min(1, -(delta))))


if (niveau1.ec5.currentFrame == 0 && delta > 0)
{
}
else {
delta = -deltatact;
TweenLite.to(niveau1, 0.3, {y: niveauY ,ease: Power1.easeOut });
niveau1.ec5.gotoAndPlay(niveau1.ec5.currentFrame + Math.max(-1, Math.min(1, -(delta))));
delta = 0;
deltatact = 0
}

//console.log(niveau1.ec5.currentFrame)
}
else{
//console.log("dans ecran 5, delta " + Math.max(-1, Math.min(1, -(delta))))
//delta = Math.max(-1, Math.min(1, -(delta)));
if (niveau1.ec5.currentFrame == 0 && delta > 0)
{
}
else {
niveau1.ec5.gotoAndPlay(niveau1.ec5.currentFrame + Math.max(-1, Math.min(1, -(delta))));
}
delta = 0;
deltatact = 0
//console.log(niveau1.ec5.currentFrame)
}
}


if ( ( (min - ecart) < niveau1.y && niveau1.y < min && delta < 0))//  || ( (max - ecart)  < niveau1.y && niveau1.y < max && delta > 0) )       //zone haute
{
//console.log("ecran5")
scrollLVL = 0;
//delta = 0;
deltatact = 0
//niveau1.ec5.play();
niveau1.ec5.gotoAndStop(0);
TweenLite.to(niveau1, 1, {y: niveauY,ease: Back.easeOut.config(1.4)});
}
}

function playanim() //réactive la molette après ancrage
{
	//niveau1.ec5.gotoAndPlay(1);
}

function afterencrage() //réactive la molette après ancrage
{
	scrollLVL = 1;
}

function removeniv() //réactive la molette après ancrage
{
	thiss.removeChild(oldniv);
	Hmax = -Hauteur[niveau] + HT;
	changzone = 0;
}

function nextniveau(){
	changzone=1;
niveau1.nextniv.removeAllEventListeners("click");
if (niveau > 0) {
niveau1.prevniv.removeAllEventListeners("click");
}
zone[niveau+1].x = 1080;
//thiss.addChild(zone[niveau+1]);
TweenLite.to(niveau1, 1, {x: -1082,ease: Back.easeOut.config(1.4)});
TweenLite.to(zone[niveau+1], 1, {x: 0,ease: Back.easeOut.config(1.4), onComplete: removeniv});
niveau1 = zone[niveau+1];
niveau++
if (niveau < (Hauteur.length-1)){
	niveau1.nextniv.addEventListener("click", nextniveau.bind(this));
	}
if (niveau > 0){
niveau1.prevniv.addEventListener("click", prevniveau.bind(this));
}
}

function prevniveau(){
	changzone = 1;
if (niveau != (Hauteur.length-1)){niveau1.nextniv.removeAllEventListeners("click");}
if (niveau > 0) {niveau1.prevniv.removeAllEventListeners("click");}

zone[niveau-1].x = -1082;
//zone[niveau-1].y = -Hauteur[niveau-1] + HT;

//thiss.addChild(zone[niveau-1]);
TweenLite.to(zone[niveau-1], 1, {x: 0,ease: Back.easeOut.config(1.4)});
TweenLite.to(niveau1, 1, {x: 1080,ease: Back.easeOut.config(1.4), onComplete: removeniv});
niveau1 = zone[niveau-1];
niveau--
if (niveau < (Hauteur.length-1)){
	niveau1.nextniv.addEventListener("click", nextniveau.bind(this));
	}
if (niveau > 0){
	niveau1.prevniv.addEventListener("click", prevniveau.bind(this));
	}
}

function fl_retourB(evt){
thiss.debA.gotoAndStop(1);
delta = 0;
retour=0;
route=0;
scrollLVL = 0;
retour=1;
tweencam = TweenLite.to(CameraObject.camera, 5, {
				y: StageInfos[1]/2,
				ease: Power1.easeInOut, onComplete: rteB
			});

function rteB(){

thiss.gotoAndStop("RtB");



}

}
function fl_retourA(evt){
window.location.reload()
/*delta = 0;
retour=0;
route=0;
scrollLVL = 0;
tweencam = TweenLite.to(CameraObject.camera, 5, {
				y: StageInfos[1]/2,
				ease: Power1.easeInOut, onComplete: rteA
			});

function rteA(){
thiss.gotoAndStop(1);



}*/

}

function fl_likeFB(evt){
window.open("https://www.facebook.com/elyapoliceinvestigation/", "_blank");
}