 # TODO use requestAnimationFrame to update dom
 # TODO listen on data and use innerHTML to create all dom elems at once
 #       http://blog.stevenlevithan.com/archives/faster-than-innerhtml
+
+# requestAnimationFrame shim
+nextAnimationFrame = do ->
+    last = 0
+    request = window.requestAnimationFrame
+    for vendor in ["webkit", "moz", "o", "ms"]
+        break if (request ?= window["#{vendor}RequestAnimationFrame"])
+    return request ? (callback) ->
+        cur = new Date().getTime()
+        time = Math.max(0, 16 - cur + last)
+        setTimeout(callback, time)
+
 
 # delay or invoke job immediately
 delay = (job) ->


    tpl.on 'replace', (el, tag) ->
        delay.call el, ->
            el._jquery.replaceWith(tag._jquery ? tag)

    tpl.on 'remove', (el) ->
        el._jquery?.remove()


    tpl.on 'raw', (el, html) ->
        delay.call el, ->
            nextAnimationFrame ->
                el._jquery.html(html)

    tpl.on 'show', (el) ->
        delay.call el, ->
