#############################################
 /date/vitals.js 
#############################################
/*   ()


  (     window)
value -   
importance -  
shift -   %       
*/
window.VitalsArr = [
  { Id: 0, value: 0, importance: 90, shift:  -0.05, name: ""},  //  /  
  { Id: 1, value: 0, importance: 80, shift: 0.001, name: "" },  //  /  
  { Id: 2, value: 0, importance: 70, shift: 0, name: "" },  // 
  { Id: 3, value: 0, importance: 60, shift: 0, name: "" },  // 
  { Id: 4, value: 0, importance: 60, shift: 0.005, name: "" },  //  ( )
  { Id: 5, value: 0, importance: 30, shift: -0.1, name: "" },  // 
  { Id: 6, value: 0, importance: 20, shift: 0.002, name: "" },  //  ( )
  { Id: 7, value: 0, importance: 50, shift: -1, name: "" },  // 
  { Id: 8, value: 0, importance: 100, shift: -0.05, name: "" }  // 
];

//   shift   ,   init_input_actions()
//window.VitalsShiftArr = Array(VitalsArr.length);

//  
window.IsSpeeping=false;


#############################################
 /date/BasicContexts.js 
#############################################
/*  

!     -    , .. 
    
    , 

autonomic - ,      getActiveBasicContexts() -   
*/

window.BasicContexts=[
{id:0,  importance:90, autonomic:false, name:""  }, //  /  
    //   Id:0 ().   , 
    //   ,       
    
 {id:1,  importance:80, autonomic:false, name:""  }, //   
    //   Id:1 ().    :
    //  ,    ,  2
    
{id:2,  importance:70, autonomic:false, name:""  }, // : 
    //   Id:2 ().  ,   ,
    //  ,   (  )
    
{id:3,  importance:70, autonomic:false, name:""   },// : 
    //   Id:3 ().   ,
    //   ( ),   
    
{id:4,  importance:60, autonomic:false, name:""   },// 
    //   Id:4 (). , : 
    //  ,  ,  
    
{id:5,  importance:30, autonomic:false, name:""   },//  ()
    //   Id:5 ().    
    //  -   ,  
    
{id:6,  importance:20, autonomic:false, name:""   },//  
    //   Id:6 ().  ,  ,
    // ,  
    
{id:7,  importance:50, autonomic:false, name:""   },//  
    //   Id:7 ().  ,   ,
    //   
    
{id:8,  importance:100, autonomic:false, name:""  },//    
    //   Id:8 ().    
    //   ,    

{id:9,  importance:10, autonomic:false, name:""  },//   / 
    //    , , 
    //   ,        ,   
    
{id:10, importance:30, autonomic:true, name:""   }// /   ( ), / .
    //       ,    ( /)     ,      ,     - .         .
]

//       true  false:
window.BasicContextsActived = Array(BasicContexts.length).fill(false); 



#############################################
 /sys/input_actions.js 
#############################################
/*  

          . 
  ,   , 
   ,       
  .  -  ,    , 
    . ,  " " 
      -  . 
           ..

vital -   id 
on -  -

shift -     .        ,       - .
  ,      . ,   -     "",     .

 name2="" -    . 
  (release_button(id))        2 . 

         . 
     : stop_all_action().

{Id: NN, vital:N, on:false, shift: N, name: "xxxx", name2: "xxxx"},// 
*/

