(function (document, window, $) {
    if ($('#dayitime').lenght) {
        function addZero(i) {
            if (i < 10) {
                i = "0" + i;
            }
            return i;
        }

        function getCurrentTime1() {
            var today_time, h, m;
            today_time = new Date(Date.now());
            h = today_time.getUTCHours();
            m = addZero(today_time.getUTCMinutes());
            return (h + 3) + '<span class=timeseparator>:</span>' + m;
        }

        function setTimer1() {
            today_time.innerHTML = getCurrentTime1();
        }

        setInterval(setTimer1, 1000);
    }

    function clock() {
        var d = new Date();
        var month_num = d.getMonth()
        var day = d.getDate();
        var hours = d.getHours();
        var minutes = d.getMinutes();
        var seconds = d.getSeconds();
        var weekday_num = d.getDay();

        var month = new Array("января", "февраля", "марта", "апреля", "мая", "июня", "июля", "августа", "сентября", "октября", "ноября", "декабря");
        var weekday = new Array("воскресенье", "понедельник", "вторник", "среда", "четверг", "пятница", "суббота");

        if (day <= 9) day = "0" + day;
        if (hours <= 9) hours = "0" + hours;
        if (minutes <= 9) minutes = "0" + minutes;
        if (seconds <= 9) seconds = "0" + seconds;

        var date_time = day + " " + month[month_num] + ", " + weekday[weekday_num] + " ";
        if (document.layers) {
            document.layers.today_day.document.write(date_time);
            document.layers.today_day.document.close();
        }
        else document.getElementById("today_day").innerHTML = date_time;
        setTimeout(clock, 1000);
    }

    if ($('#filterservice').length) {
        function checktoggle() {
            if ($("#switch_old").is(':checked')) {
                $('#switch_old2').addClass("filterservice__labelbig--active");
                $('#switch_old1').removeClass("filterservice__labelbig--active");
            } else {
                $('#switch_old1').addClass("filterservice__labelbig--active");
                $('#switch_old2').removeClass("filterservice__labelbig--active");
            }

            if ($("#switch_male").is(':checked')) {
                $('#switch_male2').addClass("filterservice__labelbig--active");
                $('#switch_male1').removeClass("filterservice__labelbig--active");
            } else {
                $('#switch_male1').addClass("filterservice__labelbig--active");
                $('#switch_male2').removeClass("filterservice__labelbig--active");
            }
        }
    }

    $(document).ready(function () {

        $(".region__link").click(function (e) {
            e.preventDefault();
        });

        setTimeout(function () {
            $(".shortnews__description").dotdotdot();
        }, 10);

        if ($('#filterservice').length) {
            checktoggle();
        }


        $(".filterservice__toggle").click(function () {
            checktoggle();
        });

        if ($('#datefilter').length) {
            $('#datefilter').dateRangePicker({
                autoClose: true,
                language: 'ru',
                duration: 500,
                startOfWeek: "monday",
                getValue: function () {
                    return document.getElementById('datefiltertext').innerHTML;
                }, setValue: function (s) {
                    var dates = s.split(' - ');
                    $('.newsfilter__date-from').val(dates[0]);
                    $('.newsfilter__date-to').val(dates[1]);
                    document.getElementById('datefiltertext').innerHTML = s;
                }
            });
        }
        if ($("*").is('.calendinfo__calendcase')) {
            $('.calendinfo__calendcase').dateRangePicker({
                inline: true,
                language: 'ru',
                container: '#calendinfo_range',
                alwaysOpen: true,
                singleMonth: true,
                showTopbar: false,
                singleDate: true,
                startOfWeek: "monday",
            });
        }

        if ($("*").is('#calendinfo')) {
            clock();
        }

        $('input, textarea').focus(function () {
            $(this).data('placeholder', $(this).attr('placeholder'))
            $(this).attr('placeholder', '');
        }).blur(function () {
            $(this).attr('placeholder', $(this).data('placeholder'));
        });

        $('#organization__title, #specialization__title').click(function (e) {
            $(this).next().toggleClass('active');
            $(this).find('.leftbar__arrow').toggleClass('active');
            e.preventDefault();
        });

        if ($("*").is('#full_news__sliderlist')) {
            $('#full_news__sliderlist').lightSlider({
                selector: '.full-news__slirderitem',
                item: 1,
                loop: true,
                auto: true,
                pause: 8000,
                speed: 1000,
                gallery: true,
                thumbItem: 4
                // controls: false,
                // onSliderLoad: function(el) {
                //     el.lightGallery({
                //         selector: '#fullnews__gallerylist .lslide'
                //     });
                // },
            });
        }

        if ($("*").is('#mainsliderId')) {
            $('#mainsliderId').lightSlider({
                selector: '.mainslider__item',
                item: 1,
                loop: true,
                auto: true,
                pause: 8000,
                speed: 1000,
                // mode: 'fade',
                controls: false
            });
        }

        /*$('.centerbar input, .centerbar select').styler();*/

        if ($('#newsfilter').length) {
            $("#newsfiltericon").click(function () {
                $(".newsfilter__open").toggleClass('newsfilter__open--show');
            });
        }

    });

    $(window).scroll(function () {
        if ($(this).scrollTop() > 750 && $('.header, .content').hasClass('loading')) {
            $('.header, .content').removeClass('loading').addClass('scrolling');
        } else if ($(this).scrollTop() <= 750 && $('.header, .content').hasClass('scrolling')) {
            $('.header, .content').removeClass('scrolling').addClass('loading');
        }
    });

    // isolate scope for easier moving and extension
    (function ($) {
        new SpecFilter.SpecFilter($('.newsfilter__specializations .newsfilter__text'));
        new RegionFilter.RegionFilter($('.newsfilter__regions .newsfilter__text'), {
            itemsContainerClass: 'regionsfilter__container',
            itemContainerClass: 'regionsfilter__region',
            itemCheckboxClass: 'regionsfilter__checkbox',
            itemLabelClass: 'regionsfilter__label'
        });
    })($);

    // isolate scope for easier moving and extension
    (function (w) {
        w.showFotorama = function (jQuery, fotoramaObjectClass, fotoramaPrevClass, fotoramaNextClass) {
            var $selector = jQuery(fotoramaObjectClass);
            $selector.data('navwidth', ($selector.outerWidth() - 80) + 'px');
            var $fotoramaDiv = $selector.fotorama();
            var fotorama = $fotoramaDiv.data('fotorama');
            jQuery(fotoramaPrevClass).click((function (e) {
                fotorama.show('<');
            }).bind(this));
            jQuery(fotoramaNextClass).click((function (e) {
                fotorama.show('>');
            }).bind(this));
        };
    })(window);

    // isolate scope for easier moving and extension
    (function (assetsUrl, document, $) {
        var n = document.getElementsByTagName('script')[0],
            s = document.createElement('script'),
            c = document.createElement('link');
        c.setAttribute('rel', 'stylesheet');
        c.href = assetsUrl.replace(/\/+$/, '') + '/css/slick.css';
        c.setAttribute('type', 'text/css');
        n.parentNode.insertBefore(c, n);

        s.async = true;
        s.onload = function () {
            $('.organization-media__slider').slick({
                infinite: true,
                dots: false,
                slidesToShow: 3,
                slidesToScroll: 1,
                prevArrow: '<div class="slick-prev"></div>',
                nextArrow: '<div class="slick-next"></div>'
            });
        };
        s.src = assetsUrl.replace(/\/+$/, '') + '/js/slick.min.js';
        n.parentNode.insertBefore(s, n);
    })(window.assetsUrl || '/', document, $);

    // isolate scope for easier moving and extension
    (function ($) {
        $('.organization-info__slidedown-handler').click(function (e) {
            e.preventDefault();
            var $content = $(this)
                .parents('.organization-info__slidedown')
                .find('.organization-info__slidedown-content');
            $content.slideToggle();
        });
    })($);

    // isolate scope for easier moving and extension
    (function ($) {
        $('.organization-info__description-read-more').click(function (e) {
            var $button;
            var $container;
            e.preventDefault();
            $button = $(this);
            $container = $button.parent().find('.organization-info__description');
            $container.animate({'max-height': $container.find('.organization-info__description-content').outerHeight() + 100}, 100, function () {
                $container.removeClass('organization-info__description--short').css('maxHeight', 'none');
                $button.remove();
            });
        });
    })($);

    // isolate scope for easier moving and extension
    (function ($) {
        if ($('.organization-list__side-menu').length) {
            var $menuItems = $('.organization-list__side-menu .organization-list__side-menu-item');
            $menuItems.each(function () {
                if ($(this).find('.organization-list__side-submenu').length) {
                    var $arrow = $('<span class="organization-list__side-menu-item-arrow"></span>');
                    $arrow.click(function () {
                        $(this).parent().toggleClass('active');
                    });
                    $(this).append($arrow);
                }
            });
        }
    })($);

    // isolate scope for easier moving and extension
    (function ($) {
        if ($('.specialization-selection__more').length) {
            $('.specialization-selection__more').click(function (e) {
                e.preventDefault();
                var $list = $(this).parent().find('.specialization-selection__list');
                $(this).parent().find('.specialization-selection__list').animate({'max-height': $list[0].scrollHeight + 20}, 200);
                $(this).remove();
            });
        }
    })($);

    // isolate scope for easier moving and extension
    (function ($) {
        if ($('.specialization-selection__checkbox--group-header').length) {
            $('.specialization-selection__checkbox--group-header').change(function () {
                $(this).parents('.specialization-selection__group')
                    .find('.specialization-selection__list .specialization-selection__checkbox')
                    .prop('checked', $(this).prop('checked'));
            });
        }
    })($);

})(window.document, window, window.jQuery);