# 777-200 systems
# Syd Adams
# Revision 03/2025 BARANGER Emmanuel
#
var SndOut      = props.globals.getNode("/sim/sound/Ovolume",1);
var chronometer = aircraft.timer.new("/instrumentation/clock/ET-sec",1);
var vmodel      = substr(getprop("sim/aero"), 3);

#EFIS specific class
# ie: var efis = EFIS.new("instrumentation/efis");
var EFIS = {
  new : func(prop1) {
    var m = { parents : [EFIS] };
    m.radio_list=["instrumentation/comm/frequencies","instrumentation/comm[1]/frequencies","instrumentation/nav/frequencies","instrumentation/nav[1]/frequencies"];
    m.mfd_mode_list=["APP","VOR","MAP","PLAN"];

    m.efis = props.globals.initNode(prop1);
    m.mfd = m.efis.initNode("mfd");
    m.pfd = m.efis.initNode("pfd");
    m.eicas = m.efis.initNode("eicas");
    m.mfd_mode_num = m.mfd.initNode("mode-num",2,"INT");
    m.mfd_display_mode = m.mfd.initNode("display-mode",m.mfd_mode_list[2]);
    m.std_mode = m.efis.initNode("inputs/setting-std",0,"BOOL");
    m.previous_set = m.efis.initNode("inhg-previos",29.92);
    m.kpa_mode = m.efis.initNode("inputs/kpa-mode",0,"BOOL");
    m.kpa_output = m.efis.initNode("inhg-kpa",29.92);
    m.kpa_prevoutput = m.efis.initNode("inhg-kpa-previous",29.92);
    m.temp = m.efis.initNode("fixed-temp",0);
    m.alt_meters = m.efis.initNode("inputs/alt-meters",0,"BOOL");
    m.fpv = m.efis.initNode("inputs/fpv",0,"BOOL");
    m.nd_centered = m.efis.initNode("inputs/nd-centered",0,"BOOL");
    m.mins_mode = m.efis.initNode("inputs/minimums-mode",0,"BOOL");
    m.mins_mode_txt = m.efis.initNode("minimums-mode-text","RADIO","STRING");
    m.minimums = m.efis.initNode("minimums",250,"INT");
    m.mk_minimums = props.globals.getNode("instrumentation/mk-viii/inputs/arinc429/decision-height");
    m.wxr = m.efis.initNode("inputs/wxr",0,"BOOL");
    m.range = m.efis.initNode("inputs/range",0);
    m.sta = m.efis.initNode("inputs/sta",0,"BOOL");
    m.wpt = m.efis.initNode("inputs/wpt",0,"BOOL");
    m.arpt = m.efis.initNode("inputs/arpt",0,"BOOL");
    m.data = m.efis.initNode("inputs/data",0,"BOOL");
    m.pos = m.efis.initNode("inputs/pos",0,"BOOL");
    m.terr = m.efis.initNode("inputs/terr",0,"BOOL");
    m.rh_vor_adf = m.efis.initNode("inputs/rh-vor-adf",0,"INT");
    m.lh_vor_adf = m.efis.initNode("inputs/lh-vor-adf",0,"INT");

    m.radio = m.efis.getNode("radio-mode",1);
    m.radio.setIntValue(0);
    m.radio_selected = m.efis.getNode("radio-selected",1);
    m.radio_selected.setDoubleValue(getprop("instrumentation/comm/frequencies/selected-mhz"));
    m.radio_standby = m.efis.getNode("radio-standby",1);
    m.radio_standby.setDoubleValue(getprop("instrumentation/comm/frequencies/standby-mhz"));

    m.kpaL = setlistener("instrumentation/altimeter/setting-inhg", func m.calc_kpa());

    m.eicas_msg_alert   = m.eicas.initNode("msg/alert"," ","STRING");
    m.eicas_msg_caution = m.eicas.initNode("msg/caution"," ","STRING");
    m.eicas_msg_info    = m.eicas.initNode("msg/info"," ","STRING");
    m.update_radar_font();
    m.update_nd_center();
    return m;
  },
  #### convert inhg to kpa ####
  calc_kpa : func {
    var kp = getprop("instrumentation/altimeter/setting-inhg");
    kp     = kp * 33.8637526;
    me.kpa_output.setValue(kp);
    kp     = getprop("instrumentation/efis/inhg-previos");
    kp     = kp * 33.8637526;
    me.kpa_prevoutput.setValue(kp);
  },
  #### update temperature display ####
  update_temp : func {
    var tmp = getprop("/environment/temperature-degc");
    if (tmp < 0.00) {
      tmp = -1 * tmp;
    }
    me.temp.setValue(tmp);
  },
  #### swap radio freq ####
  swap_freq : func() {
    var tmpsel = me.radio_selected.getValue();
    var tmpstb = me.radio_standby.getValue();
    me.radio_selected.setValue(tmpstb);
    me.radio_standby.setValue(tmpsel);
    me.update_frequencies();
  },
  #### copy efis freq to radios ####
  update_frequencies : func() {
    var fq = me.radio.getValue();
    setprop(me.radio_list[fq]~"/selected-mhz",me.radio_selected.getValue());
    setprop(me.radio_list[fq]~"/standby-mhz",me.radio_standby.getValue());
  },
  #### modify efis radio standby freq ####
  set_freq : func(fdr) {
    var rd       = me.radio.getValue();
    var frq      = me.radio_standby.getValue();
    var frq_step = 0;
    if (rd >=2) {
      if ( fdr ==   1 ) {
        frq_step = 0.05;
      }
      if ( fdr ==  -1 ) {
        frq_step = -0.05;
      }
      if ( fdr ==  10 ) {
        frq_step = 1.0;
      }
      if ( fdr == -10 ) {
        frq_step = -1.0;
      }
      frq += frq_step;
      if ( frq > 118.000 ) {
        frq -= 10.000;
      }
      if ( frq < 108.000 ) {
        frq += 10.000;
      }
    } else {
      if ( fdr ==   1 ) {
        frq_step = 0.025;
      }
      if ( fdr ==  -1 ) {
        frq_step = -0.025;
      }
      if ( fdr ==  10 ) {
        frq_step = 1.0;
      }
      if ( fdr == -10 ) {
        frq_step = -1.0;
      }
      frq += frq_step;
      if ( frq > 136.000 ) {
        frq -= 18.000;
      }
      if ( frq < 118.000 ) {
        frq += 18.000;
      }
    }
    me.radio_standby.setValue(frq);
    me.update_frequencies();
  },

  set_radio_mode : func(rm){
    me.radio.setIntValue(rm);
    me.radio_selected.setDoubleValue(getprop(me.radio_list[rm]~"/selected-mhz"));
    me.radio_standby.setDoubleValue(getprop(me.radio_list[rm]~"/standby-mhz"));
  },
  ######### Controller buttons ##########
  ctl_func : func(md,val){
    controls.click(3);
    if(md=="range") {
      var rng = me.range.getValue();
      if(val ==1) {
        rng =rng * 2;
        if(rng > 640) rng = 640;
      } elsif(val =-1) {
        rng =rng / 2;
        if(rng < 10) rng = 10;
      }
      me.range.setValue(rng);
    } elsif(md=="dh") {
      var num =me.minimums.getValue();
      if(val==0) {
        num=250;
      } else {
        num+=val;
        if(num<0)num=0;
        if(num>1000)num=1000;
      }
      me.minimums.setValue(num);
      me.mk_minimums.setValue(num);
    } elsif(md=="mins") {
      var mode = me.mins_mode.getValue();
      me.mins_mode.setValue(1-mode);
      if (mode)
        me.mins_mode_txt.setValue("RADIO");
      else
        me.mins_mode_txt.setValue("BARO");
    } elsif(md=="display") {
      var num =me.mfd_mode_num.getValue();
      num+=val;
      if(num<0)num=0;
      if(num>3)num=3;
      me.mfd_mode_num.setValue(num);
      me.mfd_display_mode.setValue(me.mfd_mode_list[num]);
    } elsif(md=="rhvor") {
      var num =me.rh_vor_adf.getValue();
      num+=val;
      if(num>1)num=1;
      if(num<-1)num=-1;
      me.rh_vor_adf.setValue(num);
    } elsif(md=="lhvor") {
      var num =me.lh_vor_adf.getValue();
      num+=val;
      if(num>1)num=1;
      if(num<-1)num=-1;
      me.lh_vor_adf.setValue(num);
    } elsif(md=="center") {
      var num =me.nd_centered.getValue();
      num = 1 - num;
      me.nd_centered.setValue(num);
      me.update_radar_font();
      me.update_nd_center();
    } else {
      print("Unsupported mode: ",md);
    }
  },
  update_radar_font : func {
    var fnt=[12,13];
    var linespacing = 0.01;
    var num = me.nd_centered.getValue();
    setprop("instrumentation/radar/font/size",fnt[num]);
    setprop("instrumentation/radar/font/line-spacing",linespacing);
  },
  update_nd_center : func {
    if (me.nd_centered.getValue() == 0) {
        setprop("instrumentation/nd/y-center", 0.5);
    } else {
        setprop("instrumentation/nd/y-center", 0.5);
    }
  },
  #### update EICAS messages ####
  update_eicas : func(alertmsgs,cautionmsgs,infomsgs) {
    var msg="";
    var spacer="";
    for(var i=0; i<size(alertmsgs); i+=1) {
      msg = msg ~ alertmsgs[i] ~ "\n";
      spacer = spacer ~ "\n";
    }
    me.eicas_msg_alert.setValue(msg);
    msg=spacer;
    for(var i=0; i<size(cautionmsgs); i+=1) {
      msg = msg ~ cautionmsgs[i] ~ "\n";
      spacer = spacer ~ "\n";
    }
    me.eicas_msg_caution.setValue(msg);
    msg=spacer;
    for(var i=0; i<size(infomsgs); i+=1) {
      msg = msg ~ infomsgs[i] ~ "\n";
    }
    me.eicas_msg_info.setValue(msg);
  },
  #### update ####
  update : func {
    if (me.fuel_out.getBoolValue() or me.cutoff.getBoolValue()) {
      me.running.setBoolValue(0);
      me.egt.setDoubleValue(me.egt_degf.getValue());
      me.generator.setBoolValue(0);
    }
    if (me.running.getBoolValue()) {
      if (me.starterSwitch.getValue() == -1) {
        settimer(func { me.starterSwitch.setValue(0);}, 0.3);
      }
      me.rpm.setValue(me.n1.getValue());
      me.n2rpm.setValue(me.n2.getValue());
      me.throttle_lever.setValue(me.throttle.getValue());
      me.egt.setDoubleValue(me.egt_degf.getValue());
      var v_pph = (me.fuel_gph.getValue() * getprop("consumables/fuel/tank/density-ppg") / 1000);
      if (v_pph < 1.2) {
        me.idle_ff();
        v_pph=1.2;
      } else {
        v_pph = v_pph + 1.2 / (1 + v_pph);
      }
      me.fuel_pph.setValue(v_pph);
      var v_egt = me.egt_degf.getValue() - 64;
      if (v_egt > 0) {
        v_egt = 270 - v_egt/4;
      } else {
        v_egt = 270;
      }
      me.egt.setDoubleValue(me.egt_degf.getValue() + v_egt);
    } else {
      me.throttle_lever.setValue(0);
      if (me.starterSwitch.getValue() == -1) {
        if(getprop("controls/electric/APU-generator") or getprop("engines/engine[0]/run") or getprop("engines/engine[1]/run")) {
          me.spool_up();
        } else {
          settimer(func { me.starterSwitch.setValue(0);}, 0.3);
        }
      } else {
        var tmprpm = me.rpm.getValue();
        tmprpm -= getprop("sim/time/delta-realtime-sec") * 1.2;
        if (tmprpm < 0.0) {
          tmprpm = 0;
        }
        me.rpm.setValue(tmprpm);
      }
    }
    var hpsi =me.rpm.getValue();
    if (hpsi>60 ) {
      hpsi = 60;
    }
    me.hpump.setValue(hpsi);

    if (vmodel == "-200LR") {
      setprop("consumables/fuel/tank[3]/selected",
        (getprop("consumables/fuel/tank[3]/level-gal_us") > 0)
        and (getprop("controls/fuel/tank[1]/boost-pump[0]") or getprop("controls/fuel/tank[1]/boost-pump[1]")));
      setprop("consumables/fuel/tank[4]/selected",
        (getprop("consumables/fuel/tank[4]/level-gal_us") > 0)
        and (getprop("controls/fuel/tank[1]/boost-pump[0]") or getprop("controls/fuel/tank[1]/boost-pump[1]")));
      setprop("consumables/fuel/tank[5]/selected",
        (getprop("consumables/fuel/tank[5]/level-gal_us") > 0)
        and (getprop("controls/fuel/tank[1]/boost-pump[0]") or getprop("controls/fuel/tank[1]/boost-pump[1]")));
      setprop("consumables/fuel/tank[1]/selected", ((getprop("consumables/fuel/tank[3]/level-gal_us") < 30)
        and (getprop("consumables/fuel/tank[4]/level-gal_us") < 30)
        and (getprop("consumables/fuel/tank[5]/level-gal_us") < 30)
        and (getprop("consumables/fuel/tank[1]/level-gal_us") > 0)
        and (getprop("controls/fuel/tank[1]/boost-pump[0]") or getprop("controls/fuel/tank[1]/boost-pump[1]"))));
       setprop("consumables/fuel/tank[0]/selected", ((getprop("consumables/fuel/tank[3]/level-gal_us") < 30)
        and (getprop("consumables/fuel/tank[4]/level-gal_us") < 30)
        and (getprop("consumables/fuel/tank[5]/level-gal_us") < 30)
        and (getprop("consumables/fuel/tank[1]/level-gal_us") < 50)
        and (getprop("controls/fuel/tank[0]/boost-pump[0]") or getprop("controls/fuel/tank[0]/boost-pump[1]"))));
      setprop("consumables/fuel/tank[2]/selected", ((getprop("consumables/fuel/tank[3]/level-gal_us") < 30)
        and (getprop("consumables/fuel/tank[4]/level-gal_us") < 30)
        and (getprop("consumables/fuel/tank[5]/level-gal_us") < 30)
        and (getprop("consumables/fuel/tank[1]/level-gal_us") < 50)
        and (getprop("controls/fuel/tank[2]/boost-pump[0]") or getprop("controls/fuel/tank[2]/boost-pump[1]"))));
    } else {
      setprop("consumables/fuel/tank[1]/selected", ((getprop("consumables/fuel/tank[1]/level-gal_us") > 0)
        and (getprop("controls/fuel/tank[1]/boost-pump[0]") or getprop("controls/fuel/tank[1]/boost-pump[1]"))));
      setprop("consumables/fuel/tank[0]/selected", ((getprop("consumables/fuel/tank[1]/level-gal_us") < 50)
        and (getprop("controls/fuel/tank[0]/boost-pump[0]") or getprop("controls/fuel/tank[0]/boost-pump[1]"))));
      setprop("consumables/fuel/tank[2]/selected", ((getprop("consumables/fuel/tank[1]/level-gal_us") < 50)
        and (getprop("controls/fuel/tank[2]/boost-pump[0]") or getprop("controls/fuel/tank[2]/boost-pump[1]"))));
    }
    if (me.apu_knob.getValue() == 0) {
      me.apu_status.setValue(0);             # OFF
      me.apu_running.setValue(0);
    } elsif(me.apu_knob.getValue() == 1) {
      if((me.apu_running.getBoolValue() == 0) and (me.apu_status.getValue() == 0)) {
        me.apu_status.setValue(1);           # ARM
      }
    } else {                                 # ARM
      if ((me.apu_status.getValue() == 1) and ((getprop("controls/electric/battery-switch") == 1) or (me.running.getBoolValue()))) {
        me.apu_status.setValue(2);           # START
        settimer(func { me.apu_status.setValue(3);}, 20);
      }
      settimer(func { me.apu_knob.setValue(1);}, 0.3);
    }
    if (me.apu_status.getValue() == 3) {
      me.apu_running.setBoolValue(1);
      if (getprop("controls/electric/APU-generator") != 1) {
        setprop("controls/electric/APU-generator", 1);
      }
    } else {
      me.apu_running.setBoolValue(0);
      if (getprop("controls/electric/APU-generator") != 0) {
        setprop("controls/electric/APU-generator", 0);
      }
    }
    if (me.apu_running.getBoolValue() and (getprop("consumables/fuel/tank[0]/level-lbs") > 0)) {
      setprop("/consumables/fuel/tank[0]/level-gal_us", getprop("/consumables/fuel/tank[0]/level-gal_us")-0.0006);
    }
    if (getprop("controls/lighting/cabin-lights") == 1
      and getprop("controls/electric/APU-generator") == 0
      and getprop("controls/electric/engine[0]/generator") == 0
      and getprop("controls/electric/engine[1]/generator") == 0
      and getprop("controls/electric/engine[2]/generator") == 0
      and getprop("controls/electric/battery-switch") == 0) {
      Shutdown();
    }
  },
  spool_up : func {
    var rpminc = 0;
    var tmprpm = me.rpm.getValue();
    var v_pph = 0;
    if (!me.fuel_out.getBoolValue() and !me.cutoff.getBoolValue()) {
      v_pph = 1.2;
      me.idle_ff();
      if(tmprpm <10) {
        rpminc = 0.5;
      } else {
        rpminc = 0.8;
      } if(tmprpm >= me.n1.getValue()) {
        controls.click(1);
        me.starterSwitch.setValue(0);
        me.running.setBoolValue(1);
        me.generator.setBoolValue(1);
        setprop("controls/lighting/cabin-lights",1);
        setprop("controls/lighting/strobe",1);
      } if(tmprpm > 0) {
        var v_egt = tmprpm * 270 / 18.5 + me.egt_degf.getValue();
        me.egt.setDoubleValue(v_egt);
      }
    } else {
      if(tmprpm <= 5) {
        rpminc = 0.1;
      }
    }
    tmprpm += getprop("sim/time/delta-realtime-sec") * rpminc;
    me.rpm.setValue(tmprpm);
    me.n2rpm.setValue(math.sqrt(tmprpm) * 12.65);
    me.fuel_pph.setValue(v_pph);
  },
  # This function could be removed if FG fuel flow program is fixed to consume fuel when engine idle.
  idle_ff : func {
    var v_consume = 0.001;
    if(me.eng_num == 0) {
      if(getprop("consumables/fuel/tank[0]/selected")) {
        setprop("consumables/fuel/tank[0]/level-gal_us", getprop("consumables/fuel/tank[0]/level-gal_us")- v_consume);
      } elsif(getprop("consumables/fuel/tank[1]/selected")) {
        setprop("consumables/fuel/tank[1]/level-gal_us", getprop("consumables/fuel/tank[1]/level-gal_us")- v_consume);
      }
    } else {
      if(getprop("consumables/fuel/tank[2]/selected")) {
        setprop("consumables/fuel/tank[2]/level-gal_us", getprop("consumables/fuel/tank[2]/level-gal_us")- v_consume);
      } elsif(getprop("consumables/fuel/tank[1]/selected")) {
        setprop("consumables/fuel/tank[1]/level-gal_us", getprop("consumables/fuel/tank[1]/level-gal_us")- v_consume);
      }
    }
  },
};

