(function () {
    var url_string = window.location.href;
    var url = new URL(url_string);
    var disable_focus = url.searchParams.get("df");
    if (disable_focus == 1) {
        //console.log('set it')
        Cookies.set('disable_focus', 1, {expires: 100000});
    } else if (disable_focus == 2) {
        Cookies.remove('disable_focus');
    }
    //console.log(Cookies.get('disable_focus'))
    if (Cookies.get('disable_focus') != 1) {
        bindDefaultEvents()

        function keyEventHandler(e) {
            var currentid = $(".activeFocus").attr('id');
            var nextid;
            var currentRowFocusableCount = $('.activeFocus').closest('.rowfcosuable').find('.fcosuable').length;
            var currentRowID = $('.activeFocus').closest('.rowfcosuable').attr('id').split('-')[1];
            var parentRowID = parseInt(currentRowID) - 1;
            var parentRowFocusableCount = $('#row-' + parentRowID).find('.fcosuable').length;
            var nextRowID = parseInt(currentRowID) + 1;
            var nextRowFocusableCount = $('#row-' + nextRowID).find('.fcosuable').length;
            /*        console.log('currentRowMaxID ' + currentRowMaxID);
                    console.log('currentRowFocusableCount ' + currentRowFocusableCount);
                    console.log('nextRowFocusableCount ' + nextRowFocusableCount);
                    console.log('parentRowFocusableCount ' + parentRowFocusableCount);
                    console.log('nextRowID ' + nextRowID);*/
            switch (e.keyCode) {
                case 40:
                    //down
                    var currentRowMaxID = 0;

                    $('.activeFocus').closest('.rowfcosuable').find('.fcosuable').each(function () {
                        currentRowMaxID = Math.max(this.id, currentRowMaxID);
                    });
                    /*            console.log('currentRowMaxID ' + currentRowMaxID);
                                console.log('currentRowFocusableCount ' + currentRowFocusableCount);
                                console.log('nextRowFocusableCount ' + nextRowFocusableCount);
                                console.log('nextRowID ' + nextRowID);
                                console.log('currentid ' + currentid);*/
                    if (currentRowFocusableCount <= nextRowFocusableCount) {
                        nextid = parseInt(currentid) + currentRowFocusableCount
                    } else {
                        //next rown is smaller
                        var diff = currentRowMaxID - currentid;
                        nextid = parseInt(diff) + parseInt(currentid) + parseInt(1)
                    }
                    // nextid = parseInt(currentid) + currentRowFocusableCount
                    //console.log('nextid ' + nextid)

                    break;
                case 37:
                    //left
                    nextid = parseInt(currentid) + 1
                    break;
                case 39:
                    //right
                    nextid = parseInt(currentid) - 1;
                    break;
                case 38:
                    //up
                    if (currentRowFocusableCount > parentRowFocusableCount) {
                        nextid = parseInt(currentid) - parentRowFocusableCount
                    } else {
                        nextid = parseInt(currentid) - currentRowFocusableCount
                    }
                    break;
            }
            //check if nextid is exsist
            //console.log('next id is: ' +nextid);
            //console.log(document.activeElement);

            //console.log($('#' + nextid));
            if ($('#' + nextid).length > 0) {
                //console.log('nextid: '+nextid)
                //$('.activeFocus').removeClass('slick-active')
                $('.activeFocus').removeClass('activeFocus')
                $('#' + nextid).addClass('activeFocus').focus();
                //$('#' + nextid).addClass('slick-active').focus();
                var $this = $('.activeFocus');
                $('html, body').animate({
                    scrollTop: $this.offset().top - 100
                }, 100);
            }
            //console.log('active class is ' + document.getElementsByClassName('activeFocus')[0].id);
            //console.log('active class is ' + document.activeElement.id);

        }

        function bindDefaultEvents() {
            //console.log('loaded')
            document.addEventListener('keydown', keyEventHandler);
        }

        /*
            window.onload = bindDefaultEvents;
        */
    }

}());
