
; /* Start:"a:4:{s:4:"full";s:91:"/bitrix/templates/lanit/components/bitrix/news.list/news_list_ajax/script.js?16161420385387";s:6:"source";s:76:"/bitrix/templates/lanit/components/bitrix/news.list/news_list_ajax/script.js";s:3:"min";s:0:"";s:3:"map";s:0:"";}"*/
$( document ).ready(function() {


    $("#mobile_hide_news_container").click(function () {

        if ($("#news-headlines-container").is(":hidden")) {
            // $("#news-headlines-container").show("slow");
            $("#news-headlines-container").removeClass("mob_hidden");
            document.getElementById("mobile_hide_news_container").innerHTML = 'Перейти к выбранной новости';

            var navEl = document.querySelector('.news-headlines');
            pathname = location.pathname;

            [].slice.call(navEl.querySelectorAll('a'))
                .forEach(function (e) {
                    pathname === e.getAttribute('href')
                        ? target = e : null;
                })

            if (target) {

                var $to = $('#news_'+target.getAttribute('data-news_id'));
                console.log(target);

                $("#news-headlines-container").mCustomScrollbar("scrollTo", $to.position().top == 0 ? 0 : $to.position().top - 5, {
                    timeout:500
                });
            }
        } else {
            // $("#news-headlines-container").hide("slow");
            $("#news-headlines-container").addClass("mob_hidden");
            document.getElementById("mobile_hide_news_container").innerHTML = 'Список новостей';
        }
        return false;
    });

    function getNews(id) {

        BX.ajax({
            url: '/ajax/one_news.php',
            data: {
                id: id, sessid: BX.bitrix_sessid()
            },
            method: 'POST',
            dataType: 'html',
            timeout: 30,
            async: true,
            processData: true,
            scriptsRunFirst: true,
            emulateOnload: true,
            start: true,
            cache: false,
            onsuccess: function(data){
                contentEl.innerHTML = data;
            },
            onfailure: function(){
                d && d.resolve();
            }
        });
    }

    var updatestate,
        contentEl,
        updatebuttons,
        navEl;

    contentEl = document.querySelector('.news-preview');
    navEl = document.querySelector('.news-headlines')

    updatestate = function (state) {
        if (!state && location.pathname) {

            pathname = location.pathname;
            target = false;
            if (pathname) {
                [].slice.call(navEl.querySelectorAll('a'))
                    .forEach(function (e) {
                        pathname === e.getAttribute('href')
                            ? target = e : null;
                    })

                if (target) {

                    // var $to = $('#news_'+target.getAttribute('data-news_id'));
                    // $("#news-headlines-container").mCustomScrollbar("scrollTo", $to.position().top, {
                    //     timeout:1000
                    // });
                    // div.scrollTop($to.position().top);

                    state = {
                        page: pathname,
                        newsId: target.getAttribute('data-news_id'),
                        title: target.text
                    };
                }
            }
        }

        if (state) {
            var $to = $('#news_'+state.newsId);
            $("#news-headlines-container").mCustomScrollbar("scrollTo", $to.position().top == 0 ? 0 : $to.position().top - 5 , {
                timeout:1000
            });
        }

        if (!state) {

            var target = false;

            [].slice.call(navEl.querySelectorAll('a'))
                .forEach(function (e) {
                    target === false
                        ? target = e : null;
                })

            if (target) {
                state = {
                    page: target.getAttribute('href'),
                    newsId: target.getAttribute('data-news_id'),
                    title: target.text
                };

                history.pushState(state, state.title, state.page);
            } else return;
        }
        getNews(state.newsId);
        updatebuttons(state);
        document.title = state.title;
    };

    updatebuttons = function(state) {
        [].slice.call(navEl.querySelectorAll('a'))
            .forEach(function (e) {
                var classList = e.parentNode.classList;
                state.page === e.getAttribute('href')
                    ? classList.add('selected')
                    : classList.remove('selected');
            })
    }

    window.addEventListener('popstate', function (e) { updatestate(e.state); });
    window.addEventListener('DOMContentLoaded', function (e) { updatestate(e.state); });

    navEl.addEventListener('click', function (e) {
        var state;
        if (e.target.tagName !== 'A') return;

        state = {
            page: e.target.getAttribute('href'),
            newsId: e.target.getAttribute('data-news_id'),
            title: e.target.text
        };

        history.pushState(state, state.title, state.page);
        updatestate(state);
        e.preventDefault();

        if ($('#mobile_hide_news_container').is(':visible')) {
            $("#mobile_hide_news_container").click();
        }
    })

});

/* End */
;; /* /bitrix/templates/lanit/components/bitrix/news.list/news_list_ajax/script.js?16161420385387*/
