-----------------------------
---  ADJUSTABLE DEFAULTS  ---
-----------------------------

var_target='07_23_2021'
                 -- Count down to target date
                 -- Syntax: month_date_year
                 --         xx_yy_zzzz
var_zoom=true    -- With / without bezel
var_chimes=true  -- Hourly alarm
var_lang=1       -- Language is English
                 -- (2=Dutch, 3=German
                 --  4=French, 5=Russian)


-----------------------------
---        VARIABLES      ---
-----------------------------

zoom1=1
zoom2=463/516
if var_zoom then tweens.z=zoom2 else tweens.z=zoom1 end
zoom=zoom1

m1=1
m2=675/512
tweens.m=m2
var_menu=false

var_agenda=false
tweens.agenda=-0.1
tweens.wh={dw}*339/50

var_sensor=false
tweens.sensor=-0.1
tweens.dh={ddw0}*360/7
tweens.h=1             -- animating heart beat
tweens.lock=0
tweens.tap=0
var_count=-1

var_zzz=true
var_ting=false
var_night=false

var_day=0

f=0.39

var_langs=5


-----------------------------
---        LANGUAGES      ---
-----------------------------

function var_language(i)
if i==1 then
   var_days={'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY'}
   var_month={'JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER'}
   var_word={'Phone', 'Watch', 'English', 'ON', 'OFF','(No calendar entry)','Night mode','Hands'}
end

if i==2 then
   var_days={'MAANDAG', 'DINSDAG', 'WOENSDAG', 'DONDERDAG', 'VRIJDAG', 'ZATERDAG', 'ZONDAG'}
   var_month={'JANUARI', 'FEBRUARI', 'MAART', 'APRIL', 'MEI', 'JUNI', 'JULI', 'AUGUSTUS', 'SEPTEMBER', 'OKTOBER', 'NOVEMBER', 'DECEMBER'}
   var_word={'Telefoon', 'Horloge', 'Nederlands', 'AAN', 'UIT','(Lege agenda)','Nachtmodus','Wijzers'}
end

if i==3 then
   var_days={'LUNDI', 'MARDI', 'MERCREDI', 'JEUDI', 'VENDREDI', 'SAMEDI', 'DIMANCHE'}
   var_month={'JANVIER', 'FÉVRIER', 'MARS', 'AVRIL', 'MAI', 'JUINE', 'JUILLET', 'AOÛT', 'SEPTEMBRE', 'OCTOBRE', 'NOVEMBRE', 'DECEMBRE'}
   var_word={'Telephone', 'Montre', 'Français', 'ACT.', 'DES.','(Rien à montrer)', 'Mode nocturne', 'Pointeurs'}
end

if i==4 then
   var_days={'MONTAG', 'DIENSTAG', 'MITTWOCH', 'DONNERSTAG', 'FREITAG', 'SAMSTAG', 'SONNTAG'}
   var_month={'JANUAR', 'FEBRUAR', 'MÄRZ', 'APRIL', 'MAI', 'JUNI', 'JULI', 'AUGUST', 'SEPTEMBER', 'OKTOBER', 'NOVEMBER', 'DEZEMBER'}
   var_word={'Telefon', 'Uhr', 'Deutsch', 'AN', 'AUS','(Keine Verabredungen)', 'Nachtmodus', 'Zeiger'}
end

if i==5 then
   var_days={'ПОНЕДЕЛЬ.', 'ВТОРНИК', 'СРЕДА', 'ЧЕТВЕРГ', 'ПЯТНИЦА', 'СУББОТА', 'ВОСКРЕС.'}
   var_month={'ЯНВАРЬ', 'ФЕВРАЛЬ', 'МАРТ', 'АПРЕЛЬ', 'МАЙ', 'ИЮНЬ', 'ИЮЛЬ', 'АВГУСТ', 'СЕНТЯБРЬ', 'ОКТЯБРЬ', 'НОЯБРЬ', 'ДЕКАБРЬ'}
   var_word={'Телефон', 'Часы', 'Русский', 'ВКЛ', 'ВЫКЛ','(Совсем ничего)', 'Ночная мода', 'Указатель'}

end

end

var_language(var_lang)



-----------------------------
---        FUNCTIONS      ---
-----------------------------

function on_second()
var_tt=wm_tag("{cd_" .. var_target .. "_df}")
if {drm}>59 and {drm}<241 then
   var_cm1=math.floor((360-{drm})*f)
   var_cm2=math.floor(({drm}+60)*f)
 else
   if {drm}>240 and {drm}<360 then
      var_cm1=math.floor(({drm}-120)*f)
      var_cm2=math.floor((540-{drm})*f)
    else
      var_cm1=math.floor(({drm}+240)*f)
      var_cm2=math.floor((180-{drm})*f)
   end
end
var_colm1=string.format('%.2x%2x%2x',var_cm1,var_cm1,var_cm1)
var_colm2=string.format('%.2x%2x%2x',var_cm2,var_cm2,var_cm2)

if {drh}>59 and {drh}<241 then
   var_ch1=math.floor((360-{drh})*f)
   var_ch2=math.floor(({drh}+60)*f)
 else
   if {drh}>240 and {drh}<360 then
      var_ch1=math.floor(({drh}-120)*f)
      var_ch2=math.floor((540-{drh})*f)
    else
      var_ch1=math.floor(({drh}+240)*f)
      var_ch2=math.floor((180-{drh})*f)
   end
end
var_colh1=string.format('%.2x%2x%2x',var_ch1,var_ch1,var_ch1)
var_colh2=string.format('%.2x%2x%2x',var_ch2,var_ch2,var_ch2)

tweens.wh={dw}*339/50
if var_count>-1 then var_count=var_count+1 end
if not var_sensor and var_count>1 then
   var_tween('dh',(tweens.dh)%360,{ddw0}*360/7)
   var_count=-1
end

if var_chimes then
   if {dm}==0 and {ds}<1 then 
      if var_zzz then wm_vibrate(200,{dh}) end
      if var_ting then wm_sfx('hourbeep135') end
   end
end

if var_sensor then
   var_tween('h',1.2,.8,{shr}/60)
end
end


function var_temph(d)
if d==0 then
   return({wtd})
 else
   t=wm_tag("{wf" .. var_day .."dth}")
   t=t .. '°' .. {wm}
   return(t)
end
end


function var_templ(d)
if d==0 then
   return({whp})
 else
   t=wm_tag("{wf" .. var_day .."dtl}")
   t=t .. '°' .. {wm}
   return(t)
end
end


function on_display_not_bright()
var_agenda=false
tweens.agenda=-0.1
var_sensor=false
tweens.sensor=-0.1
var_day=0
var_menu=false
tweens.m=m2
tweens.z=zoom
end

function on_display_bright()
var_agenda=false
tweens.agenda=-0.1
var_sensor=false
tweens.sensor=-0.1
var_day=0
rotate('dh',{pbr},{ddw0}*360/7)
rotate('wh',{br},{dw}*339/50)
end

function rotate(hand,a,b)
--this function calculates the shortest way.
if math.abs(a-b)>180 then
  if a<b then b=b-360 else b=b+360 end
end
var_tween(hand,a,b)
end


function var_tap(Du)
wm_schedule{action = 'tween', tween = 'tap', from = 100, to = 0, duration = Du, easing = linear} 
end


function var_tween(Tw,Fr,To)
wm_schedule { 
   {action='tween', tween=Tw, from=Fr, to=To, duration=1, easing=linear}
}
end