// Set click events for tab blocks
document.addEventListener("click", (e) => {
    var target = (!e.target.className.includes("pTagTitleText")) ? e.target : e.target.parentElement;
    var targetID  = target.id;
    var parentElm = target.parentElement;
    var id        = 0;

    if (targetID == "closeBttn") {
        if (parentElm == oldElm) {
            var index = Array.from(parentElm.parentElement.children).indexOf(parentElm);
            (index - 1 < 0) ? index++ :  index-- ;  // Check what index to chose
            var newElm = parentElm.parentElement.children[index];

            id = parseInt(newElm.getAttribute("tabID"));
            browser.tabs.update(id, { active: true });
            setOldElm(newElm);
        }

        id = parseInt(parentElm.getAttribute("tabID"));
        browser.tabs.remove(id);

        parentElm.parentElement.removeChild(parentElm);
    } else if (targetID == "iconElm") {
        id = parseInt(target.getAttribute("tabID"));
        tabsAction.get(id).then((tab) => {
            browser.windows.update(tab.windowId, {focused: true});
            browser.tabs.update(id, { active: true });
        },id);

        setOldElm(target);
    } else if (targetID == "goTop") {
        window.scrollTo(0,0);
    } else if (targetID == "goBottom") {
        window.scrollTo(0, document.body.scrollHeight);
    } else if (targetID == "goToTab") {
        // Go to selected and 100px up
         oldElm.scrollIntoView();
         window.scrollBy(0, -100);
    } else if (targetID == "newTab") {
        createTab();
    } else if (targetID == "hideTgglBttn") {
        id = parseInt(parentElm.getAttribute("tabID"));

        if (id != oldElm.getAttribute("tabID")) {
            if (target.src.includes("eyeClosed.png")) {
                parentElm.setAttribute("class", "block");
                target.src  = "../icons/eyeOpen.png"
                unhideSelectedTab(id);
            } else {
                parentElm.setAttribute("class", "block hiddenBGColor");
                target.src  = "../icons/eyeClosed.png"
                hideSelectedTab(id);
            }
        }
    } else if (targetID == "hideAllBttn") {
        doAllTabs(0)
    } else if (targetID == "showAllBttn") {
        doAllTabs(1);
    } else if (targetID == "searchMode") {
        var currentMode = target.getAttribute("searchwindowsmode");
        if (currentMode == "true") {
            target.title           = "Searching curent window...";
            target.children[0].src = "../icons/window.png";
            target.setAttribute("searchwindowsmode", "false");
        } else {
            target.title           = "Searching all windows...";
            target.children[0].src = "../icons/windows.png";
            target.setAttribute("searchwindowsmode", "true");
        }
        clearNodes(listZone);
        getTabs();
    }
});

function setOldElm(target) {
    target.querySelectorAll(".hiderImg")[0].src = "../icons/eyeOpen.png";
    oldElm.setAttribute("class", "block");
    oldElm = target;
    target.setAttribute("class", "block block-focused");
}

document.getElementById("searchBar").onkeypress = function () {
    searchTabs();
}