window.InputActions = [
//_________________________________ 
{Id: 0, vital:0, on:false, shift: 1.0, name: " ", name2: " ", title:" "},
{Id: 1, vital:0, on:false, shift: 0.5, name: "  ", name2: " ", title:" "},
{Id: 2, vital:0, on:false, shift: 60.0, name: " ", name2: "", title:"   "},//  
//_________________________________  
{Id: 3, vital:1, on:false, shift: 0.5, name: " ", name2: " ", title:"      ."},// 
{Id: 4, vital:1, on:false, shift: 60, name: "", name2: ""},// 
{Id: 5, vital:1, on:false, shift: 0, name: " ", name2: " ", title:"   ."},//  
{Id: 6, vital:1, on:false, shift: 0, name: " ", name2: " ", title:"    ."},//  
//________________________________  
{Id: 7, vital:4, on:false, shift: 1, name: ".", name2: " ", title:"   "},// 
{Id: 8, vital:4, on:false, shift: 60, name: "", name2: "    ."},// 
{Id: 9, vital:4, on:false, shift: 0, name: " ", name2: " ", title:"   ."},// 
{Id: 10, vital:4, on:false, shift: 0, name: " ", name2: " ", title:""},// 
//________________________________ 
{Id: 11, vital:2, on:false, shift: 60, name: "", name2: "", title:"   ."},//
{Id: 12, vital:2, on:false, shift: 1, name: "", name2: " ", title:"  ."},//
//________________________________ 
{Id: 13, vital:3, on:false, shift: 60, name: "", name2: "", title:"   ."},//
{Id: 14, vital:3, on:false, shift: 1, name: "", name2: " ", title:"   ."},//
//________________________________ 

{Id: 15, vital:5, on:false, shift: 60, name: " ", name2: "", title:""},//
{Id: 16, vital:5, on:false, shift: 60, name: " ", name2: "", title:""},//
//________________________________ 

{Id: 17, vital:6, on:false, shift: 10, name: "", name2: " ", title:"  ."},//
{Id: 18, vital:6, on:false, shift: 0, name: " ", name2: " ", title:"    ."},//
//________________________________ 
{Id: 19, vital:7, on:false, shift: 60, name: "", name2: "", title:"    ."},// 
{Id: 20, vital:5, on:false, shift: 10, name: "", name2: "", title:"  ."},//
//________________________________ 
{Id: 21, vital:8, on:false, shift: 60, name: "", name2: "", title:" ."},//
{Id: 22, vital:8, on:false, shift: 0, name: "", name2: "", title:" ."},//

//________________________________   vital:-1
{Id: 23, vital:-1, on:false, shift: 0, name: " ", name2: "", title:" "},//
{Id: 24, vital:-1, on:false, shift: 0, name: " ", name2: "", title:" "},//
{Id: 25, vital:-1, on:false, shift: 0, name: " ", name2: "", title:" "},//
{Id: 26, vital:5, on:false, shift: -10, name: " ", name2: "", title:""},//


//     :    0.005,    0.004
{Id: 1000, vital:4, on:false, shift: -0.004, outID:1000, name: " ", name2: "", title:"   "},
];

////////////  
function init_input_actions()
{
var acts="";
for( var i = 0; i < InputActions.length; i++) 
{
var brd="";
if(InputActions[i].name2.length==0)
	brd=" style='border:solid 1px red;' ";
if(InputActions[i].vital==-1)
	brd=" style='border:solid 1px blue;' ";
acts += `
	<div id="input_action-item_${i}" class="input_action-item" ${brd} onclick="onInput_actionClick(${i})" title="${InputActions[i].title}">
        ${InputActions[i].name}
    </div>
`;
}
	
document.getElementById('input_actions_id').innerHTML = acts;
}
////////////////////////////////////////




////////////////////////////////////////
/*   

*/
function onInput_actionClick(i)
{
//	alert(i+" | "+InputActions[i].shift);
var btn=document.getElementById('input_action-item_'+i); 
if(InputActions[i].on)//  
{  //alert("!!!!  "+i);
btn.style.backgroundColor="#eeeeee";
btn.innerHTML=InputActions[i].name;
InputActions[i].on=false;
if(InputActions[i].Id==1000)// 
{
IsSpeeping=false;
BasicContextsActived[10] = false;
setTextInfo("","");
}

}
else//   
{
IsActualStimul=true;
//    
InputActionsPrev.impID=InputActionsCur.impID;
InputActionsPrev.context=InputActionsCur.context;
InputActionsPrev.actionID=InputActionsCur.actionID;
InputActionsCur.impID=InputActions[i].Id;
InputActionsCur.context=get_cur_context();

stop_all_action(); //   
btn.style.backgroundColor="#E1E4FF";
if(InputActions[i].name2.length==0)
	btn.innerHTML="  ";
else
	btn.innerHTML=InputActions[i].name2;
InputActions[i].on=true;
if(InputActions[i].Id==1000)// 
{ //alert(IsSpeeping);
IsSpeeping=true;
BasicContextsActived[10] = true;
setTextInfo(""," ,     .");
}
}

}
//////////////////////////////////////////

function get_sleep_id()
{
for( var i = 0; i < InputActions.length; i++)
	{
if(InputActions[i].Id == 1000)
	return i;
	}

}
///////////////////////////////////////


///////////////////////////////////////////
/*      
  
*/
function release_button(id)
{
InputActions[id].on=false;
var btn=document.getElementById('input_action-item_'+id);  
btn.style.backgroundColor="#eeeeee";
btn.innerHTML=InputActions[id].name;
//addVitalsShift(btn.vital,-btn.shift);
}
///////////////////////////////////////////
//        
function release_one_time_buttons()
{
for( var i = 0; i < InputActions.length; i++)
{
	if(InputActions[i].on && InputActions[i].name2.length==0)
	{
		release_button(i);
	}
}
}
///////////////////////////////////////////////

/*     .      .
*/
function stop_all_action()
{
for( var i = 0; i < InputActions.length; i++)
{
if(InputActions[i].on)
	{
		release_button(i);
	}
}
}

//   -  
function get_any_input()
{
for( var i = 0; i < InputActions.length; i++)
{
if(InputActions[i].on)
	{
		return true;
	}
}
}



