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

$(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');
});

$(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();
    $('[data-toggle="tooltip"]').tooltip()
});

$(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');
});

var ctrlPressed = false,
    shiftPressed = false;

$(document).keydown(function (event) {
    if (event.which == 16) {
        shiftPressed = true;
    } else if (event.which == 17) {
        ctrlPressed = true;
    }
    if (event.which == 13 && window.activePopup != undefined && window.activePopup && !window.activeChosen) {
        if (window.activePopup.hasClass("no-enter")) {
            return;
        }
        window.activePopup.find(".modal-footer .btn-primary").click();
    }
});

$(document).keyup(function () {
    ctrlPressed = false;
    shiftPressed = false;
});

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

$(document).on('click', "#performanceDialog .quant-tabs .tabs-header > div", function () {
    $(this).parent().find("div").removeClass("active");
    $(this).addClass("active");
});