##########################
var Wiper = {
  new : func {
    var m = { parents : [Wiper] };
    m.direction   = 0;
    m.delay_count = 0;
    m.spd_factor  = 0;
    m.node        = props.globals.getNode(arg[0],1);
    m.power       = props.globals.getNode(arg[1],1);
    if (m.power.getValue() == nil) {
      m.power.setDoubleValue(0);
    }
    m.spd = m.node.getNode("arc-sec",1);
    if (m.spd.getValue() == nil) {
      m.spd.setDoubleValue(1);
    }
    m.delay = m.node.getNode("delay-sec",1);
    if (m.delay.getValue() == nil) {
      m.delay.setDoubleValue(0);
    }
    m.position = m.node.getNode("position-norm", 1);
    m.position.setDoubleValue(0);
    m.switch = m.node.getNode("switch", 1);
    if (m.switch.getValue() == nil) {
      m.switch.setBoolValue(0);
    }
    return m;
  },
  active: func {
    if (me.power.getValue()<=5) {
      return;
    }
    var spd_factor = 1/me.spd.getValue();
    var pos = me.position.getValue();
    if (!me.switch.getValue()) {
      if (pos <= 0.000) {
        return;
      }
    }
    if(pos >=1.000) {
      me.direction=-1;
    } elsif(pos <=0.000) {
      me.direction=0;
      me.delay_count+=getprop("/sim/time/delta-sec");
      if (me.delay_count >= me.delay.getValue()) {
        me.delay_count=0;
        me.direction=1;
      }
    }
    var wiper_time = spd_factor*getprop("/sim/time/delta-sec");
    pos +=(wiper_time * me.direction);
    me.position.setValue(pos);
  }
};
#####################

