 numberRegExp = /^-?[£$¤]?[\d,.]+%?$/
 trimRegExp = /^\s+|\s+$/g
 
+clickEvents = ['click']
 touchDevice = 'ontouchstart' of document.documentElement
-clickEvent = if touchDevice then 'touchstart' else 'click'
+clickEvents.push 'touchstart' if touchDevice
 
 addEventListener = (el, event, handler) ->
   if el.addEventListener?

      tBody = table.tBodies[0]
      rowArray = []

      for row in tBody.rows
        rowArray.push [sortable.getNodeValue(row.cells[i]), row]

      if sorted
        rowArray.reverse()
      else
        rowArray.sort type.compare

      for rowArrayObject in rowArray
        tBody.appendChild rowArrayObject[1]

  getColumnType: (table, i) ->
    for row in table.tBodies[0].rows
      text = sortable.getNodeValue row.cells[i]
      if text isnt ''
        if text.match(numberRegExp)
          return sortable.types.numeric
        if not isNaN Date.parse(text)
          return sortable.types.date

    return sortable.types.alpha

  getNodeValue: (node) ->
    return '' unless node
    return node.getAttribute('data-value') if node.getAttribute('data-value') isnt null
    return node.innerText.replace(trimRegExp, '') unless typeof node.innerText is 'undefined'
    node.textContent.replace trimRegExp, ''

  types:
    numeric:
      defaultSortDirection: 'descending'
      compare: (a, b) ->
        aa = parseFloat(a[0].replace(/[^0-9.-]/g, ''), 10)
        bb = parseFloat(b[0].replace(/[^0-9.-]/g, ''), 10)
        aa = 0 if isNaN(aa)
        bb = 0 if isNaN(bb)
        bb - aa

    alpha:
      defaultSortDirection: 'ascending'
      compare: (a, b) ->
        a[0].localeCompare b[0]

    date:
      defaultSortDirection: 'ascending'
      compare: (a, b) ->
        aa = Date.parse(a[0])
        bb = Date.parse(b[0])
        aa = 0 if isNaN(aa)
        bb = 0 if isNaN(bb)
        aa - bb

setTimeout sortable.init, 0

window.Sortable = sortable

  setupClickableTH: (table, th, i) ->
    type = sortable.getColumnType table, i

    onClick = (e) ->
      if event.handled isnt true
        event.handled = true
      else
        return false

      sorted = @getAttribute('data-sorted') is 'true'
      sortedDirection = @getAttribute 'data-sorted-direction'

