function sleep(milliseconds) {
	
	
  const date = Date.now();
  let currentDate = null;
  do {
    currentDate = Date.now();
  } while (currentDate - date < milliseconds);
}





function asleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}


length_measurement_Face=function(x)
{
	var path_Face = $(x).get(0);
	var pathLen_Face = path_Face.getTotalLength();
	
	return pathLen_Face;
}

copy_func_Face=function(group1,group2)

{	//if (Flag_Finish==0){nn=22}
	//else {nn=45}
	nn=22
	$(group1).clone().prop({ id: "new_group1" }).appendTo('.face');
	for (jj1 = 0; jj1 < nn; jj1++){
		group1.children[jj1].style.visibility='hidden';	
			}
	
	$(group2).clone().prop({ id: "new_group2" }).appendTo('.face');
	for (jj2 = 0; jj2 < nn; jj2++){
		group2.children[jj2].style.visibility='hidden';	
			}
	Resault_Face = [];
	
	Resault_Face[0] = new_group1;
	Resault_Face[1] = new_group2;
	return Resault_Face
	
}

Visible_Function_Face=function(element)
{
	element.style.visibility='visible';	
}

Hidden_Function_Face=function(element)
{
	element.style.visibility='hidden';	
}

small_morph_visible_Face = function(element)
{	
	KUTE.to(element, {svgTransform:{scale:0.01}} , {duration: 1 ,complete:function(){Visible_Function_Face(element);}}).start();

}

small_morph_hidden_Face = function(element)
{
	KUTE.to(element, {svgTransform:{scale:0.01}} , {duration: 1,complete:function(){Hidden_Function_Face(element)}}).start();
}

big_morph_Face = function(element,Time)
{
	KUTE.to(element, {svgTransform:{scale:1}},{duration: Time}).start();
}


Hide_Function_Face = function(real1_Face,real2_Face)
{
	for (jj3 = 0; jj3 < nn; jj3++){
		real2_Face.children[jj3].style.visibility='visible';	
	}
	new_group1.remove();
	new_group2.remove();
	//new_group1.remove();
	//new_group2.remove();
	//console.log("3");
}	


async function Morphing_part_one_Face(new_group1,new_group2,a_g1_new_Face,a_g2_new_Face){
	
    return new Promise((resolve, reject) => {
		
			//console.log('part one')
			if (a_g2_new_Face.length!=0){
				
				for (kk4 = 0; kk4 < a_g2_new_Face.length; kk4++){
				
					Visible_Function_Face(new_group2.children[a_g2_new_Face[kk4]-1]);
				}
				
						
					resolve();
			}
			else {resolve();} ;
			
				
		});
}
			
async function Morphing_part_two_Face(new_group1,new_group2,a_g1_new_Face,a_g2_new_Face,Time){
	
	let result = await Morphing_part_one_Face(new_group1,new_group2,a_g1_new_Face,a_g2_new_Face);
	//console.log('part 2')

    return new Promise((resolve, reject) => {
		

		
			if (a_g1_new_Face.length!=0){
				
				for (jj5 = 0; jj5 < a_g1_new_Face.length; jj5++){
					//console.log('ss')
					if (a_g1_new_Face[jj5]!=24 && a_g1_new_Face[jj5]!=26){KUTE.to(new_group1.children[a_g1_new_Face[jj5]], {path:new_group1.children[a_g1_new_Face[jj5]-1]},{duration: Time,onComplete:function(){}}).start();}

					// KUTE.to(new_group3.children[a_g1_new_Lip[j5]], {path:new_group3.children[a_g1_new_Lip[j5]-1]} , {duration: 1,easing: 'easingQuarticIn' , morphPrecision:1}).start();
				// }

				//j5 = a_g1_new_Lip.length-1;
				
				// KUTE.to(new_group3.children[a_g1_new_Lip[j5]], {path:new_group3.children[a_g1_new_Lip[j5]-1]} , {duration: 1,easing: 'easingQuarticIn', morphPrecision:1,onComplete:function(){
					
						
						// for (k5 = 0; k5 < a_g1_new_Lip.length; k5++){
				
						// Hidden_Function_Lip(new_group3.children[a_g1_new_Lip[k5]]);
			
						// }
						
						// resolve();
						
				// } }).start();						
			// 
			};
			resolve();}
			
			else { resolve();}
			
   });
}


