$(document).ready(function () {
    $('body').on('click', '.login-open', function () {
        $('.login-block').toggleClass('login-block-vis');
        return false;
    });
    const swiper = new Swiper('.sld1', {
        loop: true,
        slidesPerView: '1',
        pagination: '.swiper-pagination',
        paginationClickable: true,
        autoplay: '5000',
        preventClicks: false
    });
    $('.main-news-info').each(function () {
        let currentMinusPRC;
        let currentPlusPRC;
        const currentRating = $(this);
        const ratingValue = parseInt(currentRating.find('.frating').text());
        const ratingPeople = parseInt(currentRating.find('.frating-num').text());
        let mPrcent;
        if (ratingValue > 0) {
            rMinus = (ratingPeople - ratingValue) / 2;
            rPlus = ratingPeople - rMinus;
            rPrcent = rPlus * 100 / ratingPeople;
            mPrcent = 100 - rPrcent;
            currentPlusPRC = rPrcent.toFixed(0);
            currentMinusPRC = mPrcent.toFixed(0);
            currentRating.find('.frating-minus a').append(rMinus);
            currentRating.find('.frating-plus a').append(rPlus);
        }
        if (ratingValue < 0) {
            rMinus = (ratingPeople - ratingValue) / 2;
            rPlus = ratingPeople - rMinus;
            rPrcent = rPlus * 100 / ratingPeople;
            mPrcent = 100 - rPrcent;
            currentPlusPRC = rPrcent.toFixed(0);
            currentMinusPRC = mPrcent.toFixed(0);
            currentRating.find('.frating-minus a').append(rMinus);
            currentRating.find('.frating-plus a').append(rPlus);
        }
        if (ratingValue == 0) {
            currentRating.find('.frating-minus a').append('0');
            currentRating.find('.frating-plus a').append('0');
        }
    });
    $('.right-menu-fix a').click(function () {
        $('body,html').animate({
            scrollTop: 0
        }, 500);
        return false;
    });
    ////////// временной портал
    const date = new Date();
    const date2 = new Date();
    const currentMouth = date.getMonth() + 1;
    const currentDate = date.getDate();
    let newNewsNum = '';
    let newCommentsNum = '';
    let newBookmarksNum = '';
    const minutesCookie = 60;
    date2.setTime(date.getTime() + (minutesCookie * 60 * 1000));
    const lastnewsUrl = $('.new-news-count').attr('rel');
    const lastcommentsUrl = $('.new-comments-count').attr('rel');
    const lastbookmarksUrl = $('.new-bookmarks-count').attr('rel');

    function getCookie(name) {
        let end;
        const dc = document.cookie;
        const prefix = name + "=";
        let begin = dc.indexOf("; " + prefix);
        if (begin == -1) {
            begin = dc.indexOf(prefix);
            if (begin != 0) return null;
        }
        else {
            begin += 2;
            end = document.cookie.indexOf(";", begin);
            if (end == -1) {
                end = dc.length;
            }
        }
        // because unescape has been deprecated, replaced with decodeURI
        //return unescape(dc.substring(begin + prefix.length, end));
        return decodeURI(dc.substring(begin + prefix.length, end));
    }

    // проверяем куки
    if (getCookie('newNewsNum')) {
        newNewsNum = $.cookie('newNewsNum');
        if (newNewsNum == 0) {
            $('.new-news-count .news-popup, .new-news-count .hoverget').remove();
        }
        if (newNewsNum != 0) { // куки есть
            $('.new-news-count span').empty().append(newNewsNum);
            newNewsNum = 0;
        }
    } else {
        $.ajax({
            url: lastnewsUrl,
            type: 'get',
            cache: true,
            success: function (data) {
                $(data).find('.main-news').each(function () {
                    const newsDay = parseInt($(this).find('.news-date-d').text());
                    const newsMouth = parseInt($(this).find('.news-date-m').text());
                    if (newsMouth == currentMouth) {
                        if (newsDay == currentDate) {
                            newNewsNum = newNewsNum + 1;
                        }
                    }
                });
                $('.new-news-count span').append(newNewsNum);
                if (newNewsNum == 0) {
                    $('.new-news-count .news-popup, .new-news-count .hoverget').remove();
                }
                /*записываем куки*/
                if (getCookie('newNewsNum')) {
                    $.cookie('newNewsNum', newNewsNum, {
                        expires: date2
                    });
                }
            }
        });
    }
    if (getCookie('newCommentsNum')) {
        newCommentsNum = $.cookie('newCommentsNum');
        if (newCommentsNum == 0) { // куки есть 0
            $('.new-comments-count .comment-popup, .new-comments-count .hoverget2').remove();
        }
        if (newCommentsNum != 0) { // куки есть
            $('.new-comments-count span').empty().append(newCommentsNum);
            newCommentsNum = 0;
        }
    } else {
        $.ajax({
            url: lastcommentsUrl,
            type: 'get',
            cache: true,
            success: function (data) {
                $(data).find('.comment-item').each(function () {
                    const newsDay = parseInt($(this).find('.news-date-d').text());
                    const newsMouth = parseInt($(this).find('.news-date-m').text());
                    if (newsMouth == currentMouth) {
                        if (newsDay == currentDate) {
                            newCommentsNum = newCommentsNum + 1;
                        }
                    }
                });
                $('.new-comments-count span').append(newCommentsNum);
                if (newCommentsNum == 0) { // куки есть 0
                    $('.new-comments-count .comment-popup, .new-comments-count .hoverget2').remove();
                }
                /*записываем куки*/
                if (getCookie('newCommentsNum')) {
                    $.cookie('newCommentsNum', newCommentsNum, {
                        expires: date2
                    });
                }
            }
        });
    }
    if (getCookie('newBookmarksNum')) {
        newBookmarksNum = $.cookie('newBookmarksNum');
        if (newBookmarksNum == 0) { // куки есть 0
            $('.new-bookmarks-count .bookmarks-popup, .new-bookmarks-count .hoverget3').remove();
        }
        if (newBookmarksNum != 0) { // куки есть
            $('.new-bookmarks-count span').empty().append(newBookmarksNum);
            newBookmarksNum = 0;
        }
    } else {
        $.ajax({
            url: lastbookmarksUrl,
            type: 'get',
            cache: true,
            success: function (data) {
                $(data).find('.bookmarks-item').each(function () {
                    const newsDay = parseInt($(this).find('.news-date-d').text());
                    const newsMouth = parseInt($(this).find('.news-date-m').text());
                    if (newsMouth == currentMouth) {
                        if (newsDay == currentDate) {
                            newBookmarksNum = newBookmarksNum + 1;
                        }
                    }
                });
                $('.new-bookmarks-count span').append(newBookmarksNum);
                if (newBookmarksNum == 0) { // куки есть 0
                    $('.new-bookmarks-count .bookmarks-popup, .new-bookmarks-count .hoverget3').remove();
                }
                /*записываем куки*/
                if (getCookie('newBookmarksNum')) {
                    $.cookie('newBookmarksNum', newBookmarksNum, {
                        expires: date2
                    });
                }
            }
        });
    }
    $('.hoverget').hover(
        function () {
            $(this).remove();
            let newNewsNum = 0;
            $.ajax({
                url: lastnewsUrl,
                type: 'get',
                cache: true,
                success: function (data) {
                    $(data).find('.main-news').each(function () {
                        const newsDay = parseInt($(this).find('.news-date-d').text());
                        const newsMouth = parseInt($(this).find('.news-date-m').text());
                        if (newsMouth == currentMouth) {
                            if (newsDay == currentDate) {
                                if (newNewsNum < 3) {
                                    $(this).find('.main-news-title a').clone().appendTo('.news-popup');
                                    newNewsNum = newNewsNum + 1;
                                }
                            }
                        }
                    });
                },
                complete: function (data) {
                    $('.load-box').remove();
                }
            });
        }, function () {
        });
    $('.hoverget2').hover(
        function () {
            $(this).remove();
            let newCommentsNum = 0;
            $.ajax({
                url: lastcommentsUrl,
                type: 'get',
                cache: true,
                success: function (data) {
                    $(data).find('.comment-item').each(function () {
                        const newsDay = parseInt($(this).find('.news-date-d').text());
                        const newsMouth = parseInt($(this).find('.news-date-m').text());
                        if (newsMouth == currentMouth) {
                            if (newsDay == currentDate) {
                                if (newCommentsNum < 5) {
                                    const lastCommentUrl = $(this).find('.comments-link a').attr('href');
                                    const lastCommentText = $(this).find('.comment-item-text').text();
                                    $('.comment-popup').append('<a href="' + lastCommentUrl + '">' + lastCommentText + '</a>');
                                    newCommentsNum = newCommentsNum + 1;
                                }
                            }
                        }
                    });
                },
                complete: function (data) {
                    $('.load-box2').remove();
                }
            });
        }, function () {
        });
    $('.hoverget3').hover(
        function () {
            $(this).remove();
            let newBookmarksNum = 0;
            $.ajax({
                url: lastbookmarksUrl,
                type: 'get',
                cache: false,
                success: function (data) {
                    $(data).find('.bookmarks-item').each(function () {
                        const newsDay = parseInt($(this).find('.news-date-d').text());
                        const newsMouth = parseInt($(this).find('.news-date-m').text());
                        if (newsMouth == currentMouth) {
                            if (newsDay == currentDate) {
                                if (newBookmarksNum < 5) {
                                    const lastBookmarksUrl = $(this).find('.bookmarks-link a').attr('href');
                                    const lastBookmarksText = $(this).find('.bookmarks-item-text').text();
                                    $('.bookmarks-popup').append('<a href="' + lastBookmarksUrl + '">' + lastBookmarksText + '</a>');
                                    newBookmarksNum = newBookmarksNum + 1;
                                }
                            }
                        }
                    });
                },
                complete: function (data) {
                    $('.load-box3').remove();
                }
            });
        }, function () {
        });
    /////
    const leftColHeight = ($('.left-col').height()) + 200;
    const rightColHeight = ($('.right-col').height()) + 200;
    $(window).scroll(function () {
        if ($(this).scrollTop() > leftColHeight) {
            $('.zoom-news .topchik').addClass('topchik-fix');
        } else {
            $('.zoom-news .topchik').removeClass('topchik-fix');
        }
    });
    $(window).scroll(function () {
        if ($(this).scrollTop() > rightColHeight) {
            $('.zoom-news .main-center-block').addClass('cols2');
        } else {
            $('.zoom-news .main-center-block').removeClass('cols2');
        }
    });
    let handleMatchMedia = function (mediaQuery) {
        if (mediaQuery.matches) {
            $('body').removeClass('zoom-news');
            $('.right-col').clone().appendTo('.chiken');
        } else {
            $('body').addClass('zoom-news');
            $('.chiken').empty();
        }
    };
    mql = window.matchMedia('all and (max-width: 1350px)');
    handleMatchMedia(mql);
    mql.addListener(handleMatchMedia);
    handleMatchMedia = function (mediaQuery) {
        if (mediaQuery.matches) {
            $('.logo, .mob-menu').clone().appendTo('.head-line');
            $('.right-menu a').clone().appendTo('.left-menu');
            $('body').on('click', '.mob-menu2, .dark', function () {
                $('.left-panel').animate({
                    left: -250
                }, function () {
                    $('.left-panel').toggleClass('left-toggle').css({width: 70});
                });
                $('.mob-menu, .mob-menu2').slideToggle(0);
                $('.dark').fadeOut();
                $('body').toggleClass('fix-scroll');
                return false;
            });
            $('body').on('click', '.mob-menu', function () {
                $('.mob-menu, .mob-menu2').slideToggle(0);
                $('.left-panel').toggleClass('left-toggle').animate({width: 250}, 0, function () {
                    $('.left-panel').animate({
                        left: 0
                    });
                });
                $('.dark').fadeIn();
                $('body').toggleClass('fix-scroll');
                return false;
            });
        } else {
            $('body').on('click', '.mob-menu2', function () {
                $('.left-panel').animate({
                    left: -250
                }, function () {
                    $('.mob-menu, .mob-menu2').slideToggle(0);
                    $('.left-panel').toggleClass('left-toggle').animate({width: 70}, 0, function () {
                        $('.left-panel').animate({
                            left: 0
                        });
                    });
                });
                return false;
            });
            $('body').on('click', '.mob-menu', function () {
                $('.left-panel').animate({
                    left: -250
                }, function () {
                    $('.mob-menu, .mob-menu2').slideToggle(0);
                    $('.left-panel').toggleClass('left-toggle').animate({width: 200}, 0, function () {
                        $('.left-panel').animate({
                            left: 0
                        });
                    });
                });
                return false;
            });
        }
    };
    mql2 = window.matchMedia('all and (max-width: 1160px)');
    handleMatchMedia(mql2);
    mql2.addListener(handleMatchMedia);
});