#############################################
 /sys/output_actions.js 
#############################################
/*   

vital -       shift  losses
        .      window.ActionsFromContext
+/-shift -      vital  -  VitalsArr[n].value
losses -       - function losses_per_action(losses) -    
*/
window.OutputActions = [ 
{Id: 0, vital:0, shift: 0, losses:1, 
name: "", 
descr: ",  ,    ."},
// ( /)
{Id: 1, vital:0, shift: -2, losses:0.1, 
name: "  .   ", 
descr: " ,   "},

{Id: 2, vital:0, shift: 0, losses:0.3, 
name: "      ", 
descr: " ,   "},
//______________________________________ 
{Id: 3, vital:1, shift: -10, losses:0.1, 
name: " ", 
descr: " "},

{Id: 4, vital:1, shift: 0, losses:0.3, 
name: "   ", 
descr: " ,   "},

{Id: 5, vital:1, shift: 0, losses:0.3, 
name: "    ", 
descr: " ,   "},

{Id: 6, vital:1, shift: 0, losses:0.3, 
name: "   ", 
descr: "  "},
//______________________________________ 

{Id: 7, vital:4, shift: -10, losses:0.1, 
name: "", 
descr: " "},

{Id: 8, vital:4, shift: 0, losses:0.3, 
name: " ", 
descr: "  "},

{Id: 9, vital:4, shift: 0, losses:0.3, 
name: "  ", 
descr: "    ."},

{Id: 10, vital:4, shift: 0, losses:0.2, 
name: "  ", 
descr: "   ."},
//______________________________________ 


{Id: 11, vital:2, shift: -0.1, losses:0.5, 
name: " ", 
descr: " ,   ."},
//______________________________________ 


{Id: 12, vital:3, shift: 0, losses:0.01, 
name: "", 
descr: " ."},

{Id: 13, vital:4, shift: -0.1, losses:0.3, 
name: ",  ,  ", 
descr: " .   ."},
//______________________________________ 

{Id: 14, vital:5, shift: 0, losses:0.05, 
name: "  ", 
descr: "  ,  ."},
//______________________________________ 

{Id: 15, vital:6, shift: 0, losses:0.05, 
name: "  ", 
descr: "    ."},

{Id: 16, vital:6, shift: -10, losses:0.5, 
name: " ", 
descr: "      ."},
//______________________________________ 

{Id: 17, vital:7, shift: 0, losses:0.3, 
name: "  ", 
descr: "  , ."},

{Id: 18, vital:7, shift: 0, losses:0.5, 
name: "  ", 
descr: "      ."},
//______________________________________ 
{Id: 19, vital:8, shift: -0.5, losses:0.05, 
name: " ", 
descr: " .         .    ."},


//______________________________________   
{Id: 20, vital:7, shift: 10, losses:0.05, 
name: "", 
descr: ", ."},

{Id: 21, vital:7, shift: -10, losses:0.01, 
name: "", 
descr: "  ."},

];
////////////////////////////////////////////////////////////////////




///////////////////////////
/*  sep_of_puls()  
  .       -  , 
    Beast.

  :
1)      ,      (OutputActions). 
    .,  window.ActionsFromContext
2)         ,  OutputActions.
3)  :   ,     -    OutputActions,    .

/ -     (    ,  ).
  watchdog_waking_up() -  getActiveBasicContexts()

    ,   .

 actionID     -1
*/
function output_action_and_info()
{ 
//        sep_of_puls()   

//        
		//    :
		if (InputActions[2].on) { // alert("!!!!");
		  watchdog_waking_up();// 
		  run_reaction(0); // 
		  return 0;
		}
		//   :
		if (InputActions[4].on) { // alert("!!!!");
		  watchdog_waking_up();// 
		  run_reaction(0); // 
		  return 0;
		}
//   :
		if (InputActions[8].on) { // alert("!!!!");
		  watchdog_waking_up();// 
		  run_reaction(8); // 
		  return 8;
		}
		//   :
		if (InputActions[11].on) { // alert("!!!!");
		  watchdog_waking_up();// 
		  run_reaction(0); // 
		  return 0;
		}
		//   :
		if (InputActions[13].on) { // alert("!!!!");
		  watchdog_waking_up();// 
		  run_reaction(12); // 
		  return 12;
		}
//   :
		if (InputActions[17].on) { // alert("!!!!");
		  watchdog_waking_up();// 
		  run_reaction(15); //   
		  return 15;
		}
// :
		if (InputActions[19].on) { // alert("!!!!");
		  watchdog_waking_up();// 
		  run_reaction(18); //   
		  return 18;
		}
// :
		if (InputActions[21].on) { // alert("!!!!");
		  watchdog_waking_up();// 
		  run_reaction(0); // 
		  return 0;
		}


// ctxId -  
for (let ctxId = 0; ctxId < BasicContextsActived.length; ctxId++) {

    if (!BasicContextsActived[ctxId]) 
		continue;
// console.log(ctxId);
    //   
    if (ctxId === 10) {
      setTextInfo("", "    ().");
      return -1;
    }

//      
    for (const { inputId, outputId } of ActionsFromContext[ctxId]) {
	//	console.log(ctxId+" | "+inputId+" | "+outputId);
      if (InputActions[inputId] && InputActions[inputId].on) {//   
		//console.log(ctxId+" | "+inputId+" | "+outputId);
          run_reaction(outputId);
          return outputId;
      }
    }

//console.log(ctxId+" ");
	//////////////////////
    //   ,       fallback-

	if(ctxId === 0)// 
	{ 
		const vital=VitalsArr[0].value; //console.log(ctxId+" | "+vital);
		//   :
		if (vital >= 50 && vital < 60) {
		  run_reaction(2); //    
		  return 2;
		}  
		if (vital  >= 60 && vital < 90) {
		  run_reaction(1); //   .   
		  return 1;
		}
	}
	if(ctxId === 2)// 
	{
			run_reaction(11); // 
		  return 11;
	}
	if(ctxId === 3)// 
	{
			run_reaction(13); // 
		  return 13;
	}
	if(ctxId === 8)// 
	{
			run_reaction(19); // 
		  return 19;
	}

if(get_any_input())
setTextInfo(" ","        .")

return -1;
}
////////////////////////////


////////// 
if(BasicContextsActived[10])
{
//console.log("");
return -1; //     
}


////////// 
if(BasicContextsActived[9])
{

}

///          ,   !

return -1;
}
//////////////////////////////

