%D \module
%D   [      file=pdfclock,
%D        version=2001-09-27,
%D          title=\CONTEXT\ Example File,
%D       subtitle=Clock,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\setupoutput
  [pdftex]

\definepapersize
  [clock]
  [width=2.5cm,
   height=2.5cm]

\setuppapersize
  [clock][clock]

\setuplayout
  [header=0pt,
   footer=0pt,
   backspace=.25cm,
   topspace=.25cm,
   width=middle,
   height=middle]

\setupinteraction % closeaction fails here
  [state=start,
   closeaction=JS(stop_clock),
   closepageaction=JS(stop_clock)]

\setupcolors
  [state=start]

\startJSpreamble stepper used now
  var state = 0 ;
  var more = 0 ;
  var step = 0 ;
  function step_clock ()
    { try
        { if (more==60)
            { more = 0 ; Walk_Field("more") }
          more += 1 ;
          Walk_Field("clock") ;
          this.dirty = false }
      catch (e)
        { } }
  function start_clock ()
    { try
        { if (state==0)
            { step = app.setInterval ("step_clock()", 1000) ;
              step.count = 0 ;
              state = 1 }
          else if (state==1)
            { app.clearInterval (step) ;
              state = 2 }
          else if (state==2)
            { app.clearInterval (step) ;
              Reset_Fields("more") ;
              Reset_Fields("clock") ;
              Set_Field("more", "1") ;
              Set_Field("clock", "1") ;
              more = 0 ;
              state = 0 } }
      catch (e)
        { } }
  function stop_clock ()
    { try
        { app.clearInterval (step) }
      catch (e)
        { } }
\stopJSpreamble

\startreusableMPgraphic{common}
  drawoptions(withpen pencircle scaled 1mm withcolor .4white) ;
  fill fullsquare scaled 2.5cm ;
  drawoptions(withpen pencircle scaled 1mm withcolor .7green) ;
  draw fullsquare scaled 2.5cm ;
  drawoptions(withpen pencircle scaled 1mm withcolor .7red) ;
  draw fullcircle scaled 2cm ;
  drawoptions(withpen pencircle scaled 2mm withcolor .7yellow) ;
  for i=1 upto 12 : draw (0,1cm) rotated ((i-1)*(360/12)) ; endfor ;
\stopreusableMPgraphic

\startuseMPgraphic{clock}
  numeric stp ; stp := (\MPvar{n}-1)*(360/60) ;
  pair p ; p := (0,\MPvar{l}-.5mm) ;
  drawoptions(withpen pencircle scaled 1mm withcolor .7\MPvar{c}) ;
  draw (origin -- p) rotated -stp ;
  draw (p shifted (-2mm,-2.5mm)--p--p shifted (2mm,-2.5mm)) rotated -stp ;
  setbounds currentpicture to fullsquare scaled 2cm ;
  drawoptions(withpen pencircle scaled 2mm withcolor .7white) ;
  draw origin ;
\stopuseMPgraphic

\defineoverlay [common] [\reuseMPgraphic{common}]
\defineoverlay [start]  [\overlaybutton{JS(start_clock)}]

\setupbackgrounds
  [page]
  [background={common,start}]

\let\clocklist\empty
\let\morelist \empty

\dorecurse{60}
  {\appendtocommalist{step:\recurselevel}\clocklist
   \expanded
     {\definesymbol[step:\recurselevel]
        [\noexpand\useMPgraphic
           {clock}{n=\recurselevel,l=1cm,c=blue}]}
   \appendtocommalist{more:\recurselevel}\morelist
   \expanded
     {\definesymbol[more:\recurselevel]
        [\noexpand\useMPgraphic
           {clock}{n=\recurselevel,l=.75cm,c=green}]}}

\expanded
  {\definefieldstack [clock] [\clocklist]
     [width=2cm,height=2cm,offset=overlay,frame=off]}

\expanded
  {\definefieldstack [more] [\morelist]
     [width=2cm,height=2cm,offset=overlay,frame=off]}

\starttext

\startoverlay
  {\fieldstack[more]}
  {\fieldstack[clock]}
\stopoverlay

\stoptext
