// Set click events for tab blocks
document.addEventListener("click", (e) => {
    var target    = undefined;

    if (!e.target.className.includes("pTagTitleText")) {
        target = e.target;
    } else {
        target = e.target.parentElement;
    }
    var targetID  = target.id;
    var parentElm = target.parentElement;
    var id        = 0;


    if (targetID == "closeBttn") {
        id = parseInt(parentElm.getAttribute("tabID"));
        browser.tabs.remove(id);
        parentElm.parentElement.removeChild(parentElm);
    } else if (targetID == "iconElm") {
        id = parseInt(target.getAttribute("tabID"));
        browser.tabs.update(id, { active: true });

        target.querySelectorAll(".hiderImg")[0].src = "../icons/eyeOpen.png";
        oldElm.setAttribute("class", "block");
        oldElm = target;
        target.setAttribute("class", "block block-focused");
    } 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);
    }
});

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