//   
function run_reaction(outputId)
{  
//  console.log(outputId);
const act=OutputActions[outputId]; //if(outputId==0) alert(""+outputId );
//   
		  shift_per_action(act.vital,act.shift);
		  //      
		  losses_per_action(act.losses);
		  //  
          setOutputInfo(outputId);
		//  console.log(outputId);
/* 		  release_one_time_buttons();//       
        sep_of_puls()    */
}
/*       
shift -           

*/
function shift_per_action(vitalID,shift)
{
if (VitalsArr[vitalID].value < 100) {
    VitalsArr[vitalID].value = Math.max(0, VitalsArr[vitalID].value + shift);
  }
}
/*"   "
losses -     -  , -     
*/
function losses_per_action(losses)
{
// :    -  
  VitalsArr[0].value = Math.min(100, VitalsArr[0].value + losses);
  // :   
  VitalsArr[4].value = Math.min(100, VitalsArr[4].value + losses / 5);
}



//////////////////   
function setOutputInfo(i)
{
document.getElementById('output_actions_id').innerHTML = "<div style='font-family:arial;font-size:14pt;'><b>"+OutputActions[i].name+"</b></div><div>"+OutputActions[i].descr+"</div>";
}
function setTextInfo(text1,text2)
{
document.getElementById('output_actions_id').innerHTML = "<div style='font-family:arial;font-size:14pt;'><b>"+text1+"</b></div><div>"+text2+"</div>";

}

///////////////////////////////////////////////



#############################################
 /sys/output_from_context.js
#############################################
/*
       
   -    .

inputId - ID InputActions
outputId - ID OutputActions
*/
window.ActionsFromContext = [ 
// id=0:  -    0 ()
  [
    { inputId: 0, outputId: 1 }, // " " - "      "
    { inputId: 1, outputId: 1 },  // "  " - "  .   "
  //  { inputId: 2, outputId: 0 },  // " " - ""

  ],

  // id=1:  -    1 ()
  [
//    ,          . 
    { inputId: 6, outputId: 3}, //" " - " "
	{ inputId: 5, outputId: 6}, //" " - "   "
	{ inputId: 4, outputId: 4}, //" " - "   "
  ],

// id=2:  -  2 ()
  [
  { inputId: 12, outputId: 11}, //"" - " "
  ], //   InputActions  vital=2 - 

  // id=3:  -  3 ()
  [
  { inputId: 13, outputId: 12}, //"" - ""
  { inputId: 14, outputId: 13}, //"" - ""
  ],

    // id=4:  -  4 ()
  [
  { inputId: 7, outputId: 10}, //"." - "  "
    { inputId: 8, outputId: 8}, //"" - " "
	{ inputId: 9, outputId: 9}, //" " - "  "
	{ inputId: 10, outputId: 7}, //" " - ""
  ],

  // id=5:  -  5 ()
  [
  { inputId: 15, outputId: 14}, //" " - "  "
  { inputId: 16, outputId: 14}, //" " - "  "
  ],

  // id=6:  -  6 ()
  [
  { inputId: 18, outputId: 16}, //" " - " "
  ],

  // id=7:  -  7 ()
  [
  { inputId: 20, outputId: 17}, //"" - "  "
  ],

  // id=8:  -  8 ()
  [
    { inputId: 22, outputId: 19}, //"" - " "
  ],

  // id=9:  -   
  [
  { inputId: 20, outputId: 20}, //"" - ""
  { inputId: 26, outputId: 21}, //" " - ""
  ],

  // id=10:  -  
  []
];


