(function($) {
    'use strict';

    jQuery(document).ready(function() {

        //woo instant redirect checkout page
		$('.qcld-express-inner .wc-proceed-to-checkout a').removeClass('checkout-button');
		$('.qcld-express-container .wc-proceed-to-checkout a').attr('href', qcld_get_checkout_url);

    	//Nav Toggler
    	$(document).on('click', '#qcld-express-toggler, .added_to_cart', function(e) {
    	    e.preventDefault();

    	    var targetClass = $('#qcld-express-toggler, .added_to_cart');

    	    if (targetClass.hasClass('open')) {
    	        targetClass.removeClass('open');
    	        if ($(window).width() < 767) {
	    	        $('.qcld-express-container').animate({
	    	            'right': '-100%'
	    	        });
    	        }else{
	    	        $('.qcld-express-container').animate({
	    	            'right': '-50%'
	    	        });
	    	    }
    	    } else {
    	        targetClass.addClass('open');
    	       	$('.qcld-express-container').animate({
    	            'right': '0%'
    	        });
    	    }
    	    //Update cart on Nav Toggle
    	    jQuery("[name='update_cart']").removeAttr('disabled');
    	    jQuery('[name="update_cart"]').trigger('click'); // Update Cart

    	});

	    //Collapse Nav if click on body
        $('html').on('click', function (e) {
            if (!$('#qcld-express-toggler, .added_to_cart, .add_to_cart_button').is(e.target) && $('#qcld-express-toggler, .added_to_cart, .add_to_cart_button').has(e.target).length === 0 && !$('.qcld-express-inner').is(e.target) && $('.qcld-express-inner').has(e.target).length === 0) {
    	        $('#qcld-express-toggler, .added_to_cart').removeClass('open');
    	        if ($(window).width() < 767) {
	    	        $('.qcld-express-container').animate({
	    	            'right': '-100%'
	    	        });
    	        }else{
	    	        $('.qcld-express-container').animate({
	    	            'right': '-50%'
	    	        });
	    	    }
            }

            //woo instant redirect checkout page
    		$('.qcld-express-inner .wc-proceed-to-checkout a').removeClass('checkout-button');
    		$('.qcld-express-container .wc-proceed-to-checkout a').attr('href', qcld_get_checkout_url);

        });


	 	//Add To Cart Fly Effect
		$(document).on('click', '.add_to_cart_button:not(.product_type_variable), .single_add_to_cart_button:not(.disabled)', function() {

		    $('body').append('<div id="qcld-express-cart-fly"><i class="fa fa-shopping-cart"></i></div>');

		    var endPos = $("#qcld-express-toggler").offset();
		    var startPos = $(this).offset();

		    if(qcld_express_cart_mode == 'wooinstants'){

			    $('#qcld-express-cart-fly').css({
			        'top': startPos.top + 'px',
			        'left': startPos.left + 'px'
			    })
			    .animate({
			        opacity: 1,
			        top: endPos.top,
			        left: endPos.left
			    }, 1500, function() {
			        $(this).css({
			            'opacity': '0',
			            'z-index': '0'
			        });
			        $(this).detach();
			        $.fancybox.close();
			    });

			}


		});

		$(document).on('keyup click','.qc_product_quantity,.qcld-express-simple-p-qnty',function(event) {
	        var quantity = $(this).val();
	        var change_value = $(this).parent().parent().find('.ajax_add_to_cart');
	        $(change_value).attr('data-quantity', quantity);
	        
	    });

	    $(document).on('click','.qc_plus, .qc_minus',function(event) {
	        $(this).parent().find('.qc_product_quantity').trigger('keyup');
	    });

    });

})(jQuery);