function hasClass(obj, cls) {
    return obj && obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
}

function addClass(obj, cls) {
    if (obj && !this.hasClass(obj, cls)) obj.className += ' ' + cls;
}

function removeClass(obj, cls) {
    if (obj && hasClass(obj, cls)) {
        var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
        obj.className = obj.className.replace(reg, ' ');
    }
}

function toggleClass(obj, cls) {
    if (hasClass(obj, cls)) {
        removeClass(obj, cls);
    } else {
        addClass(obj, cls);
    }
}

c1.documentReady(function () {
    var btnNavs_WideScreen = document.querySelector('.header .list-icon.wide-screen');
    btnNavs_WideScreen.addEventListener('click', function () {
        var body = document.querySelector('div.body');
        toggleClass(body, "collapsed");

        setTimeout(function () {
            $(window).trigger("resize");
        }, 100);
    });

    var dropdownNavBtn = document.querySelector('.header .list-icon.narrow-screen');
    var dropdownNav = document.querySelector('.dropdown-nav.narrow-screen');
    new c1.sample.MultiLevelMenu(dropdownNav, dropdownNavBtn);
});

$(window).resize(function () {
    window.__containerWidth = getContainerWidth();
    window.__containerHeight = getContainerHeight();
});

function getContainerWidth() {
    var NAVIGATION_SCREEN_LIMIT = 922;
    var NAVIGATION_MENU_WIDTH = 140;
    var NAVIGATION_MENU_COLLAPSED_WIDTH = 50;
    var bodyWidth = $(document.body).width() - 8;

    var containerWidth = bodyWidth;
    if (bodyWidth >= NAVIGATION_SCREEN_LIMIT) {
        var nav = $(".c1-col.nav");
        var navWidth = nav.css("display") != "none" ? NAVIGATION_MENU_WIDTH : NAVIGATION_MENU_COLLAPSED_WIDTH;
        containerWidth -= navWidth;
    }
    return containerWidth;
}

function getContainerHeight() {
    return $(document.body).height() - $(".c1-table .header").height();
}