#############################################
 /sys/condition_reflex.js
#############################################
/*  

    :
1.    () -   ()
	 ,     ( ).
2. ,        .
3.         .
4.        2 (news_detectior.go: if tempImg.motAutmtzmID > 2 -      rank int)),
    .
    ,     ,   - .
   :
1.     ( ,    )        . lastActivation int         ;
2.     ..       ..             ,    ..   .    (  ),   ,     .    .
3.      .       ,       ,           :   ,          ..        ,    , ,       .        ,       ,   .
4.         .  ""      ,   "",  -  (func conditionRexlexFound).

      
    ,
      .
*/
//   
window.CReflex = { 
	ID: -1, 
	context: "",// Id   -   -   
	inputID: -1,// Id InputActions -  
	actionID: -1,// Id OutputActions -  
	birthTime:-1,
	lastActivation:-1, //        		
	tryCount:0 //    inputID  actionID,     tryCount>3
	};

//   
window.NextReflexId = 0;
window.ConditionalReflexes = [];
//   : window.ConditionalReflexes.push(newReflex);

//   ,       .
window.InputActionsPrev={ impID: -1, context: "", actionID: -1 };
//     ,     InputActionsPrevID
window.InputActionsCur={ impID: -1, context: "", actionID: -1 };

//    
function condition_reflex_forming()
{
	if(InputActionsPrev.actionID>=0)//     
		return;
	if(InputActionsCur.actionID<0)//      
		return;		
	if(InputActionsPrev.impID<0)
		return;
// alert("!!!!!!!!!");
//    
const idx = findConditionalReflexIndex(InputActionsCur.context, InputActionsPrev.impID, InputActionsCur.actionID);
if (idx === -1) {

  //    ,  
  NextReflexId++;
ConditionalReflexes.push({
    ID: NextReflexId,
    context: InputActionsCur.context,
    inputID: InputActionsPrev.impID,
    actionID: InputActionsCur.actionID,
    birthTime: Cur_puls_val,
    lastActivation: Cur_puls_val,
    tryCount: 0
  });
	var out=OutputActions[InputActionsCur.actionID].name+"<br>"+OutputActions[InputActionsCur.actionID].descr;
	setTextInfo("    ",out);
//console.log(ConditionalReflexes);
} else {
  //       tryCount
  ConditionalReflexes[idx].tryCount++;
  ConditionalReflexes[idx].lastActivation = Cur_puls_val;
  var out=OutputActions[InputActionsCur.actionID].name+"<br>"+OutputActions[InputActionsCur.actionID].descr;
	setTextInfo("  ("+ConditionalReflexes[idx].tryCount+")   ",""+out);
}

}
///////////////////////////////////////////

/* ,   ConditionalReflexes    
   context, inputID  actionID, 
  ,     ID   ConditionalReflexes
  -1
*/
function findConditionalReflexIndex(context, inputID, actionID)
{
  for (let i = 0; i < ConditionalReflexes.length; i++) {
    const cr = ConditionalReflexes[i];
    if (
      cr.context === context &&
      cr.inputID === inputID &&
      cr.actionID === actionID
    ) {
//		console.log(context+", "+cr.context+" && "+inputID+", "+cr.inputID+" && "+actionID+", "+cr.actionID);
      return i; //    
    }
  }
//  console.log(ConditionalReflexes);
  return -1; //  
}
////////////////////////////////////////////



/*        inputID    get_cur_context()
 ,   .   sep_of_puls()  output_action_and_info().
   -  true,  - false
*/
function check_and_run_CReflex(inputID)
{
//	if(ConditionalReflexes.length) console.log(": "+ConditionalReflexes.length);
if(inputID<0)
	return false;

	const currentContext = get_cur_context(); // "0,2"  "none"  ..

//     
  for (let i = 0; i < ConditionalReflexes.length; i++) {
    const cr = ConditionalReflexes[i];
    
//console.log(cr.inputID +" == "+ inputID+" | "+cr.context +" == "+ currentContext+" | "+cr.tryCount);
    // :   ,    ""
    if (
      cr.inputID === inputID &&
      cr.context === currentContext &&
      cr.tryCount > 3 //    3+ 
    ) { 
// console.log(ConditionalReflexes);
      //   
      //run_reaction(cr.actionID);
	  var out=OutputActions[cr.actionID].name+"<br>"+OutputActions[cr.actionID].descr;
	  setTextInfo("   :",out);
      
	  cr.tryCount++;//  ,    -    
      //    
      cr.lastActivation = Cur_puls_val;
      
      return true; //  
    }
  }
  
  return false; //     
}
///////////////////////////////////////////////



