
jQuery(function($) {
    
    jQuery(function($) {
        $("a.fancybox").fancybox({
            prevEffect: 'none',
            nextEffect: 'none',
            helpers: {
                overlay: {
                    locked: false
                }
            }
        });
    });
    $(document).ready(function() {
        var height = $('.dummy').height();
        var wheight=$('.tagcont').height();
        if (height>wheight) {
            $('.smtags').show();
        }
        $('.smtags a').click(function(){
            if ($('.tagcont').is('.shown')) {
                $('.tagcont').removeClass('shown');
                $('.smtags a').text('Показать еще');
            } else {
            $('.tagcont').addClass('shown');
            $('.smtags a').text('Скрыть');
            }
            })
     /*var elem=$('select.cityselect option:selected');
            var phone=elem.attr('data-phone');
            var citycode=elem.val();            
        
        console.log(citycode);        
        $('div.number span span').html('<a href="tel:'+phone+'">'+phone+'</a>')
        console.log(phone);
    $('select.cityselect').change(
        function(){
            var elem=$('select.cityselect option:selected');
            var phone=elem.attr('data-phone');
            var citycode=elem.val();            
        
        console.log(citycode);
        var cookie_date = new Date(new Date().getTime() + 60 * 100000);
        $('div.number span span').html('<a href="tel:'+phone+'">'+phone+'</a>');
        console.log(phone);
        document.cookie = "CITYCODE="+citycode+"; expires=" + cookie_date.toGMTString()+"; path=/;domain=ngs-penza.ru; secure";
    })/**/
        
        
        $(".popup").fancybox({
            width: 400,
            'autoSize': false,
            wrapCSS: "window"
        });
    });

    /*$('form#frm1').submit(function(e){
        
        var data=$(this).serialize();        
        $.ajax({
        method:'post',
        data:data,
        url: "/ajax/recaptcha.php",
        success: function(data){
          if (data==1) {            
        
          } else {
            
            alert('Не пройдена проверка на робота!');
            return false;
          }
        }
      });
        })*/
    $(function() {
        if (navigator.userAgent.toLowerCase().indexOf('msie') != -1) {
            $('input[placeholder]').each(function() {
                if (this.value == '') {
                    var original_value = this.value;
                    var this1 = this;
                    var value = this.value = this.getAttribute('placeholder');
                    this.style.color = 'white';
                    $(this).focus(function() {
                        if (this.value == value) {
                            this.value = '';
                            this.style.color = 'black';
                        }
                    });
                    $(this).blur(function() {
                        if (this.value == '') {
                            this.value = value;
                            this.style.color = 'white';
                        }
                    });
                    $(this.form).submit(function() {
                        if (this1.value == value) {
                            this1.value = original_value;
                        }
                    });
                }
            });
        }
    });
    $(document).ready(function() {
        $('#first_inp').autotab({
            target: 'second_inp',
            maxlength: 2
        });
        $('#second_inp').autotab({
            target: 'third_inp',
            previous: 'tiparm',
            maxlength: 2
        });
        $('#third_inp').autotab({
            target: 'fourth_inp',
            previous: 'matkor',
            maxlength: 3
        });
        $('#fourth_inp').autotab({
            target: 'fifth_inp',
            previous: 'konstr',
            maxlength: 2
        });
        $('#fifth_inp').autotab({
            previous: 'matpov',
            maxlength: 2
        });
    });

    function getOffsetRect(elem) {
        var box = elem.getBoundingClientRect()
        var body = document.body
        var docElem = document.documentElement
        var scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop
        var scrollLeft = window.pageXOffset || docElem.scrollLeft || body.scrollLeft
        var clientTop = docElem.clientTop || body.clientTop || 0
        var clientLeft = docElem.clientLeft || body.clientLeft || 0
        var top = box.top + scrollTop - clientTop
        var left = box.left + scrollLeft - clientLeft
        return {
            top: Math.round(top),
            left: Math.round(left)
        }
    }
    $(".top_menu ul li ").hover(function() {
        if ($(window).width() < 960) return;
        var ss;
        var tt;
        var xx = 0;
        var br = getOffsetRect(this);
        var ss = br.left;
        var tt = br.top;
        $(".sub_menu", this).attr({
            style: "left: " + (ss + 0.5) + "px; top: " + (tt + 69) + "px;"
        });
        $(".sub_menu", this).show();
    }, function() {
        $(".sub_menu", this).hide();
    });
    $(document).on("click", ".toggle", function(e) {
        e.preventDefault();
        $(this).parent().siblings().removeClass("active");
        $(this).parent().toggleClass('active');
    });
    $(document).click(function(e) {
        if ($(e.target).closest(".top_menu .inner_block.active").length) return;
        $(".top_menu .inner_block.active").removeClass("active");
    });
    $(document).on("click", ".top_menu .inner_block.active ul > li > a", function(e) {
        e.preventDefault();
        $(this).parent().siblings().removeClass("active");
        $(this).parent().toggleClass('active');
    });
    $(document).on("click", ".toggle_catalog", function(e) {
        e.preventDefault();
        $(this).parent().siblings().removeClass("active");
        $(this).parent().toggleClass('active');
    });
    $(document).click(function(e) {
        if ($(e.target).closest(".control_block.active").length) return;
        $(".control_block.active").removeClass("active");
    });
    $(document).on("click", ".control_block.active .catalog_menu > ul > li > div", function(e) {
        e.preventDefault();
        $(this).parent().siblings().removeClass("active");
        $(this).parent().toggleClass('active');
    });
    $(document).on("click", ".filter_block .item .name_filter", function(e) {
        e.preventDefault();
        $(this).parent().siblings().removeClass("active");
        $(this).parent().toggleClass('active');
    });
    $(document).on("click", ".filter_block .items .name_filters", function(e) {
        e.preventDefault();
        $(this).parent().siblings().removeClass("active");
        $(this).parent().toggleClass('active');
    });
    $(document).on("click", ".item_inner .items .name_filters", function(e) {
        e.preventDefault();
        $(this).parent().siblings().removeClass("active");
        $(this).parent().toggleClass('active');
    });
    $('select').each(function() {
        $(this).siblings('p').text($(this).children('option:selected').text());
    });
    $('select').change(function() {
        $(this).siblings('p').text($(this).children('option:selected').text());
    });
    $(document).on("click", ".toggle_filter", function(e) {
        e.preventDefault();
        $(this).parent().siblings().removeClass("active");
        $(this).parent().toggleClass('active');
    });
    jQuery(document).ready(function() {
        jQuery(".niceRadio").each(function() {
            changeRadioStart(jQuery(this));
        });
    });

    function changeRadio(el) {
        var el = el,
            input = el.find("input").eq(0);
        var nm = input.attr("name");
        jQuery(".niceRadio input").each(function() {
            if (jQuery(this).attr("name") == nm) {
                jQuery(this).parent().removeClass("radioChecked");
            }
        });
        if (el.attr("class").indexOf("niceRadioDisabled") == -1) {
            el.addClass("radioChecked");
            input.attr("checked", true);
        }
        return true;
    }

    function changeVisualRadio(input) {
        var wrapInput = input.parent();
        var nm = input.attr("name");
        jQuery(".niceRadio input").each(function() {
            if (jQuery(this).attr("name") == nm) {
                jQuery(this).parent().removeClass("radioChecked");
            }
        });
        if (input.attr("checked")) {
            wrapInput.addClass("radioChecked");
        }
    }

    function changeRadioStart(el) {
        try {
            var el = el,
                radioName = el.attr("name"),
                radioId = el.attr("id"),
                radioChecked = el.attr("checked"),
                radioDisabled = el.attr("disabled"),
                radioTab = el.attr("tabindex"),
                radioValue = el.attr("value");
            if (radioChecked) el.after("<span class='niceRadio radioChecked'>" + "<input type='radio'" + "name='" + radioName + "'" + "id='" + radioId + "'" + "checked='" + radioChecked + "'" + "tabindex='" + radioTab + "'" + "value='" + radioValue + "' /></span>");
            else
                el.after("<span class='niceRadio'>" + "<input type='radio'" + "name='" + radioName + "'" + "id='" + radioId + "'" + "tabindex='" + radioTab + "'" + "value='" + radioValue + "' /></span>");
            if (radioDisabled) {
                el.next().addClass("niceRadioDisabled");
                el.next().find("input").eq(0).attr("disabled", "disabled");
            }
            el.next().bind("mousedown", function(e) {
                changeRadio(jQuery(this))
            });
            if (jQuery.browser.msie) el.next().find("input").eq(0).bind("click", function(e) {
                changeVisualRadio(jQuery(this))
            });
            else el.next().find("input").eq(0).bind("change", function(e) {
                changeVisualRadio(jQuery(this))
            });
            el.remove();
        } catch (e) {}
        return true;
    }
    $('#baner').owlCarousel({
        items: 1,
        loop: true,
        margin: 0,
        nav: true,
        autoplay: true,
        autoplayTimeout: 3000,
        autoplayHoverPause: true
    });
    $('#comment').owlCarousel({
        items: 1,
        loop: true,
        margin: 0,
        nav: true,
        autoplay: true,
        autoplayTimeout: 3000,
        autoplayHoverPause: true
    });
    $('#partner').owlCarousel({
        items: 6,
        loop: true,
        margin: 0,
        nav: true,
        autoplay: true,
        autoplayTimeout: 3000,
        autoplayHoverPause: true
    });
    $(".tabs").on("click", ".tabs_nav a", function(e) {
        e.preventDefault();
        $(this).parent().siblings().removeClass("active");
        $(this).parent().addClass("active");
        $(this).parents(".tabs").eq(0).find(".tab_pane").removeClass("active");
        $(this).parents(".tabs").eq(0).find(".tab_pane").eq($(this).parent().index()).addClass("active");
        $('body').attr('class', $(this).attr('rel'));
    });
    $(document).on("click", ".select_bl > form > .item > input", function(e) {
        e.preventDefault();
        $(this).parent().siblings().removeClass("active");
        $(this).parent().toggleClass('active');
        $('body').attr('class', $(this).attr('rel'));
    });
    $(document).on("click", ".parametr", function(e) {
        e.preventDefault();
        $(this).parent().siblings().removeClass("active");
        $(this).parents('.item:first').children('input').focus();
        $(this).parent().toggleClass('active');
        $('body').attr('class', $(this).attr('rel'));
    });
    $("#scroll1").niceScroll({
        cursorcolor: "#fff",
        background: "#5c6b7b",
        cursorborderradius: "2px",
        zindex: "1",
        cursorwidth: "4px",
        touchbehavior: true,
        horizrailenabled: false,
        autohidemode: false
    });
    $("#scroll2").niceScroll({
        cursorcolor: "#fff",
        background: "#5c6b7b",
        cursorborderradius: "2px",
        zindex: "1",
        cursorwidth: "4px",
        touchbehavior: true,
        horizrailenabled: false,
        autohidemode: false
    });
    $("#scroll3").niceScroll({
        cursorcolor: "#fff",
        background: "#5c6b7b",
        cursorborderradius: "2px",
        zindex: "1",
        cursorwidth: "4px",
        touchbehavior: true,
        horizrailenabled: false,
        autohidemode: false
    });
    $("#scroll_tb").niceScroll({
        cursorcolor: "#fff",
        background: "#515f6e",
        cursorborderradius: "2px",
        zindex: "5",
        cursorwidth: "4px",
        touchbehavior: true,
        horizrailenabled: false,
        autohidemode: false
    });
    $("#scroll_tb1").niceScroll({
        cursorcolor: "#fff",
        background: "#515f6e",
        cursorborderradius: "2px",
        zindex: "4",
        cursorwidth: "4px",
        touchbehavior: true,
        horizrailenabled: false,
        autohidemode: false
    });
    $("#scroll_tb2").niceScroll({
        cursorcolor: "#fff",
        background: "#515f6e",
        cursorborderradius: "2px",
        zindex: "3",
        cursorwidth: "4px",
        touchbehavior: true,
        horizrailenabled: false,
        autohidemode: false
    });
    $("#scroll_tb3").niceScroll({
        cursorcolor: "#fff",
        background: "#515f6e",
        cursorborderradius: "2px",
        zindex: "2",
        cursorwidth: "4px",
        touchbehavior: true,
        horizrailenabled: false,
        autohidemode: false
    });
    $("#scroll_tb4").niceScroll({
        cursorcolor: "#fff",
        background: "#515f6e",
        cursorborderradius: "2px",
        zindex: "1",
        cursorwidth: "4px",
        touchbehavior: true,
        horizrailenabled: false,
        autohidemode: false
    });
});
$(document).ready(function() {
    $("#phone").mask("(999) 999-9999");
});
function cityChange(elem) {
    var domain=$(elem).val();
    var path='';
    if(typeof(location.pathname)!='undefined') {
        path=path+location.pathname;
    }
    if(typeof(location.search)!='undefined') {
        var path=path+location.search;
    }
    if(typeof(location.hash)!='undefined') {
        var path=path+location.hash;
    }
    console.log(path);
    if(domain=='www') {
        location="https://ngs-penza.ru"+path;
    } else {
        location="https://"+domain+".ngs-penza.ru"+path;
    }
    
}

$(function () {

    /**
     *разделы товаров: показать ещё/скрыть
     */
    $(".dropdown-top-btn-mob").on("click", function ()
    {
        var btnTitleOld = $(this).find("span").text();
        var btnTitleNew = $(this).data("title");

        $(this).find("span").text(btnTitleNew);
        $(this).data("title", btnTitleOld);

        $(this).parent().find(".submenu.mob").toggleClass("active");
    });

    /**
     * ОТКРЫТИЕ/ЗАКРЫТИЕ РАЗДЕЛОВ КАТАЛОГА НА СТРАНИЦЕ /catalog/
     */
    $(".catalog_block .toggle_catalog").on("click", function () {
        $(".catalog_block .left-menu").slideToggle(300);
    });


});