var Efis  = EFIS.new("instrumentation/efis");
var wiper = Wiper.new("controls/electric/wipers","systems/electrical/bus-volts");

setlistener("/sim/signals/fdm-initialized", func {
  SndOut.setDoubleValue(0.15);
  chronometer.stop();
  props.globals.initNode("/instrumentation/clock/ET-display",0,"INT");
  props.globals.initNode("/instrumentation/clock/time-display",0,"INT");
  props.globals.initNode("/instrumentation/clock/time-knob",0,"INT");
  props.globals.initNode("/instrumentation/clock/et-knob",0,"INT");
  props.globals.initNode("/instrumentation/clock/set-knob",0,"INT");
  # setprop("/instrumentation/groundradar/id",getprop("sim/tower/airport-id"));
  setprop("/sim/flaps/current", 0);
  var capwing = getprop("consumables/fuel/tank[0]/capacity-gal_us");
  # make the fuel quantity balancing
  var total_fuel = 0;
  var capcenter = 0;
  var j = 3;
  if (vmodel == "-200LR") {
    j = 6;
    capcenter = getprop("consumables/fuel/tank[1]/capacity-gal_us");
  }
  for(var i = 0; i < j; i += 1) {
    if (getprop("consumables/fuel/tank["~i~"]/level-gal_us") != nil) {
      total_fuel +=getprop("consumables/fuel/tank["~i~"]/level-gal_us");
    }
  }
  if (j == 6) {
    if (total_fuel > ((capwing * 2) + capcenter)) {
      var capaux = ((total_fuel -  ((capwing * 2) + capcenter)) / 3);
      setprop("consumables/fuel/tank[3]/level-gal_us", capaux);
      setprop("consumables/fuel/tank[4]/level-gal_us", capaux);
      setprop("consumables/fuel/tank[5]/level-gal_us", capaux);
      total_fuel -= (capaux * 3);
    } else {
      setprop("consumables/fuel/tank[3]/level-gal_us", 0);
      setprop("consumables/fuel/tank[4]/level-gal_us", 0);
      setprop("consumables/fuel/tank[5]/level-gal_us", 0);
    }
  }
  if (total_fuel > (capwing * 2)) {
    setprop("consumables/fuel/tank[0]/level-gal_us", capwing);
    setprop("consumables/fuel/tank[1]/level-gal_us", (total_fuel - (capwing * 2)));
    setprop("consumables/fuel/tank[2]/level-gal_us", capwing);
  } else {
    setprop("consumables/fuel/tank[0]/level-gal_us", (total_fuel / 2));
    setprop("consumables/fuel/tank[1]/level-gal_us", 0);
    setprop("consumables/fuel/tank[2]/level-gal_us", (total_fuel / 2));
  }
  # setprop("/controls/gear/tiller-enabled", 0);
  #settimer(start_updates,1);
});

