jQuery(document).ready(function() {
    'use strict';
    /*jQuery('.products .product').each(function(){
        var self = jQuery(this);
        self.find('.cherry-quick-view.btn, .yith-wcwl-add-to-wishlist a, .compare, a.btn').wrapInner('<span class="feedback" />');
        var prodCopy = self.children().clone();
        jQuery(self).append('<div class="product_hidden-content"></div>');
        jQuery('.product_hidden-content', self).append(prodCopy);
    });*/

    jQuery('.shop-sidebar .product-categories .cat-parent > a').on('click', function(event) {
        event.preventDefault();
        var self = jQuery(this);
        self.toggleClass('opened');
    });

    jQuery(document).ajaxSuccess(function() {
        jQuery('input[type="number"]').spinner();
    });

    jQuery('input[type="number"]').spinner();

    jQuery(".woocommerce-ordering select").transformSelect({dropDownClass: "transformSelect wooSelect"});

    jQuery('.close-notice').on('click', function(event){
        event.preventDefault();
        var el = jQuery(this);
        el.parent('.woocommerce-message, .woocommerce-info').hide('fast');
    });

    /*
     * debouncedresize: special jQuery event that happens once after a window resize
     *
     * Copyright 2012 @louis_remi
     * Licensed under the MIT license.
     */
    (function(jQuery) {
        var jQueryevent = jQuery.event,
            jQueryspecial,
            resizeTimeout;

        jQueryspecial = jQueryevent.special.debouncedresize = {
            setup: function() {
                jQuery( this ).on( "resize", jQueryspecial.handler );
            },
            teardown: function() {
                jQuery( this ).off( "resize", jQueryspecial.handler );
            },
            handler: function( event, execAsap ) {
                // Save the context
                var context = this,
                    args = arguments,
                    dispatch = function() {
                        // set correct event type
                        event.type = "debouncedresize";
                        jQueryevent.dispatch.apply( context, args );
                    };

                if ( resizeTimeout ) {
                    clearTimeout( resizeTimeout );
                }

                execAsap ?
                    dispatch() :
                    resizeTimeout = setTimeout( dispatch, jQueryspecial.threshold );
            },
            threshold: 150
        };

    })(jQuery);

    /*var woocommerceProductImage = jQuery('.product-large-image img'),
        woocommerceProductThumbnailsList = jQuery('div.product .product-thumbnails_list'),
        woocommerceProductThumbnailItem = jQuery('div.product .product-thumbnails_item'),
        woocommerceProductThumbnailsLength = woocommerceProductThumbnailItem.length;

    // zoom init
    function zoomInit() {

        if ( woocommerceProductImage.length <= 0 ) {
            return;
        }

        jQuery('.zoomContainer').remove();

        if ( jQuery.isFunction( jQuery.fn.elevateZoom ) ) {
            woocommerceProductImage.elevateZoom({
                zoomType: "inner",
                cursor: "pointer",
                zoomWindowFadeIn: 500,
                zoomWindowFadeOut: 750
            });
        }
    }

    zoomInit();*/

    /*jQuery(document).on('click', '.product-thumbnails_item', function(event) {

        event.preventDefault();
        var _this      = jQuery(this),
            _parent    = _this.parents('.product-images'),
            _large_img = _this.attr('data-large-img'),
            _orig_img  = _this.attr('data-original-img');

        if ( jQuery( '.placeholder-thumb', _this ).length > 0 ) {
            return;
        }

        if (jQuery('div.product .owl-item').length) {
            _this.parent().addClass('active-image').siblings().removeClass('active-image');
        } else {
            _this.addClass('active-image').siblings().removeClass('active-image');
        }

        _parent.find('.product-large-image img').attr('src', _large_img);
        _parent.find('.product-large-image img').attr('data-zoom-image', _orig_img);
        zoomInit();
    });


    if (woocommerceProductThumbnailsLength > 5) {
    woocommerceProductThumbnailsList.owlCarousel({
        loop:false,
        margin:10,
        nav:true,
        items: 5
    });
        woocommerceProductThumbnailItem.css({
            'width': 'auto',
            'margin': '0',
            'float': 'none'
        });
    }

    jQuery(window).on( "orientationchange debouncedresize", zoomInit );

    if (woocommerceProductThumbnailsLength <= 5) {
        woocommerceProductThumbnailsList.addClass('less-than-five-items');
    }*/

    jQuery('.cart_totals .shipping td').attr('colspan', '2');

    jQuery('header .widget_shopping_cart h5').on('click', function() {
        if (jQuery('header .widget_shopping_cart .product_list_widget li').length) {
            jQuery('header .widget_shopping_cart h5, header .widget_shopping_cart h5 + .widget_shopping_cart_content').toggleClass('opened');
        }
    });

    //Change variation images on variation change
    /*jQuery(document).on('found_variation', function(event, variation) {
        event.preventDefault();
        var thumb     = variation.image_src,
            large_img = variation.image_link,
            alt       = variation.image_alt,
            item      = jQuery('.product-large-image'),
            link      = jQuery('a', item),
            image     = jQuery('img', item);

        if ( '' == thumb || '' == large_img ) {
            thumb     = link.data('initial-thumb'),
            large_img = link.data('initial-thumb-large');
        } else if ( jQuery('.product-thumbnails_item.active-image').length > 0 ) {
            jQuery('.product-thumbnails_item.active-image').removeClass('active-image');
        } else if ( jQuery('.owl-item.active-image').length > 0 ) {
            jQuery('.owl-item.active-image').removeClass('active-image');
        }

        link.attr('href', large_img);
        image.attr('src', thumb).data('zoom-image', large_img).attr('data-zoom-image', large_img);
        zoomInit();
    });*/

    /*jQuery(document).on('reset_data', function(event) {
        event.preventDefault();
        var item          = jQuery('.product-large-image'),
            link          = jQuery('a', item),
            image         = jQuery('img', item),
            initial_thumb = link.data('initial-thumb'),
            initial_large = link.data('initial-thumb-large');

        link.attr('href', initial_large);
        image.attr('src', initial_thumb).data('zoom-image', initial_large).attr('data-zoom-image', initial_large);
        zoomInit();
    });*/ 

    function checkSrcs() {
        var mainImageTitle = jQuery('.yith_magnifier_zoom.woocommerce-main-image img').attr('src');
        
        jQuery('.yith_magnifier_gallery').find('li').each(function(){
            var el = jQuery(this),
                elSrc = el.find('a').attr('data-small');

            if(elSrc === mainImageTitle) {
                el.addClass('active');
            } else {
                el.removeClass('active');
            }
        })
    }

    if (jQuery('body.single-product').length) {        
        checkSrcs();
    }
    jQuery('a.yith_magnifier_thumbnail').live('click', checkSrcs);

    //Disable cart selection
    (function (jQuery) {
        jQuery.fn.disableSelection = function () {
            return this
                .attr('unselectable', 'on')
                .css('user-select', 'none')
                .on('selectstart', false);
        };
        jQuery('header .widget_shopping_cart h5').disableSelection();
    })(jQuery);

    // Make cart content disappear. If an event gets to the body
    jQuery("html").on('click', function() {
        jQuery(".widget_shopping_cart_content, .widget_shopping_cart h5").removeClass("opened");
    });

    // Prevent events from getting pass h3 and cart content
    jQuery(".widget_shopping_cart").on('click', function(e) {
        e.stopPropagation();
    });

    // Change product review widget layout
    jQuery('[id*="woocommerce_recent_reviews"].widget .product_list_widget li').each(function() {
        var self = jQuery(this);

        self.wrapInner('<div class="product-content_wrapper"></div>');
        self.find('img').insertBefore(self.find('.product-content_wrapper'));
    });

    /*if (jQuery('.woocommerce-product-lightbox-enabled').length) {
         //Add magnific popup with gallery to product image
        jQuery('.product-images .product-thumbnails .product-thumbnails_item').on('click', function() {
            var thumbnailHref = jQuery(this).find('a').attr('href');
            jQuery('.product-images .product-large-image a').attr('href', thumbnailHref);
        });
        jQuery('.product-images .product-large-image').magnificPopup({
            type: 'image',
            delegate: 'a',
            closeBtnInside: false,
        });
    } else {
        jQuery('.product-images .product-large-image > a').on('click', function() {
            this.preventDefault();
        })
    }*/

    jQuery('.products li.product').each(function(){
        var el = jQuery(this),
            elHeight = el.find('.product-wrap').innerHeight();

        el.find('.product_hidden-content').css({'padding-top': elHeight});
    })
});