

length_measurement_Lip=function(x)
{
	var path_Lip = $(x).get(0);
	var pathLen_Lip = path_Lip.getTotalLength();
	
	return pathLen_Lip;
}

copy_func_Lip=function(group3,group4)
{
	$(group3).clone().prop({ id: "new_group3" }).appendTo('.face');
	for (j1 = 0; j1 < group3.childElementCount; j1++){
		group3.children[j1].style.visibility='hidden';	
			}
			
	
	$(group4).clone().prop({ id: "new_group4" }).appendTo('.face');
	for (j2 = 0; j2 < group4.childElementCount; j2++){
		group4.children[j2].style.visibility='hidden';	
			}
	Resault_Lip = [];
	new_group3.children[20].style.visibility='hidden';	//21 shaddow
	Resault_Lip[0] = new_group3;
	Resault_Lip[1] = new_group4;
	return Resault_Lip
	
}

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

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

small_morph_visible_Lip = function(element)
{	
	KUTE.to(element, {svgTransform:{scale:0.01}} , {duration: 1 ,onComplete:function(){Visible_Function_Lip(element);}}).start();

}

small_morph_hidden_Lip = function(element)
{
	KUTE.to(element, {svgTransform:{scale:0.01}} , {duration: 1,onComplete:function(){Hidden_Function_Lip(element)}}).start();
}

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



Hide_Function_Lip = function(real1_Lip,real2_Lip)
{
	for (j3 = 0; j3 < real2_Lip.childElementCount; j3++){
		real2_Lip.children[j3].style.visibility='visible';	
	}
	//real2_Lip.children[21].style.visibility='visible'
	new_group3.remove();
	new_group4.remove();
	//new_group1.remove();
	//new_group2.remove();
	//console.log("3");
}	


async function Morphing_part_one_Lip(new_group3,new_group4,a_g1_new_Lip,a_g2_new_Lip){
	
    return new Promise((resolve, reject) => {
		
			//console.log('part one')
			if (a_g2_new_Lip.length!=0){
				
				for (k4 = 0; k4 < a_g2_new_Lip.length; k4++){
				
					Visible_Function_Lip(new_group4.children[a_g2_new_Lip[k4]-1]);
				}
						
					resolve();
			}
			else {resolve();} ;
			
				
		});
}
			
