// source --> https://verdeagua.shop/wp-content/themes/fashion-divi-ecommerce-theme/js/functions.js?ver=2.8.5 
jQuery(function($) {

	$(window).on('load', function() { // makes sure the whole site is loaded 
        $('#status').fadeOut(); // will first fade out the loading animation 
        $('#preloader').delay(350).fadeOut('slow', function(){
        	$('#page-container').animate({ opacity:1 });
        }); // will fade out the white DIV that covers the website. 
        
    });

	//Fallback loader
    setTimeout(function() {
		$('#status').fadeOut(); // will first fade out the loading animation 
        $('#preloader').delay(350).fadeOut('slow', function(){
        	$('#page-container').animate({ opacity:1 });
        }); // will fade out the white DIV that covers the website. 
    }, 2500);

	//wrapping post meta
	$.each($('.et_pb_post'), function(i, post) {
		var $post = $(post);
		$post.find('.entry-title, .post-meta, .post-content').wrapAll('<div class="post-meta-wrapper" />');
		var $tags = $post.find('a[rel="tag"]');
		$tags.wrapAll('<div class="post-categories-wrapper" />');
		$post.find('.post-meta-wrapper').prepend($post.find('.post-categories-wrapper'));

		//delete separator
		if ($tags.length > 1) {
			var $meta = $post.find('.post-meta');
			$meta.html($meta.html().replace(/(,\s)+$/g, ''));
		}

		/*$(this).click(function() {
			window.location.href = $(this).find('a').attr('href');
		});*/
	});

	//search form
	$('button.is-search-submit').click(function(evt) {
		var $button = $(this),
			$form = $button.parents('form.is-search-form'),
			$input = $form.find('input.is-search-input');
		if (!$form.hasClass('search-form-open')) {
			evt.preventDefault();
			$form.addClass('search-form-open');
			$input.focus();
		}
	});

	if ($('body.single-product').length) {
		build_sheet_scroll($);

		$(window).on('resize', function() {
	        build_sheet_scroll($);
		});
		
		if ( ! ecommerce_ajax_obj.version_2 ) {
			$('#sheet-row form.variations_form').attrchange({
				trackValues: true,
				callback: function(event) {
					if (event.attributeName == 'current-image' ) {
						var $img = $('img[data-img="' + event.newValue + '"]');
						if ($img.length) {
							$([document.documentElement, document.body]).animate({
								scrollTop: $img.offset().top
							}, 1000);
						}
					}
				}
			});

			$('#mobile-product-sheet form.variations_form').attrchange({
				trackValues: true,
				callback: function(event) {
					if (event.attributeName == 'current-image') {
						var $img = $('img[data-img="' + event.newValue + '"]');
						if ($img.length) {
							$([document.documentElement, document.body]).animate({
								scrollTop: $img.offset().top
							}, 1000);
						}
					}
				}
			});
		}
	}

	//Stick to top menu
	var $main_menu = $('#top-main-menu');
	if ( $main_menu.length && $main_menu.offset())
		$main_menu.attr('data-offset', $main_menu.offset().top);
	
	var $filters = $('#filters-landscape');
	if ($filters.length > 0) {
		$filters.attr('data-offset', $filters.offset().top);
	}

	$(window).scroll(function() {
		stick_menu($);
		stick_filters($);
	});

	check_cart_items($);
});

function stick_menu($) {
	var top = $(window).scrollTop(),
		$main_menu = $('#top-main-menu'),
		offset = $main_menu.attr('data-offset');
	if (top > offset) {
		$('body').addClass('with-sticky-menu');
		$main_menu.addClass('menu-sticky');
	} else {
		$('body').removeClass('with-sticky-menu');
		$main_menu.removeClass('menu-sticky');
	}
}

function stick_filters($) {
	var $filters = $('#filters-landscape');
	if ($filters.length > 0) {
		var top = $(window).scrollTop(),
			$main_menu = $('#top-main-menu'),
			offset = $filters.attr('data-offset'),
			$admin = $('#wpadminbar'), 
			extra = 0;

		if ( $admin.length > 0 ) {
			extra = $admin.outerHeight();
		}

		if (top > Number(offset) - $main_menu.outerHeight()) {
			$filters.addClass('filter-sticky');
			$filters.css('top', $main_menu.outerHeight() + extra - 1 );
		} else {
			$filters.removeClass('filter-sticky');
			$filters.css('top', 0 );
		}
	}
}

function check_cart_items($) {
	$.ajax({
      url : window.ecommerce_ajax_obj.ajaxurl,
      type : 'POST',
      data : { 
        action : 'ecommerce_cart_items',
        security : window.ecommerce_ajax_obj.security_nonce
      },
      success : function(response) {
      	if (response && response.count && response.count > 0) {
      		$('#cart-icon a').append('<span id="ecommerce-cart-count" class="ecommerce-cart-with-items">' + response.count + '</span>');
      		
      	}
      }
    });
}

