kindling.module(function () {
	'use strict';

	var tabs = [];

	function initSetting(setting, defaultValue) {
		localStorage[setting] = localStorage[setting] || defaultValue;
	}

	function getSettingsObject() {
		var i, settingsObject = {};
		var count = localStorage.length;
		for (i = 0; i < count; i += 1) {
			var key = localStorage.key(i);
			settingsObject[key] = localStorage.getItem(key);
		}
		return settingsObject;
	}

	function sendOptionsChangedNotification() {
		var i, count = tabs.length;
		for (i = 0; i < count; i += 1) {
			chrome.tabs.sendRequest(tabs[i], { type: 'optionsChanged', value: getSettingsObject() });
		}
	}

	function tryToCreateNotification(payload, sender, successCallback) {
		if (localStorage.disableNotificationsWhenInFocus === 'true') {
			chrome.windows.getLastFocused(function (wnd) {
				if (wnd.id === sender.tab.windowId) {
					chrome.tabs.getSelected(wnd.id, function (tab) {
						if (tab.id !== sender.tab.id) {
							successCallback(payload, sender);
						}
					});
				} else {
					successCallback(payload, sender);
				}
			});
		} else {
			successCallback(payload, sender);
		}
	}

	var showNotification = function (payload, sender) {
		var notification = webkitNotifications.createHTMLNotification('notification.html'
			+ '?room=' + payload.room
			+ '&author=' + payload.author
			+ '&avatar=' + payload.avatar
			+ '&user=' + payload.username
			+ '&baseUrl=' + kindling.getDomain(sender.tab.url)
			+ '#' + payload.message);

		notification.onclick = function () {
			chrome.windows.update(sender.tab.windowId, { focused: true });
			chrome.tabs.update(sender.tab.id, { selected: true });
		};

		notification.show();
	};

	return {
		init: function () {
			initSetting('enterRoom', 'false');
			initSetting('leaveRoom', 'false');
			initSetting('timeStamps', 'true');
			initSetting('showAvatars', 'true');
			initSetting('filterNotifications', 'false');
			initSetting('autoDismiss', 'true');
			initSetting('notifications', 'true');
			initSetting('notificationTimeout', '5000');
			initSetting('highlightName', 'true');
			initSetting('soundAndEmojiMenus', 'true');
			initSetting('disableNotificationsWhenInFocus', localStorage.focusNotifications === 'false');
			localStorage.removeItem('focusNotifications'); //obsolete option

			chrome.extension.onRequest.addListener(function (request, sender, callback) {
				if (request.type === 'notification') {
					tryToCreateNotification(request.value, sender, showNotification);
				} else if (request.type === 'init') {
					chrome.pageAction.show(sender.tab.id);
					tabs[tabs.length] = sender.tab.id;
					sendOptionsChangedNotification();
				} else if (request.type === 'unload') {
					var index = tabs.indexOf(sender.tab.id);
					if (index !== -1) {
						tabs.splice(index, 1);
					}
				} else if (request.type === 'optionsChanged') {
					sendOptionsChangedNotification();
				}

				if (callback) {
					callback();
				}
			});
		}
	};
}());

@@ -54,14 +54,16 @@
  tab
-s = %5B%5D
+Map = %7B%7D
 ;%0D%0A%0D
@@ -500,64 +500,71 @@
 var 
-i, count = tabs.length;%0D%0A%09%09for (i = 0; i %3C count; i += 1
+settingsObject = getSettingsObject();%0D%0A%09%09for (var tab in tabMap
 ) %7B%0D
@@ -595,15 +595,25 @@
 est(
-tabs%5Bi%5D
+parseInt(tab, 10)
 , %7B 
@@ -643,20 +643,17 @@
  value: 
-getS
+s
 ettingsO
@@ -657,18 +657,16 @@
 gsObject
-()
  %7D);%0D%0A%09%09
@@ -1191,19 +1191,24 @@
 %0A%09%7D%0D%0A%0D%0A%09
-var
+function
  showNot
@@ -1216,28 +1216,16 @@
 fication
- = function 
 (payload
@@ -1598,32 +1598,39 @@
 .windows.update(
+tabMap%5B
 sender.tab.windo
@@ -1624,24 +1624,19 @@
 der.tab.
-windowId
+id%5D
 , %7B focu
@@ -1743,17 +1743,16 @@
 w();%0D%0A%09%7D
-;
 %0D%0A%0D%0A%09ret
@@ -2374,16 +2374,190 @@
 tion%0D%0A%0D%0A
+%09%09%09chrome.tabs.onAttached.addListener(function (tabId, attachInfo) %7B%0D%0A%09%09%09%09if (tabMap.hasOwnProperty(tabId)) %7B%0D%0A%09%09%09%09%09tabMap%5BtabId%5D = attachInfo.newWindowId;%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D);%0D%0A%0D%0A
 %09%09%09chrom
@@ -2846,21 +2846,25 @@
 %09tab
-s%5Btabs.length
+Map%5Bsender.tab.id
 %5D = 
@@ -2874,17 +2874,23 @@
 der.tab.
-i
+windowI
 d;%0D%0A%09%09%09%09
@@ -2979,112 +2979,37 @@
 %09%09%09%09
-var index = tabs.indexOf(sender.tab.id);%0D%0A%09%09%09%09%09if (index !== -1) %7B%0D%0A%09%09%09%09%09%09tabs.splice(index, 1);%0D%0A%09%09%09%09%09%7D
+delete tabMap%5Bsender.tab.id%5D;
 %0D%0A%09%09

Fix for issue #2, clicking desktop notifications doesn't always activate the correct tab/window.