$(window).on("resize", function () {
    if ($('.quant-tabs >.tabs-header').length > 0) {
        tabsShowHiddenTabs();
    }
});

$(window).on('resize', function () {
    autoSizeTabContents();
});

function autoSizeTabContents() {}

$(document).on("click", '#left-panel nav a', function (e) {
    var parentLiObj = $(this).closest('li');

    if (parentLiObj.hasClass('nav-root')) {
        //if collapsing menu item, don't set it active
        if (parentLiObj.hasClass('has-submenu')) return;
    }

    $('#left-panel nav a.active').removeClass('active');
    $(this).addClass('active');

    e.stopPropagation();
});

$(document).on("click", '#left-panel nav .arrow-down', function (e) {
    var parentItem = $(this).closest('li');
    parentItem.toggleClass('menu-opened');

    e.stopPropagation();
});

function selectFirstSubItem(liObj) {
    var aObj = liObj.find('li a')[0];

    if (liObj.find('.submenu').length) {
        if ($('body').hasClass('menu-reduced')) return;

        setTimeout(function () {
            aObj.click(); //select first subitem
        }, 200);
        return;
    }
}

$(document).on("click", '.btn-reduce-menu', function () {
    $('body').toggleClass('menu-reduced');
});

function tabsShowHiddenTabs() {
    return;
    $('.quant-tabs').each(function () {
        $(this).find('.tabs-header').children('div').removeAttr('style');
        var tabsHeaderRect = $(this).find('.tabs-header')[0].getBoundingClientRect();
        var tabsHeaderRight = tabsHeaderRect.right;
        var tabsHeaderBottom = tabsHeaderRect.top + $(this).find('.tabs-header .active').outerHeight();
        var tabsHidden = [];
        var tabsHidden2 = [];
        $(this).find('.tabs-header').children('div').each(function () {
            if (!$(this).hasClass('tabs-menu')) {
                var tabsItemRect = $(this)[0].getBoundingClientRect();
                var tabsItemTop = tabsItemRect.top;
                var tabsItemLeft = tabsItemRect.left;
                if (tabsItemTop > tabsHeaderBottom - 10 || tabsItemLeft > tabsHeaderRight - 35) {
                    tabsHidden.push($(this)[0].outerHTML);
                    tabsHidden2.push($(this));
                }
            }
        });
        for (var key in tabsHidden2) {
            tabsHidden2[key].hide();
        }
        $(this).find('.tabs-header').children('.tabs-menu').remove();
        if (tabsHidden.length > 0) {
            var $tabsMenu = $('<div class="tabs-menu">&#x25BC;</div>');
            var $tabsSubmenu = $('<div class="tabs-submenu"></div>');
            for (var itemKey in tabsHidden) {
                var itemItself = tabsHidden[itemKey];
                $tabsSubmenu.append($(itemItself));
            };
            $tabsMenu.append($tabsSubmenu);
            $(this).find('.tabs-header').append($tabsMenu);
        }
    });
}

$(document).on("click", '.quant-tabs > .tabs-header div', function () {
    var itemName = $(this).data('item');
    if (itemName == '' || itemName == undefined) return;
    var $tabsContainer = $(this).parent().parent();
    if ($tabsContainer.children('.tabs-body').length <= 0) {
        $tabsContainer = $tabsContainer.parent().parent();
    }
    var $tabsBody = $tabsContainer.children('.tabs-body');
    $tabsContainer.children('.tabs-header div').removeClass('active');
    $tabsContainer.children('.tabs-header div[data-item=' + itemName + ']').addClass('active');
    $tabsBody.children('.active').removeClass('active');
    $tabsBody.children('div[data-item=' + itemName + ']').addClass('active');
});

function tabsShowHiddenTabs() {
    $('.quant-tabs').each(function () {
        $(this).children('.tabs-header').children('div').removeAttr('style');
        var tabsHeaderRect = $(this).children('.tabs-header')[0].getBoundingClientRect();
        var tabsHeaderRight = tabsHeaderRect.right;
        var tabsHeaderBottom = tabsHeaderRect.top + $(this).find('.tabs-header .active').outerHeight();
        var tabsHidden = [];
        var tabsHidden2 = [];
        $(this).children('.tabs-header').children('div').each(function () {
            if (!$(this).hasClass('tabs-menu')) {
                var tabsItemRect = $(this)[0].getBoundingClientRect();
                var tabsItemTop = tabsItemRect.top;
                var tabsItemLeft = tabsItemRect.left;
                if (tabsItemTop > tabsHeaderBottom - 10 || tabsItemLeft > tabsHeaderRight - 35) {
                    tabsHidden.push($(this)[0].outerHTML);
                    tabsHidden2.push($(this));
                }
            }
        });
        for (var key in tabsHidden2) {
            tabsHidden2[key].hide();
        }
        $(this).children('.tabs-header').children('.tabs-menu').remove();
        if (tabsHidden.length > 0) {
            var $tabsMenu = $('<div class="tabs-menu">&#x25BC;</div>');
            var $tabsSubmenu = $('<div class="tabs-submenu"></div>');
            for (var itemKey in tabsHidden) {
                var itemItself = tabsHidden[itemKey];
                $tabsSubmenu.append($(itemItself));
            };
            $tabsMenu.append($tabsSubmenu);
            $(this).children('.tabs-header').append($tabsMenu);
        }
    });
}