function build_sheet_scroll($) {
	var $sheet_cont = $('#sheet-row .et_pb_column_1_tb_body'),
		$mobile_sheet = $('#mobile-product-sheet');

	if ($sheet_cont.length <= 0 || $mobile_sheet.length <= 0)
		return;
	
		$sheet_cont.removeClass('sticky sticky-bottom');
	var init = $sheet_cont.offset().top,
		init_mobile = $mobile_sheet.offset().top;
			
		$sheet_cont.attr('data-init-offset', init);
		$mobile_sheet.attr('data-init-offset', init_mobile);
		update_sheet_scroll($);
		$(window).off('scroll', create_scroll).on('scroll', create_scroll);
}

function create_scroll() {
	stick_menu(jQuery);
	update_sheet_scroll(jQuery);
}


function update_sheet_scroll($) {
	var top = $(window).scrollTop(),
		$sheet_cont = $('#sheet-row .et_pb_column_1_tb_body'),
		$mobile_sheet = $('#mobile-product-sheet'),
		init = $('#top-main-menu').length ? ($sheet_cont.attr('data-init-offset') - $('#top-main-menu').height()) : 100,
		$images_cont = $('#sheet-row .et_pb_column_0_tb_body'),
		max_scroll = $images_cont.length ? ($images_cont.offset().top + $images_cont.innerHeight() - $sheet_cont.outerHeight() - 100) : 999999,
		max_scroll_mobile = $('.related.products').length ? ($('.related.products').offset().top - $(window).height()) : 999999;

	if ( !$('.woocommerce-product-gallery').length ) {
		if (top < init) {
			$sheet_cont.removeClass('sticky sticky-bottom');
		} else if (top > init && top < max_scroll) { //Falta restar el tamaño de la pantalla
			$sheet_cont.removeClass('sticky-bottom');
			$sheet_cont.addClass('sticky');
		} else if (top >= max_scroll) {
			$sheet_cont.addClass('sticky-bottom');
		}
	}

	$mobile_sheet.addClass('sticky-bottom');
	if (top > max_scroll_mobile) {
		$mobile_sheet.hide();
	} else {
		$mobile_sheet.show();
	}
}

jQuery(document).ready(function($){
	$(window).load(function() {
		
		setTimeout(function() {
           	$('.menu-item-has-children').off('touchend');   
           	$('.menu-item-has-children').off('touchforcechange');
        }, 250);

	  	setTimeout(function() {
			jQuery( document ).on( 'wpf_ajax_success', function() {
	       		jQuery(document).trigger('yith_wcwl_init');
        	});
			var top_level_link = '.et_mobile_menu .menu-item-has-children > a';
			$('.et_mobile_menu a').each(function() {
				$(this).off('click');
				if ( $(this).is(top_level_link) ) {
					$(this).attr('href', '#');
				}

				if ( ! $(this).siblings('.sub-menu').length ) {
					$(this).on('click', function(event) {
						$(this).parents('.mobile_nav').trigger('click');
					});
				} else {
					$(this).on('click', function(evt) {
						evt.preventDefault();

						var $cont = $(this).parent(),
							$submenu = $cont.find('.sub-menu').eq(0),
							items = $cont.find('li'),
							itemHeight = 47;

						if ($submenu.parents('.sub-menu').length > 1)
							return;

						if ($submenu.hasClass('menu-open')) {
							$submenu.removeClass('menu-open');
							$submenu.animate({ 'height' : 0 }, 650);
						} else {
							var $open_menus = $('.et_mobile_menu > li > .sub-menu.menu-open');
							$open_menus.removeClass('menu-open');
							$open_menus.animate({ 'height' : 0 }, 650);
							$submenu.addClass('menu-open');
							$submenu.animate({ 'height' : (itemHeight * items.length) + 'px' }, 650);
						}

					});
				}
			});
	  	}, 700);
	});
});
// source --> https://verdeagua.shop/wp-content/themes/fashion-divi-ecommerce-theme/js/new-functions.js?ver=2.8.5 
jQuery(function($) {
    $(window).on('load', function() {
        topSearch();
      });

    $(window).resize(function(){
        topSearch();
    });

      function topSearch(){
        if($(this).width() < 980){
             if($("body > form.is-search-form").length){
                var searchHeight = $("body > form.is-search-form").height();
                $("#top-header").css('transform', 'translateY('+searchHeight+'px)');
                $("#main-header").css('transform', 'translateY('+searchHeight+'px)'); 
                $("body").addClass('hasSearch');
                }
            }
        else{
             $("#top-header").css('transform', 'translateY(0px)');
             $("#main-header").css('transform', 'translateY(0px)'); 
             $("body").removeClass('hasSearch');
        }
    };
});