async function Morphing_part_two_Lip(new_group3,new_group4,a_g1_new_Lip,a_g2_new_Lip,Time){
	
	let result = await Morphing_part_one_Lip(new_group3,new_group4,a_g1_new_Lip,a_g2_new_Lip);
	//console.log('part 2')

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

		
			if (a_g1_new_Lip.length!=0){
				
				for (j5 = 0; j5 < a_g1_new_Lip.length; j5++){
					//console.log('ss')
					if (a_g1_new_Lip[j5]!=2 && a_g1_new_Lip[j5]!=4){KUTE.to(new_group3.children[a_g1_new_Lip[j5]], {path:new_group4.children[a_g1_new_Lip[j5]-1]},{duration: Time,onComplete:function(){}}).start();}//3 dandun bala 5 dandun paiin

					// 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_Lip(new_group3,new_group4,a_g1_new_Lip,a_g2_new_Lip,intersection_set_Lip,Time){
	
    //let result = await Morphing_part_two_Lip(new_group3,new_group4,a_g1_new_Lip,a_g2_new_Lip,Time);
	Morphing_part_two_Lip(new_group3,new_group4,a_g1_new_Lip,a_g2_new_Lip,Time)
	//console.log('part3')
	
				
			//new_group4.children[21].style.visibility='hidden';	
			//new_group4.children[22].style.visibility='hidden';	
			
	return new Promise((resolve, reject) => {
			
			for (j6 = 0; j6 < a_g2_new_Lip.length; j6++){
				
			 if (a_g2_new_Lip[j6]==2 || a_g2_new_Lip[j6]==4){//3 dandun bala 5 dandun paiin
				//new_group4.children[a_g2_new_Lip[j6]].style.visibility="visible";
				KUTE.to(new_group3.children[a_g2_new_Lip[j6]-1], {path:new_group4.children[a_g2_new_Lip[j6]]},{duration: 1, morphPrecision:5}).start();
			}
			
			else {KUTE.to(new_group3.children[a_g2_new_Lip[j6]-1], {path:new_group4.children[a_g2_new_Lip[j6]]},{duration: Time, morphPrecision:5,onComplete:function(){}}).start();};
			
			
			}
			for (j7 = 0; j7 < intersection_set_Lip.length-1; j7++){
				
				//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_group3.children[intersection_set_Lip[j7]], {path: new_group4.children[intersection_set_Lip[j7]] }, {duration: Time, morphPrecision:5,onComplete:function(){resolve();}}).start();	
			}
			
			j7 = intersection_set_Lip.length-1;
			
			//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_group3.children[intersection_set_Lip[j7]], {path: new_group4.children[intersection_set_Lip[j7]] }, {duration: Time, morphPrecision:5, onComplete:function(){ resolve();} }).start();	
			
			
    });
}


async function Morphing_part_four_Lip(new_group3,new_group4,a_g1_new_Lip,a_g2_new_Lip,intersection_set_Lip,real1_Lip,real2_Lip,Time){
	
	let result = await Morphing_part_three_Lip(new_group3,new_group4,a_g1_new_Lip,a_g2_new_Lip,intersection_set_Lip,Time);
	//console.log('part 4')
	Hide_Function_Lip(real1_Lip,real2_Lip);
	return new Promise((resolve, reject) => {resolve();});
};


async function Morphing_part_five_Lip(X,Time_Series){
	
	q=X.length-1;
	
	while (q<2*(X.length-1)){
		copy_func_Lip(X[q-(X.length-1)],X[q-(X.length-2)]);
	    morphing_func_Lip(new_group3,new_group4,X[q-(X.length-1)],X[q-(X.length-2)]);
		
		await Morphing_part_four_Lip(new_group3,new_group4,a_g1_new_Lip,a_g2_new_Lip,intersection_set_Lip,X[q-(X.length-1)],X[q-(X.length-2)],Time_Series[q-(X.length-1)]);	
		//console.log('part 5')
		q=q+1;  
	};
	
}

async function Morphing_part_six_Lip(X,Time_Series){
	Flag_Finish=0
	second_position_mouth = 0;
	first_position_mouth = 0;
	await Morphing_part_five_Lip(X,Time_Series)
	Flag_Finish=1
	Flag_sentence=0
};


async function Morphing_part_seven_Lip(X,Time_Series){
	Flag_Finish_Emotion=0
	await Morphing_part_five_Lip(X,Time_Series)
	Flag_Finish_Emotion=1
};


morphing_func_Lip=function(new_group3,new_group4,real1_Lip,real2_Lip)
{

	// n = new_group1.childElementCount;
	n_Lip = 23;//in mishe 10
	a_g1_Lip = [];
	a_g2_Lip = [];	
	for (i1 = 0; i1 < n_Lip;) {// az 1 shoru bshe ta 
		if (length_measurement_Lip(new_group3.children[i1])>8){//inm bshe 2i
			a_g1_Lip.push(i1);
		}//end g1 if
		
		if (length_measurement_Lip(new_group4.children[i1])>8){//inm bsh 2i
			a_g2_Lip.push(i1);
		}//end g2 if
		i1=i1+1;		
		}
		intersection_set_Lip = a_g1_Lip.filter(value => -1 !== a_g2_Lip.indexOf(value));
		a_g1_new_Lip = a_g1_Lip.filter(x => !intersection_set_Lip.includes(x) );
		a_g2_new_Lip = a_g2_Lip.filter(x => !intersection_set_Lip.includes(x) );
		
		}

		
// group_name = [a,aa,e,ee,o,oo,b,p,t,s,j,ch,h,kh,d,z,r,zh,gh,f,k,g,l,m,n,v,y,normal]
// for (ik = 0; ik < group_name.length; ik++){
	// for (jk=0; jk<group_name[ik].childElementCount;jk++){
		// if (length_measurement_Lip(group_name[ik].children[jk])==0){console.log(group_name[ik].children[jk].id)};
	// };

// }
