(function ($) {
	'use strict';

	var ecomus = ecomus || {};

	ecomus.init = function () {
        this.stickyAddToCart();
        this.addToCartFormScroll();
        this.foundVariation();
        this.changeAttributesData();

        $(document.body).on('ecomus_off_canvas_closed ecomus_modal_closed ecomus_popover_closed', function (e) {
            ecomus.stickyAddToCart();
        });
    }

    ecomus.stickyAddToCart = function() {
        var $selector = $( '#ecomus-sticky-add-to-cart' );
        if ( !$selector.length ) {
            return;
        }

        var headerHeight = 0;

        if ( $(document.body).hasClass( 'admin-bar' ) ) {
            headerHeight += 32;
        }
        function stickyATCToggle() {
            var cartHeight = $('div.product').find( 'form.cart' ).offset().top + $('div.product').find( 'form.cart' ).outerHeight() - headerHeight;

            if ( window.scrollY > cartHeight ) {
                $selector.addClass( 'open' );
                $(document.body).addClass('ecomus-atc-sticky-height-open');
            } else {
                $selector.removeClass( 'open' );
                $(document.body).removeClass('ecomus-atc-sticky-height-open');
            }

        }

        $(window).on( 'scroll', function () {
            stickyATCToggle();
        } );

        $(window).on( 'resize', function () {
            var height = $selector.height();
            $(document.body).css('--em-atc-sticky-height', height + 'px');
        } ).trigger('resize');
    }

    ecomus.addToCartFormScroll = function() {
        //sticky-atc-button
        $( '#ecomus-sticky-add-to-cart' ).on( 'click', '.em-add-to-cart-options', function ( event ) {
            event.preventDefault();

            $( 'html,body' ).stop().animate({
                scrollTop: $('div.product').find( 'form.cart' ).offset().top - 50
            },
            'slow');
        });
    }

    ecomus.foundVariation = function() {
        var $selector = $( '#ecomus-sticky-add-to-cart' );
        if ( !$selector.length ) {
            return;
        }

        var $image = $selector.find( '.ecomus-sticky-atc__image img' ),
            $price = $selector.find( '.ecomus-sticky-atc__price' ),
            $priceValue = $price.html();
        $selector.find('form.cart')
        .on( 'reset_data', function() {
            $image.attr( 'src', $image.data( 'o_src' ) );
            $price.html( $priceValue );
        } )
        .on( 'found_variation', function( event, variation ) {
            if ( variation.image && variation.image.gallery_thumbnail_src && variation.image.gallery_thumbnail_src.length > 1 ) {
                $image.attr( 'src', variation.image.gallery_thumbnail_src );
            }

            if ( variation.price_html && variation.price_html.length ) {
                $price.html( $( variation.price_html ) );
            }
        } );
    }

    ecomus.changeAttributesData = function() {
        var $selector = $( '#ecomus-sticky-add-to-cart' );
        if ( !$selector.length ) {
            return;
        }
        var $image = $selector.find( '.ecomus-sticky-atc__image img' ),
            $addToCart = $selector.find('.single_add_to_cart_button');

        $selector.find( '.ecomus-sticky-atc__variations' ).on('change', 'select', function (e) {
            var optionSelected = $("option:selected", this),
                imageSelected = optionSelected.data('image'),
                attributes = optionSelected.data('attributes');

            if( attributes !== undefined ) {
                $addToCart.removeClass('disabled');
                for( var key in attributes) {
                    $selector.find('form.cart').find('input[name="'+ key +'"]').val( attributes[key] );
                }
            } else {
                $addToCart.addClass('disabled');
            }

            if( imageSelected === undefined ) {
                imageSelected = $image.data('o_src');
            }
            $image.attr( 'src', imageSelected );
        });
    }
	/**
	 * Document ready
	 */
	$(function () {
		ecomus.init();
	});

})(jQuery);