             KD.getSingleton("router").handleRoute href
     , no)
 
-    window.addEventListener 'beforeunload', @bound "beforeUnload"
-
-    document.addEventListener getVisibilityEventName(), (event)=>
-      @focusChange event, @isFocused()
+    addListener 'beforeunload', @bound 'beforeUnload'
+
+    window.onfocus = (event) => @focusChange event, yes
+    window.onblur  = (event) => @focusChange event, no
 
   addUnloadListener:(key, listener)->
     @unloadListeners[key] or= []
     @unloadListeners[key].push listener
 
   clearUnloadListeners: (key)->
+
     if key
-      @unloadListeners[key] = []
-    else
-      @unloadListeners = {}
+    then @unloadListeners[key] = []
+    else @unloadListeners = {}
+
 
   isFocused: -> !Boolean document[getVisibilityProperty()]
 


  focusChange: (event, state)->

    return unless event
    listener state, event for listener in @focusListeners

  beforeUnload:(event)->

    return unless event

    # all the listeners make their checks if it is safe or not to reload the page
    # they either return true or false if any of them returns false we intercept reload

    for own key, listeners of @unloadListeners
      for listener in listeners
        if listener() is off
          message = unless key is "window" then " on #{key}" else ""
          return "Please make sure that you saved all your work#{message}."

  setDragInAction:(@dragInAction = no)->
    $('body')[if @dragInAction then "addClass" else "removeClass"] "dragInAction"

  setMainView:(@mainView)->



  @keyViewHistory = []
  superKey        = if navigator.userAgent.indexOf("Mac OS X") is -1 then "ctrl" else "command"
  addListener     = (eventName, listener, capturePhase = yes) ->
    window.addEventListener eventName, listener, capturePhase

  constructor:(options,data)->

