$(document).ready(function () {

    $(window).scroll(function () {
        var scroll = $(window).scrollTop();
        //>=, not <=
        if (scroll >= 30) {
            $(".sH").addClass("dH");
            console.log("AA");
        } else {
            $(".sH").removeClass("dH");
        }
    }); //missing ); 

    $('.yHead').css('margin-top', $('.sH').height());
    $('.yHead3').css('margin-top', $('.sH').height());

    window.addEventListener("resize", function (event) {
        $('.yHead').css('margin-top', $('.sH').height());
        $('.yHead3').css('margin-top', $('.sH').height());
        try {
            $('#cartModel').css('margin-top', $('.sH').height());
        } catch (er) {}
    });
    
        $(document).on("click", ".mobHead", function (event) {
        $('body').toggleClass('mobShow');
    });


    //rmvItem
    $(document).on("click", ".mobTailWr", function (event) {
        var buttnElem = $(event.target).hasClass("mobTailWr");
        console.log("index of selected li" + buttnElem);
        if (buttnElem != null && buttnElem != undefined && buttnElem) {    
            $('body').removeClass('mobShow');
        }
    });


    $('#cartModel').on('show.bs.modal', function (e) {
        $('#cartModel').css('margin-top', $('.sH').height());
    })

    $(".oWlh").owlCarousel({
        items: 4,
        loop: true,
        margin: 1,
        autoWidth: true,
        autoplay: false,
        autoplayTimeout: 4500,
        autoplayHoverPause: false,
        animateOut: 'fadeOut',
        dots: false,
        nav: true,
        navText: ["<i class='icN icN-Big-Arrow-Left'></i>", "<i class='icN icN-Big-Arrow-Right'></i>"],
        onChanged: owlHcallback
    });

    function owlHcallback(event) {
        var maxCount = event.item.count;
        var currentItem = (event.item.index - 2) % maxCount;
        if (currentItem <= 0 || currentItem > maxCount) {
            currentItem = 0;
        }
        setTimeout(function () {
            if (currentItem != "NaN" && currentItem != 0) {
                forTabSelect(currentItem);
            }
        }, 800);

    }

    function forTabSelect(i) {
        try {
            $("#nav-tab1>a:nth-child(" + (i) + ")").tab('show');
        } catch (er) {

        }
    }
    $('.cTab1  a[data-toggle="tab"]').on('click', function (e) {
        e.target // newly activated tab
        e.relatedTarget // previous active tab

        $('.oWlh').trigger('to.owl.carousel', $(e.target).parent().index())
    });


    $(".oWlcSli").owlCarousel({
        items: 3,
        loop: false,
        margin: 10,
        autoWidth: true,
        autoplay: false,
        autoplayTimeout: 4500,
        autoplayHoverPause: false,
        animateOut: 'fadeOut',
        dots: false,
        nav: true,
        navText: ["<i class='icN icN-Arrow-Left'></i>", "<i class='icN icN-Arrow-Right'></i>"]
    });
    $(".oWlT").owlCarousel({
        items: 1,
        loop: false,
        margin: 1,
        autoWidth: false,
        autoplay: true,
        autoplayTimeout: 1500,
        autoplayHoverPause: false,
        animateOut: 'fadeOut',
        dots: false,
        nav: false
    });

    $(".oWlPack").owlCarousel({
        items: 3,
        loop: false,
        margin: 5,
        autoWidth: false,
        autoHeight: true,
        autoplay: false,
        dots: false,
        nav: true,
        navText: ["<i class='icN icN-Arrow-Left'></i>", "<i class='icN icN-Arrow-Right'></i>"]
    });


    $(document).on("click", ".pP_i2_Mrk .icN-Wish-Outline", function () {
        console.log("fav clicked");
    });

});


function isOnScreen(elem) {
    // if the element doesn't exist, abort
    if (elem.length == 0) {
        return;
    }
    var $window = jQuery(window)
    var viewport_top = $window.scrollTop()
    var viewport_height = $window.height()
    var viewport_bottom = viewport_top + viewport_height
    var $elem = jQuery(elem)
    var top = $elem.offset().top
    var height = $elem.height()
    var bottom = top + height

    return (top >= viewport_top && top < viewport_bottom) ||
        (bottom > viewport_top && bottom <= viewport_bottom) ||
        (height > viewport_height && top <= viewport_top && bottom >= viewport_bottom)
}