setlistener("/sim/signals/reinit", func {
  SndOut.setDoubleValue(0.15);
  Shutdown();
});

setlistener("/sim/current-view/internal", func(vw) {
  if (vw.getValue()) {
    SndOut.setDoubleValue(0.3);
  } else {
    SndOut.setDoubleValue(1.0);
  }
},1,0);

setlistener("/instrumentation/clock/et-knob", func(et) {
  var tmp = et.getValue();
  if (tmp == -1) {
    chronometer.reset();
  } elsif(tmp==0) {
    chronometer.stop();
  } elsif(tmp==1) {
    chronometer.start();
  }
},0,0);

setlistener("instrumentation/transponder/mode-switch", func(transponder_switch) {
  var mode = transponder_switch.getValue();
  var tcas_mode = 1;
  if (mode == 3) {
    tcas_mode = 2;
  }
  if (mode == 4) {
    tcas_mode = 3;
  }
  setprop("instrumentation/tcas/inputs/mode",tcas_mode);
},0,0);

setlistener("instrumentation/tcas/outputs/traffic-alert", func(traffic_alert) {
    var alert = traffic_alert.getValue();
    # any TCAS alert enables the traffic display
    if (alert) setprop("instrumentation/efis/inputs/tfc",1);
},0,0);

setlistener("controls/flight/speedbrake", func(spd_brake) {
  var brake = spd_brake.getValue();
  # do not update lever when in AUTO position
  if ((brake==0) and (getprop("controls/flight/speedbrake-lever")==2)) {
    setprop("controls/flight/speedbrake-lever",0);
  } elsif ((brake==1) and ((getprop("controls/flight/speedbrake-lever")==0) or (getprop("controls/flight/speedbrake-lever")==1))) {
    setprop("controls/flight/speedbrake-lever",2);
  }
},0,0);

