+tgAttribute = (attr) ->
+  tgAttr = if attr[0...3] == 'tg-'
+    "data-#{attr}"
+  else
+    "data-tg-#{attr}"
+
+getTGAttribute = (node, attr) ->
+  tgAttr = tgAttribute(attr)
+  node.getAttribute(tgAttr) || node.getAttribute(attr)
+
+removeTGAttribute = (node, attr) ->
+  tgAttr = tgAttribute(attr)
+  node.removeAttribute(tgAttr)
+  node.removeAttribute(attr)
+
+tgAttributeExists = (node, attr) ->
+  tgAttr = tgAttribute(attr)
+  node.getAttribute(tgAttr)? || node.getAttribute(attr)?
+
 hasClass = (node, search) ->
   node.classList.contains(search)
 

setupRemoteFromTarget = (target, httpRequestType, form = null) ->
  httpUrl = target.getAttribute('href') || target.getAttribute('action')

  throw new Error("Turbograft developer error: You did not provide a URL ('data-tg-#{urlAttribute}' attribute) for data-tg-remote") unless httpUrl

  if target.getAttribute("data-tg-remote-once") || target.getAttribute("remote-once")
    target.removeAttribute("data-tg-remote-once")
    target.removeAttribute("remote-once")
    target.removeAttribute("data-tg-remote")
    target.removeAttribute("tg-remote")

  options =
    httpRequestType: httpRequestType
    httpUrl: httpUrl
    fullRefresh: target.getAttribute('data-tg-full-refresh')? || target.getAttribute('full-refresh')?
    refreshOnSuccess: target.getAttribute('data-tg-refresh-on-success') || target.getAttribute('refresh-on-success')
    refreshOnSuccessExcept: target.getAttribute('data-tg-full-refresh-on-success-except') || target.getAttribute('full-refresh-on-success-except')
    refreshOnError: target.getAttribute('data-tg-refresh-on-error') || target.getAttribute('refresh-on-error')
    refreshOnErrorExcept: target.getAttribute('data-tg-full-refresh-on-error-except') || target.getAttribute('full-refresh-on-error-except')

  new TurboGraft.Remote(options, form, target)

TurboGraft.handlers.remoteMethodHandler = (ev) ->
  target = ev.clickTarget
  httpRequestType = target.getAttribute('data-tg-remote') || target.getAttribute('tg-remote')

  return unless httpRequestType
  ev.preventDefault()

  target = ev.target
  method = target.getAttribute('method')

  return unless tgAttributeExists(target, 'tg-remote')
  ev.preventDefault()

  remote = setupRemoteFromTarget(target, method, target)
