// Calcium Gap Junctions
objref gapsca[SizeTotal*2]
// Random
objref randomStretch
objref randomLeavesDiameters
randomLeavesDiameters = new Random()

// Sets rresistance and potential to gaps.
// $1 - Gap resistance
// $2 - Potential
proc res_gap() {
    for i  = 2, NumberDendrites-2 {
        gaps[i].r = $1
        gaps[i].VoltageGap = $2
    }
}

// Changes height distribution of dendrites.
// $1 - Random seed
// $2 - Scale factor
proc stretch() { local i, j, scaleNew
    scaleNew = $2
    randomStretch = new Random($1)
    for i = 0, OriginalDendrite-2 dendrite[i] {
        for j = 0, n3d()-1 {
            if ( x3d(j) > 0.01 &&  y3d(j) > 0.01) {
                pt3dchange(j, x3d(j), y3d(j), z3d(j),  1.67/sqrt(scaleNew*sqrt(x3d(j)^2+y3d(j)^2))) //randomStretch.uniform(-1, 1)+z3d(j)
            } else {
                pt3dchange(j, x3d(j), y3d(j), z3d(j), 1.67/sqrt(scaleNew*sqrt(x3d(j)^2+1+y3d(j)^2))) // randomStretch.uniform(-1, 1)+z3d(j)
            }
        }
    }
	
	
}

// Changes height distribution of dendritic length.
// $1 -XY
// $2 - Z


// Gap junction modifying the Ca concentration
//****************************************************************************************
proc GapCaJunc() {
   dendrite[$1] { gapsca[$1] = new GapCa(0.5)}
    setpointer  gapsca[$1].gapCaP, dendrite[$2].cai(0.5) gapsca[$1].jd = 0
}

// Installation of two types ofGap junction,  GapJunction with another astrocyte (in LargeGlia) and autapse  Gap junctions
proc insertGapJunc() { local i
   // for i = 0, SizeTotal-2 {    
   //       LargeGlia[i] gapsca[i] = new GapCa(0.5)
   for i = 0, NumberDendrites-2  {
          dendrite[i] gapsca[i] = new GapCa(0.5)
		  gapsca[i].jd = 0
        }	
	for i = 0, NumberDendrites-2  {
	   dendrite[i] {insert CAl}
	   }
	for i = 0, NumberDendrites-3  {	  
       GapCaJunc(i,i+1)
	   GapCaJunc(i+1,i) 
	   }
}
    //   LargeGlia[i+1] for j = 0, n3d()-1 { 
	//   x1=x3d(j)
	//    y1=y3d(j)	
	//}
	//if (sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1)) < $2) {
    //    gapsca[i].jd = $1
	//	print i, sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1))
	//	} else {
	//	gapsca[i].jd = 0
	//	}

proc CaGapFlux() {
    ControlParameter=$2
	print "Contr=", ControlParameter, "jd=", jd
    if (ControlParameter < 0.5) {
	   // for i = 0, SizeTotal-2 {
		//LargeGlia[i] {
		for i = 0, NumberDendrites-2  {
	     dendrite[i] {
		  gapsca[i].jd = $1
          gapsca[i].ControlGap=ControlParameter 		  
		 }
		  }
	} else {
	    for i = 0, NumberDendrites-2  {
	     dendrite[i] {
		 gapsca[i].jd = $1
		 gapsca[i].ControlGap=ControlParameter 
		 }
	      }
	       } 
}
//********************************************************************************************************



// The procedure determines the geometry and biophysical properties of Leaves.
// $1 - Max diameter for uniform distribution
// $2 - Min diameter for uniform distribution
// $3 - Max length for uniform distribution
// $4 - Min length for uniform distribution
// $5 - Boolean. 0 - uniform distribution for diameters. 1 - distribution from file.
// $6 - DensityGluTransporters
// $7 - Membrane density conductance, mS/cm2
proc setLeaves() {
    // All density function of membrane kinetic  must be multiply by 1+D/2L to take into account the lateral surface 
    for i = 0, SizeTotal-1 LargeGlia[i] {
        if ($5 == 0) {
            diam = randomLeavesDiameters.uniform($2, $1)
        } else {
            diam = getDiameterFromDistr(diamDistrLeaves, diamStep)
        }
        
        L = randomLeavesDiameters.uniform($4, $3)
        g_pas = (1+diam/(2*L))*$7 //(1/11150)  // mho/cm2

        if (currentMechanismSetup == 1) {
            density_GluTrans=(1+diam/(2*L))*$6
        }
    }
}

// The procedure determines the geometry and biophysical properties of Stalks.
// $1 - Max diameter for uniform distribution
// $2 - Min diameter for uniform distribution
// $3 - Max length for uniform distribution
// $4 - Min length for uniform distribution
// $5 - Boolean. 0 - uniform distribution for diameters. 1 - distribution from file.
// $6 - DensityGluTransporters
proc setStalks() {
    for i = 0, SizeTotal-1 SmallGlia[i] {
        if ($5 == 0) {
            diam = randomLeavesDiameters.uniform($2, $1)
        } else {
            diam = getDiameterFromDistr(diamDistrStalks, diamStep)
        }

        L    = randomLeavesDiameters.uniform($4, $3)
        
        if (currentMechanismSetup == 1) {
            density_GluTrans=$6
        }
    }
}