(version 2)
(include "sci.sh")
(use "Main")
(use "Cycle")
(script 805)

(class EndBegLoop of CT
    (properties
        client 0
        caller 0
        cycleDir 1
        cycleCnt 0
        completed 0
        endCel 0
        maxLoops 0
        lastView 0
        curLoop 0
        curView 1
        origView 0
    )

    (method (init theClient theLastView param3)
        = client theClient
        = origView (send client:view)
        = curView (send client:view)
        = maxLoops NumLoops(client)
        --maxLoops
        (if (>= paramTotal 2)
            = lastView theLastView
        )(else
            = lastView (send client:view)
        )
        (super:init(theClient 100 1 rest param3))
    )


    (method (doit)
        (var endBegLoopNextCel)
        = endBegLoopNextCel (self:nextCel())
        (send client:cel(endBegLoopNextCel))
        (if (== cycleDir 1)
            (if (== (+ (send client:lastCel()) 1) endBegLoopNextCel)
                (if (<= ++curLoop maxLoops)
                    (send client:
                        setLoop(curLoop)
                        cel(0)
                    )
                )(else
                    (if (<= ++curView lastView)
                        = curLoop 0
                        (send client:
                            view(curView)
                            setLoop(0)
                            cel(0)
                        )
                        = maxLoops NumLoops(client)
                        --maxLoops
                    )(else
                        --curLoop
                        --curView
                        = cycleDir neg cycleDir
                        (send client:cel((send client:lastCel())))
                    )
                )
            )
        )(else
            (if (<= endBegLoopNextCel 0)
                (if (>= --curLoop 0)
                    (send client:setLoop(curLoop))
                    (send client:cel((send client:lastCel())))
                )(else
                    (if (>= --curView origView)
                        (send client:view(curView))
                        = maxLoops NumLoops(client)
                        --maxLoops
                        (send client:
                            setLoop(= curLoop maxLoops)
                            cel((send client:lastCel()))
                        )
                    )(else
                        (self:cycleDone())
                    )
                )
            )
        )
    )


    (method (cycleDone)
        (send global1:handsOn())
        (super:cycleDone())
    )

)