#############################################
 /sys/insect.js 
#############################################
/*       

*/


/*   
      .
sep_of_puls()            ,          1 .
*/
window.Cur_puls_val=0;
//     
window.IsActualStimul=false;
function sep_of_puls()
{
//    
    const startTime = Date.now();

	//     
    performSensorOperations();//   
    performRegulationOperations();// - 
    getActiveBasicContexts();//  

if(IsActualStimul){
if(!check_and_run_CReflex(InputActionsCur.impID))//      
{  
//    
	var actonID=output_action_and_info();//   
 //console.log(actonID);
	
		InputActionsCur.actionID=actonID;
		condition_reflex_forming();//   
}
}

	//        
    release_one_time_buttons();

	//      1 
    const elapsedTime = Date.now() - startTime;
    //     1  -  
    //   -   
    const delayTime = elapsedTime >= 1000 ? 0 : 1000 - elapsedTime;


document.getElementById('pulsSum_id').innerHTML =Cur_puls_val;
	Cur_puls_val++;
	IsActualStimul=false;

	if(VitalsArr[0].value>99 
		|| VitalsArr[1].value>99 
		|| VitalsArr[2].value>99 
		|| VitalsArr[4].value>99 
		|| VitalsArr[8].value>99)
//	if(Cur_puls_val>3)
	{
fatalling();
return;// ...
	}
    //  
    setTimeout(sep_of_puls, delayTime);
}

//////////////////////////////////////////////////////////////////



///////////////// 
function setIndicationItems()
{
// 
var vi="";
for( var i = 0; i < VitalsArr.length; i++) 
{
if (!VitalsArr[i]) continue;

var vVal=roundToHundredths(VitalsArr[i].value);
//     placeholder    
  vi += `
    <div class="vital-item">
      <div id="vital-indicator_${i}" class="vital-indicator">
        0%
      </div>
      <span class="vital-label">${VitalsArr[i].name}</span>
    </div>
  `;
}
// 2.   DOM
document.getElementById('vital_info_id').innerHTML = vi;
/////////////////     ////////////////////////////
var bc="";
for( var i = 0; i < BasicContexts.length; i++) 
{
if (!BasicContexts[i]) continue;

var vVal=BasicContexts[i].name;
  bc += `
    <div id="basic-context_${i}" class="context-item">
        ${vVal}
    </div>
  `;
}
// 2.   DOM
document.getElementById('contexts_info_id').innerHTML = bc;
////////////  
init_input_actions();

///  
sep_of_puls();
}

setIndicationItems();
////////////////////////////////////////////////////////



//   
function performSensorOperations() {
    //console.log(VitalsArr[1].value);
	//alert("!!!!: "+VitalsArr[1].value);
    //        
    //document.getElementById('params_id').innerHTML=""+Cur_puls_val+"<hr>"+VitalsArr[0].value+"<br>"+VitalsArr[1].value+"<br>"+VitalsArr[2].value+"<br>"+VitalsArr[3].value+"<br>"+VitalsArr[4].value+"<br>"+VitalsArr[5].value+"<br>"+VitalsArr[6].value+"<br>"+VitalsArr[7].value+"<br>"+VitalsArr[8].value+"<hr>"+BasicContextsActived[9];


// 3.  DOM        
const items = document.querySelectorAll('.vital-item');
for (let i = 0; i < VitalsArr.length; i++) {
	var vVal=roundToHundredths(VitalsArr[i].value);
  const indicator = items[i].querySelector('.vital-indicator'); // !  vital-indicator!
  if (indicator) {
	  indicator.innerHTML=vVal;
    //    [0, 100]  [0, 1]
const threat = Math.max(0, Math.min(100, vVal)) / 100;
const color = interpolateColor(threat, [144, 238, 144], [255, 182, 193]);
indicator.style.backgroundColor = color;
  }
}

//   
for( var i = 0; i < BasicContextsActived.length; i++) 
{
setActiveContextItem(i,false);
	if(BasicContextsActived[i])
		setActiveContextItem(i,true);
}
}
////////////////////////////////////////////////////
function interpolateColor(t, rgbStart, rgbEnd) {
  const r = Math.round(rgbStart[0] + t * (rgbEnd[0] - rgbStart[0]));
  const g = Math.round(rgbStart[1] + t * (rgbEnd[1] - rgbStart[1]));
  const b = Math.round(rgbStart[2] + t * (rgbEnd[2] - rgbStart[2]));
  return `rgb(${r}, ${g}, ${b})`;
}

