var chmiPortalWebPageUrl = "http://portal.chmi.cz/files/portal/docs/uoco/web_generator/actual_hour_data_CZ.html"
var chmiPortalJSONUrl = "http://portal.chmi.cz/files/portal/docs/uoco/web_generator/aqindex_cze.json"
var stationCode;
var periodInMinutes = 1

// SCHEDULING START
function onInit() {
    scheduleNextUpdate();
}

function onAlarm(alarm) {
    if (alarm.name === 'station-data-changed') {
        updateIcon();
        showNotification();
    } else {
        chrome.storage.sync.get({
            stationToMonitor: 'AKALA'
        }, function (items) {
            stationCode = items.stationToMonitor;
        });
        getAirQualityJSON();
        scheduleNextUpdate();
    }    
}

function scheduleNextUpdate() {
    chrome.alarms.create('refresh', { periodInMinutes: periodInMinutes });
}

chrome.runtime.onInstalled.addListener(onInit);
chrome.alarms.onAlarm.addListener(onAlarm);


chrome.runtime.onStartup.addListener(function () {
    getAirQualityJSON();
});
// SCHEDULING END

// ONCLICK START
chrome.browserAction.onClicked.addListener(showNotification);
// ONCLICK END

function updateIcon() {
    chrome.browserAction.setBadgeText({
        text: typeof localStorage.stationIndex !== 'undefined' ? localStorage.stationIndex : "?"
    });
}

function showNotification() {
    chrome.notifications.create("AQnotifID", {
        title: "*** Air Quality Checker ***",
        iconUrl: chrome.runtime.getURL('images/icon_128.png'),
        type: "basic",
        message: "Station: " + localStorage.stationName + "\nAQ status: " + localStorage.stationIndex
    }, function () { });
}
@@ -685,20 +685,16 @@
 );%0A    %7D
-    
 %0A%7D%0A%0Afunc
@@ -1119,147 +1119,880 @@
     
-chrome.browserAction.setBadgeText(%7B%0A        text: typeof localStorage.stationIndex !== 'undefined' ? localStorage.stationIndex : %22?%22%0A    %7D)
+var stationIndex = typeof localStorage.stationIndex !== 'undefined' ? localStorage.stationIndex : %22?%22;%0A    chrome.browserAction.setBadgeText(%7B%0A        text: stationIndex%0A    %7D);%0A    var iconColor = getIconColor(stationIndex);%0A    chrome.browserAction.setBadgeBackgroundColor(%7B%0A        color: iconColor%0A    %7D);%0A%7D%0A%0Afunction getIconColor(stationIndex) %7B%0A    var color;%0A    switch (stationIndex) %7B%0A        case %221%22:%0A            color = %22#C7EAFB%22;%0A            break;%0A        case %222%22:%0A            color = %22#9BD3AE%22;%0A            break;%0A        case %223%22:%0A            color = %22#FFF200%22;%0A            break;%0A        case %224%22:%0A            color = %22#FAA61A%22;%0A            break;%0A        case %225%22:%0A            color = %22#ED1C24%22;%0A            break;%0A        case %226%22:%0A            color = %22#671F20%22;%0A            break;%0A        default:%0A            color = %22#C7EAFB%22;%0A    %7D%0A    return color
 ;%0A%7D%0A

add changing badge color according to current index value