 ###
 
 # target = id of Html element where counting occurs
-# startVal = the value you want to start at
-# endVal = the value you want to arrive at
-# decimals = number of decimal places in number, default 0
-# duration = duration in seconds, default 2
+# @startVal = the value you want to start at
+# @endVal = the value you want to arrive at
+# decimals = number of decimal places, default 0
+# duration = duration of animation in seconds, default 2
+# options = optional object of options (see below)
 
-countUp = (target, startVal, endVal, decimals, duration) ->
+countUp = (target, startVal, endVal, decimals, duration, options) ->
+
+  # default options
+  @options = option || { 
+    useEasing: true # toggle easing
+    useGrouping: true # 1,000,000 vs 1000000
+    separator: ',' # character to use as a separator
+    decimal: '.' # character to use as a decimal
+  }
 
   lastTime = 0
   vendors = [

  @reset = () ->
    @startTime = null
    cancelAnimationFrame @rAF
    @doc.innerHTML = @addCommas @startVal.toFixed(decimals)

  @resume = () ->
    @startTime = null

    @startVal = @framVal
    requestAnimationFrame @count

  @formatNumber = (nStr) ->
    nStr += ''
    x = nStr.split('.')
    x1 = x[0]
    x2 = if x.length > 1 then @options.decimal + x[1] else ''
    rgx = /(\d+)(\d{3})/

    if @options.useGrouping
      while rgx.test(x1)
        x1 = x1.replace(rgx, '$1' + @options.separator + '$2')

    x1 + x2

  # format @startVal on initialization
  @doc.innerHTML = @formatNumber @startVal.toFixed(decimals)