async function Morphing_part_three_Face(new_group1,new_group2,a_g1_new_Face,a_g2_new_Face,intersection_set_Face,Time){
	
    //let result = await Morphing_part_two_Lip(new_group3,new_group4,a_g1_new_Lip,a_g2_new_Lip,Time);
	Morphing_part_two_Face(new_group1,new_group2,a_g1_new_Face,a_g2_new_Face,Time)
	//console.log('part3')
	
				
			//new_group4.children[21].style.visibility='hidden';	
			//new_group4.children[22].style.visibility='hidden';	
			
	return new Promise((resolve, reject) => {
			
			for (jj6 = 0; jj6 < a_g2_new_Face.length; jj6++){
				
			 if (a_g2_new_Face[jj6]==24 || a_g2_new_Face[jj6]==26 ){ 
				//new_group4.children[a_g2_new_Lip[j6]].style.visibility="visible";
				KUTE.to(new_group1.children[a_g2_new_Face[jj6]-1], {path:new_group2.children[a_g2_new_Face[jj6]]},{duration: 1, morphPrecision:10}).start();
			}
			else if (a_g2_new_Face[jj6]==1 || a_g2_new_Face[jj6]==3){KUTE.to(new_group2.children[a_g2_new_Face[jj6]-1], {path:new_group2.children[a_g2_new_Face[jj6]]},{duration: Time, morphPrecision:10}).start();}
			
			else {KUTE.to(new_group1.children[a_g2_new_Face[jj6]-1], {path:new_group2.children[a_g2_new_Face[jj6]]},{duration: Time, morphPrecision:10,onComplete:function(){}}).start();};
			
			
			}
			for (jj7 = 0; jj7 < intersection_set_Face.length-1; jj7++){
				
				if (([intersection_set_Face[jj7]].filter(value => -1 !== [1,3].indexOf(value))).length==0){
				//idx_Lip = pathCross(document.getElementById(new_group3.children[intersection_set_Lip[j7]].id),document.getElementById(new_group4.children[intersection_set_Lip[j7]].id));	
				KUTE.to(new_group1.children[intersection_set_Face[jj7]], {path: new_group2.children[intersection_set_Face[jj7]] }, {duration: Time, morphPrecision:10,onComplete:function(){}}).start();	
				}
				else{	
				
				KUTE.to(new_group1.children[1], {path:new_group1.children[1-1]} , {duration: Time/2,onComplete:function(){Visible_Function_Face(new_group2.children[0]);KUTE.to(new_group2.children[0], {path:new_group2.children[1]} , {duration: Time/2}).start();}}).start();
				KUTE.to(new_group1.children[3], {path:new_group1.children[3-1]} , {duration: Time/2,onComplete:function(){Visible_Function_Face(new_group2.children[2]);KUTE.to(new_group2.children[2], {path:new_group2.children[3]} , {duration: Time/2,onComplete:function(){}}).start();}}).start();

	
				//new_group2.children[intersection_set_Face[jj7]-1].style.visibility="visible";
				}
			}
			
			jj7 = intersection_set_Face.length-1;
			

			KUTE.to(new_group1.children[intersection_set_Face[jj7]], {path: new_group2.children[intersection_set_Face[jj7]] }, {duration: Time, morphPrecision:10, onComplete:function(){ resolve();} }).start();	

			
			//idx_Lip = pathCross(document.getElementById(new_group3.children[intersection_set_Lip[j7]].id),document.getElementById(new_group4.children[intersection_set_Lip[j7]].id));	
			
			
			
    });
}



async function Morphing_part_four_Face(new_group1,new_group2,a_g1_new_Face,a_g2_new_Face,intersection_set_Face,real1_Face,real2_Face,Time){
	
	let result = await Morphing_part_three_Face(new_group1,new_group2,a_g1_new_Face,a_g2_new_Face,intersection_set_Face,Time);
	Hide_Function_Face(real1_Face,real2_Face);
	return new Promise((resolve, reject) => {
	resolve();
	key_w=1;});
};


async function Morphing_part_five_Face(X,Time_Series){
	
	qq=X.length-1;
	
	while (qq<2*(X.length-1)){
		copy_func_Face(X[qq-(X.length-1)],X[qq-(X.length-2)]);
	    morphing_func_Face(new_group1,new_group2,X[qq-(X.length-1)],X[qq-(X.length-2)]);
		
		await Morphing_part_four_Face(new_group1,new_group2,a_g1_new_Face,a_g2_new_Face,intersection_set_Face,X[qq-(X.length-1)],X[qq-(X.length-2)],Time_Series[qq-(X.length-1)]);	
		//console.log('part 5')
		qq=qq+1;  
	};
	
}


async function Morphing_part_six_Face(X,Time_Series){
	Flag_Finish_Face=0
	await Morphing_part_five_Face(X,Time_Series)
	Flag_Finish_Face=1
};



morphing_func_Face=function(new_group1,new_group2,real1_Face,real2_Face)
{
	//if (Flag_Finish==0){nn=22} //tedad ghabl az dahan
	//else {nn=45}// tedad kol
	nn=22
	
	a_g1_Face = [];
	a_g2_Face = [];	
	for (ii1 = 0; ii1 < nn;) {
		if (length_measurement_Face(new_group1.children[ii1])>9){
			a_g1_Face.push(ii1);
		}//end g1 if
		
		if (length_measurement_Face(new_group2.children[ii1])>9){
			a_g2_Face.push(ii1);
		}//end g2 if
		ii1=ii1+1;		
		}
		intersection_set_Face = a_g1_Face.filter(value => -1 !== a_g2_Face.indexOf(value));
		a_g1_new_Face = a_g1_Face.filter(x => !intersection_set_Face.includes(x) );
		a_g2_new_Face = a_g2_Face.filter(x => !intersection_set_Face.includes(x) );
		
		}

		