###
Neft object
###
@_neft = {}

###
Window object
###
@window = @

###
Timers
###
setTimeout = clearTimeout = setImmediate = null
do ->
    shots = Object.create null

    ios.timerCallback = (id) ->
        if timer = shots[id]
            delete shots[id]
            timer()
        return

    setTimeout = (func, delay, arg1, arg2, arg3) ->
        if typeof func isnt 'function'
            throw new TypeError "'callback' argument must be a function"

        if typeof delay isnt 'number'
            delay = parseInt delay
        if isNaN(delay) or delay < 4
            delay = 4

        argc = arguments.length
        switch argc
            when 1, 2
                callFunc = func
            when 3
                callFunc = ->
                    func arg1
            when 4
                callFunc = ->
                    func arg1, arg2
            when 5
                callFunc = ->
                    func arg1, arg2, arg3
            else
                args = new Array argc - 2
                for i in [2...argc] by 1
                    args[i - 2] = arguments[i]
                callFunc = ->
                    func.apply null, args

        id = ios.timerShot delay
        shots[id] = callFunc
        id

    setImmediate = (func, arg1, arg2, arg3) ->
        if typeof func isnt 'function'
            throw new TypeError "'callback' argument must be a function"

        argc = arguments.length
        switch argc
            when 1
                callFunc = func
            when 2
                callFunc = ->
                    func arg1
            when 3
                callFunc = ->
                    func arg1, arg2
            when 4
                callFunc = ->
                    func arg1, arg2, arg3
            else
                args = new Array argc - 1
                for i in [1...argc] by 1
                    args[i - 1] = arguments[i]
                callFunc = ->
                    func.apply null, args

        ios.immediate callFunc

        -1

    clearTimeout = (id) ->
        delete shots[id]
        return

###
Animation frame
###
requestAnimationFrame = null
do ->
    queue = []

    ios.animationFrameCallback = ->
        for i in [0...queue.length]
            queue.shift()()
        return

    requestAnimationFrame = (func) ->
        if typeof func isnt 'function'
            throw new TypeError "'callback' argument must be a function"

        queue.push func
        return

###
Neft code
###
`{{&neftCode}}`

###
App code
###
`{{&appCode}}`
