var CHK_Footer = {};

CHK_Footer.showPage = function(pageId) {
    var i, pages;
    pages = document.getElementsByClassName("page-content");
    for (i = 0; i < pages.length; i++) {
        if (pages[i].id == pageId) {
            pages[i].style.display = "block";
        } else {
            pages[i].style.display = "none";
        }
    }
    if (pageId == 'dashboard') {
        CHK_Dashboard.init();
    }
}

CHK_Footer.activeBottomNav = function(bottomNavId) {
    var elm = $("#" + bottomNavId);
    if (typeof elm !== 'object') {
        console.log('CHK_Footer.activeBottomNav:elm !== object:' + bottomNavId);
        return;
    }
    elm.removeClass('text-secondary');
    elm.addClass("text-primary");
    let img = elm.find("img")[0];
    if (img && img.getAttribute("data-src")) {
        img.src = img.getAttribute("data-src");
    }
}

CHK_Footer.inactiveAllBottomNav = function() {
    let navs = document.getElementsByClassName("bottom-nav");
    for (i = 0; i < navs.length; i++) {
        navs[i].className = navs[i].className.replace("text-primary", "text-secondary");
        var img = navs[i].querySelector("img");
        if (img && img.getAttribute('data-inactive-src')) {
            img.src = img.getAttribute('data-inactive-src');
        } 
    }
}

CHK_Footer.isIndexPage = function() {
    if (typeof location === 'object' && typeof location.pathname === 'string') {
        if (location.pathname.includes('index.html') == true) {
            return true;
        }
    }
    return false;
}

CHK_Footer.goIndexPage = function(tabId) {
    sessionStorage.activeTab = tabId;
    var params = {activeTab: tabId};
    if (typeof sessionStorage.sortIndex != 'undefined') {
        params.sortIndex = sessionStorage.sortIndex;
    }
    CHK.goUrlWithCurrentParams('index.html', params);
}

CHK_Footer.activeDashboardBottomNav = function() {
    CHK_Footer.inactiveAllBottomNav();
    CHK_Footer.activeBottomNav('dashboardBottomNav');
}

CHK_Footer.goDashboard = function() {
    sessionStorage.activeTab = 'dashboard';
    CHK_Footer.activeDashboardBottomNav();
    if (CHK_Footer.isIndexPage()) {
        CHK_Footer.showPage('dashboard');
        return;
    }
    CHK_Footer.goIndexPage('dashboard');
}

CHK_Footer.goOperationList = function() {
    sessionStorage.activeTab = 'operationList';
    CHK_Footer.inactiveAllBottomNav();
    CHK_Footer.activeBottomNav('operationListBottomNav');
    if (CHK_Footer.isIndexPage()) {
        CHK_Footer.showPage('operationList');
        return;
    }
    CHK_Footer.goIndexPage('operationList');
}