// - 
function performRegulationOperations() {
//   
for( var i = 0; i < VitalsArr.length; i++) 
{
VitalsArr[i].value = Math.min(100, Math.max(0, VitalsArr[i].value + VitalsArr[i].shift));
}

//       :
for( var i = 0; i < InputActions.length; i++) 
{
	if(InputActions[i].on && InputActions[i].shift !=0)
		shift_parametr(i);
}
}
//  
function shift_parametr(n)
{
//	alert(n+" | "+InputActions[n].shift);
const vID=InputActions[n].vital;
VitalsArr[vID].value = Math.min(100, Math.max(0, VitalsArr[vID].value + InputActions[n].shift));
}

//   
function roundToHundredths(num) {
  return Math.round(num * 10) / 10;
}
////////////////////////////////////////////////////////





/////////////////     ////////////////////////////
function setActiveContextItem(i,on)
{
if(on)
	{
document.getElementById('basic-context_'+i).style.backgroundColor="#FFD7FF";
document.getElementById('basic-context_'+i).style.boxShadow="0 0 8px 2px #FFD7FF";
	}
	else
	{
document.getElementById('basic-context_'+i).style.backgroundColor="#f9f9f9";
document.getElementById('basic-context_'+i).style.boxShadow="0 0 0 0 #ccc";
	}
}
//setActiveContextItem(3,true);

/*   
return   
*/
function getActiveBasicContexts() {  
    //console.log("  ...");
//   ,   (,  )
for( var i = 0; i < BasicContextsActived.length; i++) 
{
if (BasicContexts[i].autonomic) //    
    continue;
 BasicContextsActived[i] = false
}


//  (Id=8)
  if (VitalsArr[8].value > 30) {
	  watchdog_waking_up();
    BasicContextsActived[8] = true  //    
   return //   
  }  

  //  (Id=0)
  if (VitalsArr[0].value > 50) {
	  watchdog_waking_up();
    BasicContextsActived[0] = true  // 	
    return //     
  } 

  //  (Id=1)
  if (VitalsArr[1].value > 40) {
	  watchdog_waking_up();
    BasicContextsActived[1] = true  // 
    return //    
  }

  //  (Id=2)
  if (VitalsArr[2].value >= 60) {
	  watchdog_waking_up();
    BasicContextsActived[2] = true  // 
   return //   
  }

  //  (Id=3)
  if (VitalsArr[3].value >= 60) {
	  watchdog_waking_up();
    BasicContextsActived[3] = true  // 
   return //   
  }

//          ,     (  , )    . :        ,     ,         .
if (BasicContextsActived[10]) { //     
    //   
    return //   
}  

  //  (Id=4)
  if (VitalsArr[4].value >= 60) {
    BasicContextsActived[4] = true  // 
   return //   
  }

  //  (Id=5)
  if (VitalsArr[5].value >= 60) {
    BasicContextsActived[5] = true  // 
   return //   
  }

  //  (Id=6)
  if (VitalsArr[6].value >= 60) {
    BasicContextsActived[6] = true  //  
   return //   
  }

  //  (Id=7)
  if (VitalsArr[7].value > 50) {
    BasicContextsActived[7] = true  //  
   return //   
  }  

  if(IsSpeeping)
	{
BasicContextsActived[10] = true  // 
   return //   
	}

  BasicContextsActived[9] = true; //  ,     

}
///////////////////////////////////////////

/*      
 ID     
       (  )
*/
function get_cur_context()
{
const activeIds = [];
  for (let i = 0; i < BasicContextsActived.length; i++) {
    if (BasicContextsActived[i]) {
      //activeIds.push(i); // i   ID  (  )
	  activeIds.push(BasicContexts[i].id);
    }
  }
  //  (  ,     )
  activeIds.sort((a, b) => a - b);
  //    "0,3,9"    
  return activeIds.length > 0 ? activeIds.join(',') : 'none';
}
//////////////////////////////



// 
function watchdog_waking_up()
{ 	
//alert(IsSpeeping);
	if(!IsSpeeping)
		return;
//    
BasicContextsActived[10] = false;
IsSpeeping=false;
setTextInfo("C ","");
//    
const id=get_sleep_id();
var btn=document.getElementById('input_action-item_'+id);
InputActions[id].on=false;
btn.style.backgroundColor="#eeeeee";
btn.innerHTML=InputActions[id].name;
}
///////////////////////////////////////////


function fatalling()
{
const overlay = document.getElementById('fatalbox');
  if (!overlay) {
    console.error(' #overlay  !');
    return;
  }

  // : ,   
  overlay.textContent = '';
  overlay.style.opacity = '0';
  overlay.style.display = 'flex';

  //     
  function pulse() {
    return new Promise(resolve => {
      overlay.style.opacity = '0.8';
      setTimeout(() => {
        overlay.style.opacity = '0';
        setTimeout(resolve, 300); //   
      }, 600);
    });
  }

  //   
  pulse()
    .then(() => pulse())
    .then(() => pulse())
    .then(() => {
      //        
      overlay.style.opacity = '1';
      overlay.textContent = ' ';
    });
}
//////////////////////////////////////



