   "http://#{ domain }/#{ DEFAULT_PATH }"
 
 # Creates a new Strophe.Connection object with the appropriate options.
-create_connection_obj = ->
+create_connection_obj = (opt) ->
 
   new Strophe.Connection get_bosh_url
-    path:     connection_options.path
-    host:     connection_options.host
-    port:     connection_options.port
-    protocol: connection_options.protocol
+    path:     opt.path
+    host:     opt.host
+    port:     opt.port
+    protocol: opt.protocol
 
 key2cache   = (k) -> "#{CACHE_PREFIX}#{k}"
 
-saveData = ->
-  if sessionStorage?
-    for k in ["jid", "sid", "rid"] when connection[k]?
-      sessionStorage[key2cache k] = connection[k]
-
-    for k in ["host", "port", "path", "protocol"] when connection_options[k]?
-      sessionStorage[key2cache k] = connection_options[k]
-
-clearData = -> sessionStorage?.clear()
-
-onConnected = ->
-
-  # The BOSH connection get aborted by the escapae event.
-  # Therefore we have to prevent it!
-
-  fn       = (ev) -> ev.preventDefault?() if ev.keyCode is 27
-  onunload = -> if connection then saveData() else clearData()
-
-  if document.addEventListener?
-    for e in ["keydown", "keypress", "keyup"]
-      document.addEventListener e, fn, false
-
-    # Save connection properties before reloading the page.
-    window.addEventListener "unload", onunload, false
-
-  else if document.attachEvent?
-    for e in ["onkeydown", "onkeypress", "onkeyup"]
-      document.attachEvent e, fn
-
-    # Save connection properties before reloading the page.
-    document.attachEvent "onunload", onunload
-
-  connection.send $pres()
+saveData = (conn, opt)->
+  return unless sessionStorage?
+  for k in ["jid", "sid", "rid"] when conn[k]?
+    sessionStorage[key2cache k] = conn[k]
+
+  for k in ["host", "port", "path", "protocol"] when opt[k]?
+    sessionStorage[key2cache k] = opt[k]
+
+clearData = ->
+  return unless sessionStorage?
+  for k in ["jid", "sid", "rid", "host", "port", "path", "protocol"]
+    sessionStorage.removeItem key2cache k
+
+# Loads the connection data from session storage.
+restoreData = ->
+  return unless sessionStorage?
+  o = {}
+  for k in [ "jid", "sid", "rid", "host", "port", "path", "protocol" ]
+    j = key2cache k
+    o[k] = sessionStorage[j] if sessionStorage[j]
+
+hasConnectionData = (opt) ->
+  for k in [ "jid", "sid", "rid" ]
+    if not opt[k] or opt[k] is 'null' or opt[k] is 'undefined'
+      return false
+  true
 
 statusCodeToString = (s) ->
   switch s

    when 7 then "Disconnecting"
    when 8 then "Reconnected"

# Processes the Strophe connection staus.
#
# Parameters:
# (int) status - The connection status code. See <Strophe.Status>

on_connection_change = (status) ->

  console.info "xmpp status changed: " + statusCodeToString status

  s = Strophe.Status

  switch status

    when s.ERROR
      resetPlugin()
      mediator.emit "error", "an error occoured"
    when s.CONNECTING
      resetPlugin()
      mediator.emit "connecting"
    when s.CONFAIL
      resetPlugin()
      mediator.emit "error", "could not connect to xmpp server"
    when s.AUTHENTICATING
      resetPlugin()
      mediator.emit "authenticating"
    when s.AUTHFAIL
      resetPlugin()
      mediator.emit "authfail"
    when s.CONNECTED
      updatePlugin connection
      onConnected()
      mediator.emit "connected"
    when s.DISCONNECTED
      clearData()
      resetPlugin()
      mediator.emit "disconnected"
    when s.DISCONNECTING
      resetPlugin()
      mediator.emit "disconnecting"
    when s.ATTACHED
      updatePlugin connection
      onConnected()
      mediator.emit "attached"

resetPlugin = ->
  xmppPlugin.connection = null
  xmppPlugin.jid = ""

updatePlugin = (conn) ->
  xmppPlugin.connection = conn
  xmppPlugin.jid = conn.jid

# Attaches an existing connection to the Strophe connection object.
#
# Parameters:
# (Object) cookie - the cookie object that contains the connection properties
# like host, port, path, rid, sid and jid.
attach_connection = (opt) ->

  connection = create_connection_obj()
  connection.attach connection_options.jid
    , connection_options.sid
    , connection_options.rid
    , on_connection_change
    , 60

# Connects to the xmpp server and login with the given JID.
#
# Parameters:
# (String) jid  - Jabber ID
# (String) pw - Password
# (Object) opt - Connection options.

login = (jid, pw, opt) ->

  if opt?
    connection_options.path     = opt.path     if opt.path
    connection_options.port     = opt.port     if opt.port
    connection_options.host     = opt.host     if opt.host
    connection_options.protocol = opt.protocol if opt.protocol

  connection = create_connection_obj()
  connection.connect jid, pw, on_connection_change

disconnect = ->

  console.debug "try to disconnect"

  if connection?.connected is true
    try
      connection.send $pres(type: "unavailable")
      connection.pause()
      connection.disconnect()
    catch e
  clearData()

# Converts a Jabber ID to an css friendly id.
#
# It replaces '.' and '@' with '-'.
#
# Parameters:
# (String) - Jabber ID
jid_to_id = (jid) ->
  Strophe.getBareJidFromJid(jid)
    .replace("@", "-")
    .replace(".", "-")
    .replace ".", "-"

# Loads the connection data from local storage.
restoreData = ->
  if sessionStorage?
    for k in [ "jid", "sid", "rid", "host", "port", "path", "protocol" ]
      j = key2cache k
      connection_options[k] = sessionStorage[j]  if sessionStorage[j]

hasConnectionData = ->
  opt = connection_options
  for k in [ "jid", "sid", "rid"  ]
    if not opt[k] or opt[k] is 'null' or opt[k] is 'undefined'
      return false
  true

init = ->
  restoreData()
  if hasConnectionData()
    attach_connection()
  else
    mediator.emit "disconnected"

# TODO: create core plugin
xmppPlugin =
  init:       init
  jid:        ""
  connection: null
  login:      login
  logout:     disconnect
  on:  -> mediator.on.apply mediator, arguments
  off: -> mediator.off.apply mediator, arguments

plugin =
  id: ID
  base:
    xmpp: xmppPlugin

scaleApp.plugin.register plugin if window?.scaleApp?
module.exports = plugin if module?.exports?
(define -> plugin) if define?.amd?

Copyright (c) 2012 - 2013 Markus Kohlhase <mail@markus-kohlhase.de>
###

DEFAULT_PATH     = "http-bind/"
DEFAULT_PROTOCOL = "http"
DEFAULT_PORT     = 5280

CACHE_PREFIX     = "scaleApp.xmpp.cache."

# Generates an appropriate bosh url for the connection.
#
