function chatController(postId) {
  if (!Notification) {
    alert('Desktop notifications not available in your browser. Try Chromium.'); 
    return;
  }
  if (Notification.permission !== "granted") {
    Notification.requestPermission().then(function(permission) { 
       
    });;
  }
  var chatDAO = new ChatDAO(postId);
  var lastChatId;
  var firstChatId;
  var isScrollingUp;
  var isWindowActive = true;
  $(window).blur(function(){
    isWindowActive = false;
  });
  $(window).focus(function(){
    isWindowActive = true;
  });
  chatDAO.getChatsInfo({limit: 30}, function(chatsInfo) {
    var members = [];
    var membersMap = {};
    chatsInfo.members.forEach(function(m) {
      var user = new User(m);
      members.push(user);
      membersMap[user.id] = user;
    });
    
    var chat = new Chat(new User(current_user), members, {}, function(message, callback) {
      myMessageSent = true;
      chatDAO.sendMessage(message, function(error, data) {
        if (callback) {
          callback(error, data);
        }
      });
    });
    
    if (chatsInfo.chats.length > 0) {
      lastChatId = chatsInfo.chats[chatsInfo.chats.length - 1].id;
      firstChatId = chatsInfo.chats[0].id; // handle case when no messages.
    }

    chat.setScrollTopEventCallback(function() {
      if (!isScrollingUp) {
        isScrollingUp = true;
        chatDAO.getChatsInfo({before_id: firstChatId, limit: 10}, function(ci) {
          if (ci.chats.length > 0) {
            firstChatId = ci.chats[0].id; // move this to a function
          }
          var messages = getMessagesFromChatInfo(ci);
          chat.addMessagesToTop(messages);
          isScrollingUp = false;
        });
      }
    });
    chat.addMessages(getMessagesFromChatInfo(chatsInfo));
    $('.js_upload_form').on("ajax:remotipartComplete", function(e, data) {
      var c = JSON.parse(data.responseText).chat;
      var msg = new Message(c, membersMap[c.user.id]);
      chat.addMessage(msg);
    });

    setInterval(function() {
      fetchLattestMessages(lastChatId);
    }, 5000);
    
    function getMessagesFromChatInfo(chatsInfo, excludeMyMessages) {
      var messages = [];
      chatsInfo.chats.forEach(function(chat) {
        if (!(excludeMyMessages && chat.user.id == current_user.id)) {
          var message = new Message(chat, membersMap[chat.user.id]);
          messages.push(message);  
        } // TODO this will not work if multiple windows are open FIX
      });
      return messages;
    }

    function getUsersFromChatInfo(chatsInfo) {
      var members = [];
      chatsInfo.members.forEach(function(m) {
        var user = new User(m);
        members.push(user);
      });
      return members;
    }

    function fetchLattestMessages() {
      chatDAO.getChatsInfo({after_id: lastChatId}, function(ci) {
        if (ci.chats.length > 0) {
          lastChatId = ci.chats[ci.chats.length - 1].id; // move this to a function
        }
        var messages = getMessagesFromChatInfo(ci, true);
        chat.addMessages(messages);
        chat.addUsersToUserArea(getUsersFromChatInfo(ci));
        desktopNotification(messages);
      });
    }

  });

  function desktopNotification(messages) {
    if (!isWindowActive) {
      if (Notification.permission !== "granted") {
        Notification.requestPermission();
      } else {
        messages.forEach(function(message) {
          var notification = new Notification('New message from ' + message.user.name, {
            icon: message.user.picture,
            body: message.text
            //sound: path //TODO
          });

          notification.onclick = function () {
            window.focus(); 
            //window.open("http://stackoverflow.com/a/13328397/1269037");      
          };
        });
      }  
    }
  }  

}



@@ -158,45 +158,60 @@
 if (
-Notification.permission !== %22granted%22
+localStorage.getItem(%22desktopNotification%22) === null
 ) %7B%0A
@@ -275,24 +275,144 @@
 sion) %7B 
-%0A       
+// denied, granted%0A       localStorage.setItem(%22desktopNotification%22, permission); //TPDO: move to sperate file(LocalStorageDAO)
 %0A    %7D);
@@ -3363,49 +3363,59 @@
 tive
-) %7B%0A      if (Notification.permission !
+ && localStorage.getItem(%22desktopNotification%22) 
 ==
+=
  %22gr
@@ -3428,67 +3428,8 @@
 ) %7B%0A
-        Notification.requestPermission();%0A      %7D else %7B%0A  
     
@@ -3463,26 +3463,24 @@
 (message) %7B%0A
-  
         var 
@@ -3564,18 +3564,16 @@
         
- 
- 
 icon: me
@@ -3602,18 +3602,16 @@
         
-  
 body: me
@@ -3621,18 +3621,16 @@
 ge.text%0A
-  
         
@@ -3660,25 +3660,21 @@
         
-  
 %7D);%0A%0A
-  
         
@@ -3720,18 +3720,16 @@
         
-  
 window.f
@@ -3737,18 +3737,16 @@
 cus(); %0A
-  
         
@@ -3827,15 +3827,11 @@
     
-  
 %7D;%0A
-  
     
@@ -3840,18 +3840,8 @@
 %7D);%0A
-      %7D  %0A
     

use localStorage to store desktop notification accept/reject status