#############################################
 /insect.htm
#############################################
<!DOCTYPE html>
<HTML>
 <HEAD>
  <TITLE></TITLE>
<meta http-equiv="Cache-Control" content="no-cache" />
<meta http-equiv="pragma" content="no-cache">
<meta name="ctsearch" content="noindex,follow">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
 </HEAD>

<BODY style="font-family:arial;font-size:12pt;">
<h1 style="margin:0;font-family:arial;font-size:12pt;"></h1>

<div id="pulsSum_id" style="position:absolute; top:0;right:10px;padding:4px;border-radius: 7px;background-color:#ffffff;"></div>

<div id="params_id"></div>

<style>/*     */
.vitals-panel {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  padding: 10px;
  #background: #f9f9f9;
  border-radius: 8px;
  #max-width: 800px;
  width: 98%;
}

.vital-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 9%;
}

.vital-indicator {
display: flex;
  justify-content: center;
  width: 90%;
  height: 32px;
  border-radius: 5px;
  margin-bottom: 4px;
  border: 2px solid #ccc;
  transition: background-color 0.3s, transform 0.2s;
  font-weight: bold;
  align-items: center;
}

.vital-indicator.active {
  transform: scale(1.1);
  box-shadow: 0 0 8px rgba(0,0,0,0.3);
}

.vital-label {
  font-size: 11px;
  text-align: center;
  color: #555;
}

.vital-value {
  font-size: 10px;
  color: #888;
}
</style>
<div id="vital_info_id" class="vitals-panel"></div>

<style>/*     */
.context-panel {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  padding: 10px;
  #background: #f9f9f9;
  border-radius: 8px;
  #max-width: 800px;
  width: 98%;
}

.context-item {
display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 7%;
  height: 32px;
  border-radius: 5px;
  #margin-bottom: 4px;
  background: #f9f9f9;
  border: 1px solid #ccc;
  transition: background-color 0.3s, transform 0.2s;
  font-weight: normal;
  font-size:13px;
}
</style>
<div id="contexts_info_id" class="context-panel"></div>

<style>/*    */
.input_actions {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  padding: 10px;
 # background: #eeeeee; 
  border-radius: 8px;
  #max-width: 800px;
  width: 86%;
  margin-bottom: 10px;
}

.input_action-item {
display: inline-flex;  /*    */
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border-radius: 5px;
  padding: 4px;
  background: #eeeeee;
  border: 1px solid #ccc;
  transition: background-color 0.3s, transform 0.2s;
  font-weight: normal;
  font-size:13px;
  white-space: nowrap;
  cursor:pointer;
}
</style>
<div id="input_actions_id" class="input_actions"></div>


<style>/*     */
.output_actions {
  padding: 10px;
  background: #eeeeee;
  border-radius: 8px;
  width: 86%;
  height: 100px;
  box-shadow: 8px 8px 8px 0px rgba(122,122,122,0.3);
}
/*  "",     */
.output_actions:empty::before {
  content: " ";
  color: #999999;
}
</style>
<div id="output_actions_id" class="output_actions"></div>



<script Language="JavaScript" src="date/vitals.js"></script>
<script Language="JavaScript" src="date/BasicContexts.js"></script>

<script Language="JavaScript" src="sys/input_actions.js"></script>
<script Language="JavaScript" src="sys/output_actions.js"></script> 
<script Language="JavaScript" src="sys/output_from_context.js"></script>

<script Language="JavaScript" src="sys/insect.js"></script>





 </BODY>
</HTML>


#############################################
index.htm
#############################################
<!DOCTYPE html>
<HTML>
 <HEAD>
  <TITLE>   </TITLE>
<meta http-equiv="Cache-Control" content="no-cache" />
<meta http-equiv="pragma" content="no-cache">
<meta name="ctsearch" content="noindex,follow">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
 </HEAD>

 <BODY style="font-family:arial;font-size:12pt;">

 <h1>   </h1>
<p>      ,     (,    )</p> 
<p> ,      ,     ,   .</p> 
<p> -          ,   <a href="https://scorcher.ru/conscience/thinking_cycles_t_channel/topic.php?tid=194" target="_blank">fornit.ru/71586</a>. 
<p>     ,         ,      .     ,     ,       .</p>
<p>&nbsp;</p>
<p> -            .</p>
<p> ,                ,   ,        .</p>
<p>     <a href="https://p-mvap.ru/" target="_blank"> </a> (   ).</p>
<p>&nbsp;</p>
<center>
<a href="insect.htm"><div style="font-size:16pt;background-color:#eeeeee;border-radius: 7px;width:300px;padding:10px;">  </div></a>

  
 </BODY>
</HTML>