jQuery(document).ready(function () {
    window.addEventListener('scroll', function (e) {
        if (isOnScreen(jQuery('.cNt'))) {
            /* Pass element id/class you want to check */
            console.log('The specified container is in view.' + $(this).parent().text());
        }
    });

    $('#lSel').on('changed.bs.select', function (e, clickedIndex, isSelected, previousValue) {
        $(".daT_Chip.lSel>li:nth-child(" + ($('#lSel').parent().parent().parent().children().length) + ")").before('<li class="daT_ChipL"> <h6  class="f_Bme s_1518" value="' + $('#lSel option').eq(clickedIndex).val() + '">' + $('#lSel option').eq(clickedIndex).text() + '<span class="delete">&times;</span></h6> </li>');
        $('#lSel').selectpicker('toggle');
    });



    $('#lLan').on('changed.bs.select', function (e, clickedIndex, isSelected, previousValue) {
        $(".daT_Chip.lLan>li:nth-child(" + ($('#lLan').parent().parent().parent().children().length) + ")").before('<li class="daT_ChipL"> <h6  class="f_Bme s_1518" value="' + $('#lLan option').eq(clickedIndex).val() + '">' + $('#lLan option').eq(clickedIndex).text() + '<span class="delete">&times;</span></h6> </li>');
        $('#lLan').selectpicker('toggle');
    });

    $('#lNtags').on('changed.bs.select', function (e, clickedIndex, isSelected, previousValue) {
        $(".daT_Chip.lNtags>li:nth-child(" + ($('#lNtags').parent().parent().parent().children().length) + ")").before('<li class="daT_ChipL"> <h6  class="f_Bme s_1518" value="' + $('#lNtags option').eq(clickedIndex).val() + '">' + $('#lNtags option').eq(clickedIndex).text() + '<span class="delete">&times;</span></h6> </li>');
        $('#lNtags').selectpicker('toggle');
    });


    $('#lNtags').on('loaded.bs.select', function (e, clickedIndex, isSelected, previousValue) {
        $('.withBut .bs-searchbox').append('<div class="cmnBlk inPW_4"></div><span class="inpB inpAut inpB_Positive fl_R crtTags"><input class="" type="button" value="Add Tag"></span>')
    });



    $(document).on("click", ".withBut .crtTags", function (event) {
        var txtValue = $('.withBut .bs-searchbox input.form-control').val();
        $('.withBut .bs-searchbox input.form-control').val('');
        console.log(txtValue);
        $('.withBut>select').append('<option selected="" value="' + txtValue + '">' + txtValue + '</option>');
        $('.withBut>select').selectpicker('refresh');
        $(".daT_Chip.lNtags>li:nth-child(" + ($('#lNtags').parent().parent().parent().children().length) + ")").before('<li class="daT_ChipL"> <h6  class="f_Bme s_1518" value="' + txtValue + '">' + txtValue + '<span class="delete">&times;</span></h6> </li>');
        $('#lNtags').selectpicker('toggle');

    });


    $(document).on("click", ".daT_Chip .delete", function () {
        console.log("val " + $(this).parent().attr("value"));
        $(this).parent().parent().parent().find('.selectpicker option[value = ' + $(this).parent().attr("value") + ']').prop("selected", false);
        $(this).parent().parent().remove();

    });


    $(document).on("click", ".upLod", function (e) {
        $(this).parent().find(".upLodIn").click();
    });





    $(document).on("click", ".upLodOut .delete", function () {
        var aft = $(this).parent().find(".upLodIn");
        //  aft.remove();
        aft.val('');
        $(this).parent().remove();
    });


    $(document).on("change", ".upLodIn:file", function () {
        var file = this.files[0];
        var reader = new FileReader();
        var tFil = $(this).parent(); //.find(".daTb_FileW");
        reader.onload = function (e) {
            console.log(file.name);
            tFil.find("h6").remove();
            tFil.append('<h6  class="cmnFlx upLodOut f_Bme s_1518 clr_Cp_Gray7 cmnP_a" ><span class="fl_C upLodOut1">' + file.name + '</span><span class="delete fl_C">&times;</span></h6>');
        }
        reader.readAsDataURL(file);
    });



    function initDatePicker() {



        try {
            $('.datepicker').datepicker({
                clearBtn: true,
                autoclose: true,
                format: "dd-mm-yyyy"
            });
        } catch (er) {}


        try {
            $('.datepickerN').datepicker({
                clearBtn: true,
                autoclose: true,
                format: "dd-mm-yyyy",
                startDate :new Date()
            });

            /*$('.datepickerN').datepicker('update', new Date());
            */
        } catch (er) {}


        try {

            $('.datetimepicker').datetimepicker({
                minDate: new Date(),
                format: 'DD-MM-YYYY',
                icons: {
                    time: 'fa fa-clock-o',
                    date: 'fa fa-calendar',
                    up: 'fa fa-chevron-up',
                    down: 'fa fa-chevron-down',
                    previous: 'fa fa-chevron-left',
                    next: 'fa fa-chevron-right',
                    today: 'fa fa-check',
                    clear: 'fa fa-trash',
                    close: 'fa fa-times'
                }
            });

        } catch (er) {


        }
        try {
            /*$('.timepicker').datepicker({
                clearBtn: true,
                autoclose: true,
                format: "LT"
            });
            */

            $('.timepicker').datetimepicker({
                format: 'LT',
                icons: {
                    time: 'fa fa-clock-o',
                    date: 'fa fa-calendar',
                    up: 'fa fa-chevron-up',
                    down: 'fa fa-chevron-down',
                    previous: 'fa fa-chevron-left',
                    next: 'fa fa-chevron-right',
                    today: 'fa fa-check',
                    clear: 'fa fa-trash',
                    close: 'fa fa-times'
                }
            });

        } catch (er) {


        }


        try {

            $('.input-daterange').datepicker({
                autoclose: true,
                format: "dd-mm-yyyy"
            });
        } catch (er) {}
    }

    initDatePicker();


    $('body').on('click', function (e) {
        $('[data-toggle=popover]').each(function () {
            // hide any open popovers when the anywhere else in the body is clicked
            if (!$(this).is(e.target) && $(this).has(e.target).length === 0 && ($('.prCrng-3').has(e.target).length === 0 || $('.autM_3').has(e.target).length === 0)) {
                setTimeout(function () {}, 200);
                if ($('#prCrng-1').hasClass('show')) {
                    $('#prCrng-1').collapse('hide');
                }
                if ($('#autM_3').hasClass('show')) {
                    $('#autM_3').collapse('hide');
                }
            }
        });
    });

});