setlistener("controls/flight/speedbrake-lever", func(spd_lever) {
  var lever = spd_lever.getValue();
  controls.click(7);
  # do not set speedbrake property unless changed (avoid revursive updates)
  if ((lever==0)and(getprop("controls/flight/speedbrake")!=0)) {
    setprop("controls/flight/speedbrake",0);
  } elsif ((lever==2)and(getprop("controls/flight/speedbrake")!=1)) {
    setprop("controls/flight/speedbrake",1);
  }
},0,0);

controls.toggleAutoSpoilers = func() {
  # 0=spoilers retracted, 1=auto, 2=extended
  if (getprop("controls/flight/speedbrake-lever")!=1) {
    setprop("controls/flight/speedbrake-lever",1);
  } else {
    setprop("controls/flight/speedbrake-lever",2*getprop("controls/flight/speedbrake"));
  }
}

setlistener("controls/flight/flaps", func { controls.click(6) } );
setlistener("/controls/gear/gear-down", func { controls.click(8) } );
controls.gearDown = func(v) {
  if (v < 0) {
    if (!getprop("gear/gear[1]/wow")) {
      setprop("/controls/gear/gear-down", 0);
    }
  } elsif (v > 0) {
    setprop("/controls/gear/gear-down", 1);
  }
}

var click_reset = func(propName) {
  setprop(propName,0);
}

controls.click = func(button) {
  if (getprop("sim/freeze/replay-state")) {
    return;
  }
  var propName="sim/sound/click"~button;
  setprop(propName,1);
  settimer(func { click_reset(propName) },0.4);
}

var update_systems = func {
  Efis.calc_kpa();
  Efis.update_temp();
  # wiper.active(); # not implemented yet!
  if (getprop("controls/gear/gear-down")){
    setprop("sim/multiplay/generic/float[16]",getprop("gear/gear[0]/compression-m"));
    setprop("sim/multiplay/generic/float[17]",getprop("gear/gear[1]/compression-m"));
    setprop("sim/multiplay/generic/float[18]",getprop("gear/gear[2]/compression-m"));
  }

  var et_tmp = getprop("/instrumentation/clock/ET-sec");
  var et_min = int(et_tmp * 0.0166666666667);
  var et_hr  = int(et_min * 0.0166666666667) * 100;
  et_tmp     = et_hr+et_min;
  setprop("instrumentation/clock/ET-display",et_tmp);

  settimer(update_systems,0);
}
