jQuery(document).ready(function () {
    if (jQuery("#toggle-filters").length > 0) {
        jQuery("#toggle-filters").click(function () {
            jQuery(".filter-hamburger #sidebar").slideToggle()
        });
    }
    //Added on 26 Dec

    if(jQuery(".warranty-5-years").length>0){
        jQuery(".single.single-product .single-product-main-image").addClass("warranty-five-years");
    }
    if(jQuery(".warranty-10-years").length>0){
        jQuery(".single.single-product .single-product-main-image").addClass("warranty-ten-years");
    }
    if(jQuery(".warranty-lifetime").length>0){
        jQuery(".single.single-product .single-product-main-image").addClass("warranty-life-time");
    }
    //Added on 26 Dec
    if(jQuery("#sidebar .current-cat.closed").length>0){
        jQuery("#sidebar .current-cat.closed").find("i.icon-plus").trigger("click")
    }
    var geolocale=getCookie("geolocale");
    if(geolocale!='' && geolocale=="China" && window.location.search=="?lang=zh-hans"){
        jQuery("span.price,p.price,.woocommerce div.product p.price").attr('style','display:block !important');
    }

    if(jQuery(".current-cat-ancestor").length>0 &&  jQuery(".current-cat-ancestor").find(".children").length>0) {
        jQuery(".current-cat-ancestor").removeClass("closed");
        jQuery(".current-cat-ancestor").addClass("opened")
        jQuery(".current-cat-ancestor").find(".children").show();
    }
    if(jQuery("#toggle-filters").length>0){
        jQuery("#toggle-filters").click(function(){
            jQuery(".filter-hamburger #yith_wc_category_accordion-2").slideToggle()
        });
    }
    if(window.location.pathname=='/room-planner-3d/'){
        jQuery('html, body').animate({
            scrollTop: jQuery("iframe").offset().top
        }, 1);
    }
    if(jQuery(".more").length>0 && jQuery(".less").length>0) {
        jQuery(".less").click(function () {
            jQuery(this).parent().find(".parent-planning-related").toggle();
            jQuery(this).parent().find(".parent-planning-related").toggleClass("opened");
            jQuery(this).parent().toggleClass("opened");
            jQuery(this).hide();
            jQuery('html, body').animate({
                scrollTop: jQuery(this).parent().offset().top
            }, 20);
            jQuery(this).parent().find(".more").show();
        })
        jQuery(".more").click(function () {
            jQuery(this).parent().find(".parent-planning-related").toggle();
            jQuery(this).parent().find(".parent-planning-related").toggleClass("opened");
            jQuery(this).parent().toggleClass("opened");
            jQuery(this).hide();
            jQuery('html, body').animate({
                scrollTop: jQuery(this).parent().offset().top
            }, 400);
            jQuery(this).parent().find(".less").show();
        })
    }
    jQuery(".env_carousel").owlCarousel({
        items : 3,
        itemsDesktop : [1000,3],
        itemsDesktopSmall : [900,3],
        itemsTablet: [600,2],
        autoPlay: 3500,
        itemsMobile : false,
        navigation : true, // Show next and prev buttons
        pagination : true,
        slideSpeed : 300,
        paginationSpeed : 400,


        // navigationText : [
        //     '<svg viewBox="0 0 100 100" class="icon icon-chevron-left"><use xlink:href="#icon-chevron-left"></use></svg>',
        //     '<svg viewBox="0 0 100 100" class="icon icon-chevron-right"><use xlink:href="#icon-chevron-right"></use></svg>'
        // ],
    });
    if(jQuery("#size_variations").length>0){
        jQuery("#size_variations").msDropdown().data("dd");

    }
    if(jQuery("#legs").length>0){
        jQuery("#legs").msDropdown().data("dd");

    }
    if(jQuery("#product_addons").length>0){
        jQuery("#product_addons").msDropdown().data("dd");
    }
    if(jQuery("#cushions").length>0) {
        jQuery("#cushions").msDropdown().data("dd");
    }
    if(jQuery("#languages").length>0) {
        jQuery("#languages").msDropdown().data("dd");
    }
    if(jQuery(".style_variation").length>0) {
        var val = jQuery(".style_variation").val();
        jQuery("ul.color .option a").hide();
        jQuery("ul.color .option a[data-style='" + val + "']").show();
        jQuery(".style_variation").change(function () {
            var val = jQuery(this).val();
            jQuery("ul.color .option a").hide();
            jQuery("ul.color .option a[data-style='" + val + "']").show();
            window.location.href=jQuery("ul.color .option a[data-style='" + val + "']").attr("href");
            // id=jQuery("ul.color .option a.selected[data-style='" + val + "']").attr("data-id");
            // jQuery("button[name='add-to-cart']").val(id);
        })
    }
    if(jQuery(".woocommerce-checkout").length>1) {
        var html=jQuery("#shipping_postcode").parent().parent()[0].outerHTML;
        var html1=jQuery("#billing_postcode").parent().parent()[0].outerHTML;
        jQuery("#billing_postcode").parent().remove();
        jQuery("#shipping_postcode").parent().remove();
        jQuery(html).insertBefore(jQuery("#shipping_city").parent())
        jQuery(html1).insertBefore(jQuery("label[for='billing_city']").parent())
        jQuery("input[name='billing_postcode']").keyup(function(){
            // if(jQuery("#calc_billing_country").val()!="US"){
            //     return;
            // }
            var zipcode = jQuery(this).val().substring(0, 5);
            var clientKey = "V1CDKMMVrPoPvKXPBE5IrCAhTaJtAjIYFlLTbT313R68hNEVQAiZot2APndb91Gz";
            var cache = {};
            if (zipcode.length == 5 && /^[0-9]+$/.test(zipcode))
            {

                var data = { action: 'get_city_address_by_pincode',clientKey:clientKey,zipcode:zipcode };
                jQuery.post(ajaxurl, data, function(response) {
                    response=JSON.parse(response);
                    jQuery("#billing_city").val(response.city);
                    jQuery('#billing_state').val(response.state).trigger('change');
                    jQuery( 'body' ).trigger( 'update_checkout' );
                });
            }

        })
        jQuery("input[name='shipping_postcode']").keyup(function(){
            // if(jQuery("#calc_shipping_country").val()!="US"){
            //     return;
            // }
            var zipcode = jQuery(this).val().substring(0, 5);
            var clientKey = "V1CDKMMVrPoPvKXPBE5IrCAhTaJtAjIYFlLTbT313R68hNEVQAiZot2APndb91Gz";
            var cache = {};
            if (zipcode.length == 5 && /^[0-9]+$/.test(zipcode))
            {

                var data = { action: 'get_city_address_by_pincode',clientKey:clientKey,zipcode:zipcode };
                jQuery.post(ajaxurl, data, function(response) {
                    response=JSON.parse(response);
                    jQuery("#shipping_city").val(response.city);
                    jQuery('#shipping_state').val(response.state).trigger('change');
                    jQuery( 'body' ).trigger( 'update_checkout' );
                });
            }

        })
        if(jQuery(".showlogin").length>0){
            jQuery(".showlogin").parent().hide()
        }
        if(jQuery(".woocommerce-account-fields").length>0){
            jQuery(".woocommerce-account-fields").hide();
        }
        jQuery("#place_order").click(function(){
            setTimeout(function(){
                window.scrollTo(0,0);
            },300);
        })
    }
    /*
   Auto Fill state in Cart Page
    */
    if(jQuery(".woocommerce-cart").length>0){
        jQuery("button[name='calc_shipping']").hide();
        if(jQuery("#calc_shipping_postcode").length<=0){
            return;
        }
        // var html1=jQuery("#calc_shipping_postcode").parent()[0].outerHTML;
        // jQuery("#calc_shipping_postcode").parent().remove();
        // jQuery(html1).insertBefore(jQuery("#calc_shipping_country_field"));
        // jQuery("#calc_shipping_country_field").hide();
        // jQuery("#calc_shipping_country").val("US").trigger('change');
        jQuery("input[name='calc_shipping_postcode']").on("keyup",function(){
            // if(jQuery("#calc_shipping_country").val()!="US"){
            //     return;
            // }
            var zipcode = jQuery(this).val().substring(0, 5);
            var clientKey = "V1CDKMMVrPoPvKXPBE5IrCAhTaJtAjIYFlLTbT313R68hNEVQAiZot2APndb91Gz";
            var cache = {};
            if (zipcode.length == 5 && /^[0-9]+$/.test(zipcode))
            {

                var data = { action: 'get_city_address_by_pincode',clientKey:clientKey,zipcode:zipcode };
                jQuery.post(ajaxurl, data, function(response) {
                    response=JSON.parse(response);
                    jQuery("#calc_shipping_city").val(response.city);
                    jQuery('#calc_shipping_state').val(response.state).trigger('change');
                    jQuery(".woocommerce-shipping-calculator").submit();
                });
            }

        })
        jQuery( document ).ajaxComplete(function() {
            shippingCalculatorCart();
        });
    }

function shippingCalculatorCart(){
    // jQuery("button[name='calc_shipping']").hide();
    // var html1=jQuery("#calc_shipping_postcode").parent()[0].outerHTML;
    // jQuery("#calc_shipping_postcode").parent().remove();
    // jQuery(html1).insertBefore(jQuery("#calc_shipping_country_field"));
    // jQuery("#calc_shipping_country_field").hide();
    // jQuery("#calc_shipping_country").val("US").trigger('change');
    jQuery("input[name='calc_shipping_postcode']").on("keyup",function(){
        // if(jQuery("#calc_shipping_country").val()!="US"){
        //     return;
        // }
        var zipcode = jQuery(this).val().substring(0, 5);
        var clientKey = "V1CDKMMVrPoPvKXPBE5IrCAhTaJtAjIYFlLTbT313R68hNEVQAiZot2APndb91Gz";
        var cache = {};
        if (zipcode.length == 5 && /^[0-9]+$/.test(zipcode))
        {

            var data = { action: 'get_city_address_by_pincode',clientKey:clientKey,zipcode:zipcode };
            jQuery.post(ajaxurl, data, function(response) {
                response=JSON.parse(response);
                jQuery("#calc_shipping_city").val(response.city);
                jQuery('#calc_shipping_state').val(response.state).trigger('change');
                jQuery(".woocommerce-shipping-calculator").submit();
            });
        }

    })
}
    if(jQuery(".woocommerce-checkout").length>1){
        jQuery("#billing_first_name").attr("placeholder","First Name");
        jQuery("#billing_last_name").attr("placeholder","Last Name");
        jQuery("#billing_company").attr("placeholder","Company");
        jQuery("#billing_city").attr("placeholder","City");
        jQuery("#billing_phone").attr("placeholder","Phone");
        jQuery("#shipping_first_name").attr("placeholder","First Name");
        jQuery("#shipping_last_name").attr("placeholder","Last Name");
        jQuery("#shipping_company").attr("placeholder","Company");
        jQuery("#shipping_city").attr("placeholder","City");
        jQuery("#shipping_phone").attr("placeholder","Phone");
        jQuery("#username").attr("placeholder","Username");
        jQuery("#password").attr("placeholder","Password");
        jQuery("#billing_email").attr("placeholder","Email Address");
        jQuery("#billing_state").attr("data-placeholder","State");
        jQuery("#shipping_postcode").attr("placeholder","Shipping Postcode");
        jQuery("#billing_postcode").attr("placeholder","Billing Postcode");
        jQuery("#shipping_email").attr("placeholder","Email Address");
        jQuery("#shipping_state").attr("data-placeholder","State");
        jQuery("#shipping_address_1").attr("placeholder","Address Line 1");
        jQuery("#shipping_address_2").attr("placeholder","Address Line 2");
        // jQuery("#billing_country_field").hide();
        // jQuery("#shipping_country_field").hide();
        // if(jQuery("#order_comments_field").length>1 && jQuery(".woocommerce-account-fields").length>1 ){
        //     var html=jQuery("#order_comments_field").html();
        //     jQuery("#order_comments_field").remove();
        //     jQuery('<p class="form-row form-row-wide">'+html+'</p>').insertAfter("#shipping_postcode");
        // }
        // if(jQuery("#order_comments") && jQuery(".woocommerce-shipping-fields")){
        //     var html=jQuery("#order_comments").parent().html();
        //     jQuery("#order_comments").remove();
        //     jQuery('<p class="form-row form-row-wide">'+html+'</p>').insertAfter("#shipping_postcode");
        // }


    }
/*
Mega menu English
 */
jQuery(".cat-item").hover(function(){
    var href=jQuery(this).find("a").attr("href");
    if(jQuery("#loading-image").length==0) {
        jQuery("#mega-menu-item-media_image-5, #mega-menu-item-media_image-7").append("<img src='/wp-content/themes/themify-simple-child/assets/images/loading.gif' id='loading-image'>")
    }
    var src=jQuery("a.menu-thumbnail[href='"+href+"']").find("img").attr("src");
    jQuery("#mega-menu-item-media_image-5").find("img").attr("srcset","");
    jQuery("#mega-menu-item-media_image-7").find("img").attr("srcset","");
    jQuery("#mega-menu-item-media_image-5 img").first().hide().attr("src",src);
    jQuery("#mega-menu-item-media_image-7 img").first().hide().attr("src",src);
    jQuery("#mega-menu-item-media_image-5 img").first().attr("onclick", "location.href='"+href+"'");
    jQuery("#mega-menu-item-media_image-7 img").first().attr("onclick", "location.href='"+href+"'");
    setTimeout(function () {
        jQuery("#mega-menu-item-media_image-5 img").first().show();
        jQuery("#mega-menu-item-media_image-7 img").first().show();
        jQuery("#loading-image").remove();

    },1000)
});

    /*
    Mega Menu Chinese
     */
    jQuery(".cat-item").hover(function(){
        var href=jQuery(this).find("a").attr("href");
        if(jQuery("#loading-image").length==0) {
            jQuery("#mega-menu-item-media_image-6").append("<img src='/wp-content/themes/themify-simple-child/assets/images/loading.gif' id='loading-image'>")
        }
        var src=jQuery("a.menu-thumbnail[href='"+href+"']").find("img").attr("src");
        jQuery("#mega-menu-item-media_image-6").find("img").attr("srcset","");
        jQuery("#mega-menu-item-media_image-6 img").first().hide().attr("src",src);
        jQuery("#mega-menu-item-media_image-6 img").first().attr("onclick", "location.href='"+href+"'");
        setTimeout(function () {
            jQuery("#mega-menu-item-media_image-6 img").first().show();
            jQuery("#loading-image").remove();

        },1000)
    })
    /*
    Mega menu end
     */
    if(jQuery("button.stock_alert_button.alert_button_hover").length>0){
        jQuery("button.stock_alert_button.alert_button_hover").html("Get Notification");
    }

    if(jQuery(".woof.woof_sid.woof_sid_widget").length>0){
        var url=window.location.href;
       var index_price= url.indexOf("min_price");
       var index_age= url.indexOf("swoof=1");
       if(index_price<=0 && index_age<=0){
           jQuery(".woof.woof_sid.woof_sid_widget").hide();
       }
    }

    jQuery(".woocommerce-page #sidebar .widget-woof .widgettitle").click(function(){
        jQuery(".woof.woof_sid.woof_sid_widget").toggle();
    });
    jQuery("#carousel").owlCarousel({
        navigation : true, // Show next and prev buttons
        pagination : true,
        slideSpeed : 300,
        paginationSpeed : 400,
        singleItem : true,
        autoPlay   : 7000,

        // navigationText : [
        //     '<svg viewBox="0 0 100 100" class="icon icon-chevron-left"><use xlink:href="#icon-chevron-left"></use></svg>',
        //     '<svg viewBox="0 0 100 100" class="icon icon-chevron-right"><use xlink:href="#icon-chevron-right"></use></svg>'
        // ],
    });
    runCloudzoom();
    // jQuery("ol.flex-control-nav.flex-control-thumbs li img").click(function () {
    //     setTimeout(function(){
    //         runCloudzoom();
    //     },300);
    // })

    //added on 26 Dec
    jQuery(".slick-slide .thumb img").click(function () {
        var ind=jQuery(this).parent().parent().index();
        if(ind>0){
            jQuery(".single.single-product .single-product-main-image").addClass("warranty-hidden");
        }else {
            jQuery(".single.single-product .single-product-main-image").removeClass("warranty-hidden");
        }
        setTimeout(function(){
            runCloudzoom();
        },300);
    })

    //Added on26 Dec
    if(jQuery(".content-copy").length>0){
        var html=jQuery(".content-copy").outerHtml;
        var str=jQuery(".content-copy").html();
        jQuery(".content-copy").remove();
        // jQuery(html).insertAfter(jQuery(".upc_sku"));


        str=str.substring(0,150)+'<span class="copy-dots">...</span><span class="hidden-copy">'+str.substring(150,str.length)+'</span><span class="copy-toggle"><a href="javascript:void(0);" class="read-more">Read More</a><a href="javascript:void(0);" class="read-less"> Read Less</a></span>';
        jQuery("<div class='content-copy'></div>").insertAfter(jQuery(".bullets"));
        jQuery(".content-copy").html(str);

        jQuery(".hidden-copy").hide();
        jQuery(".copy-toggle a").click(function(){
            jQuery(this).parent().parent().find(".hidden-copy").toggle();
            jQuery(this).parent().parent().find(".copy-dots").toggle();
            jQuery(this).parent().toggleClass("opened");
        })
    }

    /*
    Instagram Gallery on home page
     */

//         var html=jQuery("#instagallery-2").html();
//         jQuery("#instagallery-1").append(html);
//         jQuery("#instagallery-2").remove();
//
//         setTimeout(function () {
//             jQuery("div#instagallery-1").owlCarousel({
// //Basic Speeds
//                 slideSpeed : 200,
//                 paginationSpeed : 800,
//
//                 //Autoplay
//                 autoPlay : true,
//                 goToFirst : true,
//                 goToFirstSpeed : 1000,
//
//                 // Navigation
//                 navigation : true,
//                 navigationText : ['<svg viewBox="0 0 100 100" class="icon icon-chevron-left"><use xlink:href="#icon-chevron-left"></use></svg>','<svg viewBox="0 0 100 100" class="icon icon-chevron-right"><use xlink:href="#icon-chevron-right"></use></svg>'],
//                 pagination : true,
//                 paginationNumbers: false,
//
//                 // Responsive
//                 responsive: true,
//                 items : 4,
//                 itemsDesktop : [1199,4],
//                 itemsDesktopSmall : [980,3],
//                 itemsTablet: [768,2],
//                 itemsMobile : [479,1]
//             });
//         },300);

    jQuery(".footer-widgets .widgettitle").click(function(){
        jQuery(this).parent().toggleClass("open");//.find("div")
    })
    //Add chinese in the transposh widget

    // jQuery(document).ready(function () {
        // var val=   jQuery("select.tp_lang2 option:last").val();
        // val=val.replace("?lang=zh","?lang=zh-hans") ;
        // jQuery("select.tp_lang2 option:last").val(val);
    // })
    // jQuery(window).load(function () {
    //
    //     if(window.location.href.indexOf("?lang=zh-hans")>10){
    //         var val=   jQuery("option[value$='/?lang=zh-hans']").val(window.location.href);
    //         jQuery("select.tp_lang2 option:first").removeAttr("selected");
    //         jQuery("select.tp_lang2 option:last").attr("selected","selected");
    //         jQuery('.select2-container').remove();
    //
    //         jQuery('.tp_lang2').select2({
    //             allowClear: true
    //         });
    //
    //     }
    // })
})
function getSelectedValue() {
    return jQuery(".tp_lang2 option[selected='selected']").attr("value");
}
/*function runCloudzoom(){
    jQuery(".woocommerce-product-gallery__wrapper div.woocommerce-product-gallery__image.flex-active-slide a img:hidden").removeAttr('data-cloudzoom');
    jQuery(".woocommerce-product-gallery__wrapper div.woocommerce-product-gallery__image.flex-active-slide a img:visible").removeAttr('data-cloudzoom');
//myinstance= jQuery(".slick-slide.slick-current.slick-active img:visible").data('CloudZoom');
// myinstance.destroy();
    var wwidth= jQuery(window).innerWidth();
    if(wwidth<768) {
        return;
    }
    if(jQuery(".woocommerce-product-gallery__wrapper div.woocommerce-product-gallery__image.flex-active-slide a img:visible").length<=0){
        return;
    }
    var imgpath=jQuery(".woocommerce-product-gallery__wrapper div.woocommerce-product-gallery__image.flex-active-slide a img:visible").attr("src");
    if(imgpath.indexOf("-600x600")){
        imgpath=imgpath.replace("-600x600","");
    }
    var attr=jQuery(".woocommerce-product-gallery__wrapper div.woocommerce-product-gallery__image.flex-active-slide a img:visible").attr('data-cloudzoom');
    if (typeof attr !== typeof undefined && attr !== false) {
    }else{
        jQuery(".woocommerce-product-gallery__wrapper div.woocommerce-product-gallery__image.flex-active-slide a img:visible").attr("data-cloudzoom", "zoomImage:'" + imgpath + "'");
//myInstance = jQuery('.slick-slide.slick-current.slick-active img').data('CloudZoom');
//myInstance.destroy();
        jQuery(".woocommerce-product-gallery__wrapper div.woocommerce-product-gallery__image.flex-active-slide a img:visible").CloudZoom({
            zoomSizeMode: 'image',
            disableOnScreenWidth: 768,
            zoomWidth: 200,
            zoomHeight: 200
        });
    }

}*/
function runCloudzoom(){

    jQuery("div.woocommerce-product-gallery__image a img:hidden").removeAttr('data-cloudzoom');
    jQuery("div.woocommerce-product-gallery__image a img:visible").removeAttr('data-cloudzoom');
//myinstance= jQuery(".slick-slide.slick-current.slick-active img:visible").data('CloudZoom');
// myinstance.destroy();
    var wwidth= jQuery(window).innerWidth();
    if(wwidth<768) {
        return;
    }
    if(jQuery("div.woocommerce-product-gallery__image a img:visible").length<=0){
        return;
    }
    var imgpath=jQuery("div.woocommerce-product-gallery__image a img:visible").attr("src");
    // alert(imgpath);
    if(imgpath.indexOf("-600x600")){
        imgpath=imgpath.replace("-600x600","");
    }
    var attr=jQuery("div.woocommerce-product-gallery__image a img:visible").attr('data-cloudzoom');
    if (typeof attr !== typeof undefined && attr !== false) {
    }else{
        jQuery("div.woocommerce-product-gallery__image a img:visible").attr("data-cloudzoom", "zoomImage:'" + imgpath + "'");
//myInstance = jQuery('.slick-slide.slick-current.slick-active img').data('CloudZoom');
//myInstance.destroy();
        jQuery("div.woocommerce-product-gallery__image a img:visible").CloudZoom({
            zoomSizeMode: 'image',
            disableOnScreenWidth: 768,
            zoomWidth: 200,
            zoomHeight: 200
        });
    }

}
var position = jQuery(window).scrollTop();

// should start at 0

var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
    var currentScrollPos = window.pageYOffset;
    if (prevScrollpos > currentScrollPos || currentScrollPos<=150) {
        document.getElementById("headerwrap").style.top = "0px";
    } else {
        document.getElementById("headerwrap").style.top = "-150px";
    }
    prevScrollpos = currentScrollPos;
}