$(document).on('click', '.ribbon-expandbtn', function () {
    fadeInProjectInfo();
});

function fadeInProjectInfo() {
    $('.ribbon-expandbtn').fadeOut(200);
    $('.ribbon-expanded').slideDown(200);
}

$(document).on('click', '.ribbon-expanded-hidebtn', function () {
    fadeOutProjectInfo();
});

function fadeOutProjectInfo() {
    $('.ribbon-expanded').slideUp(200);
    $('.ribbon-expandbtn').fadeIn(200);
}

$(document).on('click', '.pathInput', function (e) {
    var input = this;
    window.setTimeout(function () {
        SetCaretAtEnd(input);
    }, 100);
});

function SetCaretAtEnd(elem) {
    var elemLen = elem.value.length;
    // For IE Only
    if (document.selection) {
        // Set focus
        elem.focus();
        // Use IE Ranges
        var oSel = document.selection.createRange();
        // Reset position to 0 & then set at end
        oSel.moveStart('character', -elemLen);
        oSel.moveStart('character', elemLen);
        oSel.moveEnd('character', 0);
        oSel.select();
    } else if (elem.selectionStart || elem.selectionStart == '0') {
        // Firefox/Chrome
        elem.selectionStart = elemLen;
        elem.selectionEnd = elemLen;
        elem.focus();
    } // if
} // SetCaretAtEnd()

function positionGrid(gridName, width, height) {
    $('#' + gridName).attr('style', 'height: ' + height + '!important;' +
        'width: ' + width + '!important;' +
        'position: relative!important;' +
        'top: 0px!important;' +
        'left: 0px!important;' +
        'right: 0px!important;');
}

//filter data items on Enter press
$(document).on('keypress', '#instSearchInput', function (e) {
    if (e.keyCode == 13) {
        $('#instFilterBtn').click();
    }
});

//fix that enables multiple modal windows overlay
$(document).on('show.bs.modal', '.modal', function () {
    var zIndex = 1040 + (10 * $('.modal:visible').length);
    $(this).css('z-index', zIndex);
    setTimeout(function () {
        $('.modal-backdrop').not('.modal-stack').css('z-index', zIndex - 1).addClass('modal-stack');
    }, 0);
});

// Code goes here

$(document).on('ready', function () {
    PointerEventsPolyfill();
});

$(document).on('click', '.sq-input label', function () {
    $(this).parent().find('input').focus();
    $(this).parent().find('select').focus();
});

$(document).on('click', '.bb-left-menu', function () {
    $('label.bb-left-menu').removeClass('selected');
    $(this).addClass('selected');
});

$(document).on('click', '.conditions-menu', function () {
    $('label.conditions-menu').removeClass('selected');
    $(this).addClass('selected');
});

function PointerEventsPolyfill(options) {
    // set defaults
    this.options = {
        selector: '*',
        mouseEvents: ['click', 'dblclick', 'mousedown', 'mouseup'],
        usePolyfillIf: function () {
            if (navigator.appName == 'Microsoft Internet Explorer') {
                var agent = navigator.userAgent;
                if (agent.match(/MSIE ([0-9]{1,}[\.0-9]{0,})/) != null) {
                    var version = parseFloat(RegExp.$1);
                    if (version < 11)
                        return true;
                }
            }
            return false;
        }
    };
    if (options) {
        var obj = this;
        $.each(options, function (k, v) {
            obj.options[k] = v;
        });
    }

    if (this.options.usePolyfillIf())
        this.register_mouse_events();
}

// singleton initializer
PointerEventsPolyfill.initialize = function (options) {
    if (PointerEventsPolyfill.singleton == null)
        PointerEventsPolyfill.singleton = new PointerEventsPolyfill(options);
    return PointerEventsPolyfill.singleton;
};

// handle mouse events w/ support for pointer-events: none
PointerEventsPolyfill.prototype.register_mouse_events = function () {
    // register on all elements (and all future elements) matching the selector
    $(document).on(this.options.mouseEvents.join(" "), this.options.selector, function (e) {
        if ($(this).css('pointer-events') == 'none') {
            // peak at the element below
            var origDisplayAttribute = $(this).css('display');
            $(this).css('display', 'none');

            var underneathElem = document.elementFromPoint(e.clientX, e.clientY);

            if (origDisplayAttribute)
                $(this)
                .css('display', origDisplayAttribute);
            else
                $(this).css('display', '');

            // fire the mouse event on the element below
            e.target = underneathElem;
            $(underneathElem).trigger(e);

            return false;
        }
        return true;
    });
};

$(document).on('mouseover', '#left-panel nav > ul > li > a', function () {
    $(this).parent().find('.dropdown').addClass('hovered');
});

$(document).on('mouseout', '#left-panel nav > ul > li > a', function () {
    $(this).parent().find('.dropdown').removeClass('hovered');
});