-isTouch = do ->
-  el = document.createElement 'div'
-  el.setAttribute 'ongesturestart', 'return;'
-  el.setAttribute 'ontouchstart', 'return;'
-  return 1 if typeof el.ontouchstart is "function"
-  return 2 if typeof el.ongesturestart is "function"
-  return 0
-
-
-
-
 getOffset = (element) ->
   style = getStyle element
-  reg = /matrix\(([0-9-\.,\s]*)\)/
-  tr = style('transform') || style('-webkit-transform') || style('-ms-transform') || ''
-  if reg.test tr
-    tr = reg.exec(tr)[1].split(',')[4].trim() || 0
+  regex = /matrix\(([0-9-\.,\s]*)\)/
+
+  transform = style('transform') or style('-webkit-transform') or style('-ms-transform') or ''
+
+  if regex.test transform
+    transform = reg.exec(tr)[1].split(',')[4].trim() or 0
   else
-    tr = 0
-  return toInt tr
-
-
-
-
-round = do ->
+    transform = 0
+
+  return toInt transform
+
+touchShift = do ->
   length = window.innerHeight * 0.25
-  (t, offset) ->
+
+  return (t, offset) ->
     offset = Math.abs offset
+
     if (t.x0 - t.x > length) or (t.x - t.x0 < length)
       return Math.ceil offset
+
     return Math.floor offset
-
-
-
 
 class Touch
 



Chocolate::initTouch = (env) ->
  return false if isTouch is 0

  isThumbing = false
  isClosing = false
  max = -1

  opacity = 1

  overlay = @overlay

  transparent = (n) ->
    opacity = n
    setStyle overlay, opacity: n

  addClass overlay, 'touch'
  addEvent @slider, 'click', (event) ->
    event.preventDefault()
    event.stopPropagation()

  addEvent @slider, 'hover', (event) ->
    event.preventDefault()
    event.stopPropagation()

  addEvent @slider, ['transitionend', 'webkitTransitionEnd'], =>
    removeClass @slider, 'animated'

  addEvent overlay, ['transitionend', 'webkitTransitionEnd'], =>
    removeClass overlay, 'animated'
    return unless isClosing
    @close()
    transparent 1

  t = new Touch overlay,
    start: (t) ->
      false

    move: (t) =>
      s = getOffset @slider
      dx = Math.abs(t.x0 - t.x)
      dy = Math.abs(t.y0 - t.y)
      if dx > dy
        isThumbing = true
        isClosing = false
        transparent 1
        translate @slider, s + t.dx
      else
        isClosing = true
        isThumbing = false
        transparent Math.round((1 - dy / env.h)*100)/100
      true

    end: (t) =>
      if isThumbing
        addClass @slider, 'animated'
        max = @storage.length() - 1 if max is -1
        s = getOffset @slider
        s = round t, (s / env.w)
        @select squeeze s, 0, max
      if isClosing
        addClass overlay, 'animated'

          isClosing = false
      false

  return true

touchType = do ->
  element = document.createElement 'div'

  element.setAttribute 'ongesturestart', 'return'
  return 2 if typeof element.ongesturestart is 'function'

  element.setAttribute 'ontouchstart', 'return'
  return 1 if typeof element.ontouchstart is 'function'

  return 0