<script type="text/javascript">
    Shopify.updateCartInfo = function(cart, cart_summary_id, cart_count_id) {
        if ((typeof cart_summary_id) === 'string') {
            var cart_summary = jQuery(cart_summary_id);
            if (cart_summary.length) {

                cart_summary.empty();

                jQuery.each(cart, function(key, value) {
                    if (key === 'items') {

                        if (value.length) {

                            jQuery('<div class="items"></div>').appendTo(cart_summary);
                          	jQuery('<div class="wrap-action"></div>').appendTo(cart_summary);
                          
                            var table = jQuery(cart_summary_id + ' div.items');
                          	var note_order = jQuery(cart_summary_id + ' div.cart-order-note');
                          	var table_action = jQuery(cart_summary_id + ' div.wrap-action');

                            jQuery.each(value, function(i, item) {

                                jQuery('<div class="items-inner animated-' + i + '"> \
                                    <div class="cart-item-image"> \
                                        <a href="' + item.url + '">\
                                            <img src="' + Shopify.resizeImage(item.image, '90x') + '" alt="" />\
                                        </a>\
                                    </div>\
                                    <div class="cart-item-info">\
                                        <div class="cart-item-title">\
                                            <a href="' + item.url + '">' + item.product_title + '</a>\
                                        </div>\
										<div class="cart-item-quantity"><span>QTY: ' + item.quantity  + '</span></div>\
                                        <div class="cart-item-price">' + Shopify.formatMoney(item.price, '{{shop.money_format}}')  + '</div>\
                                    </div>\
                                    <a data-cart-item="' + item.id + '" class="cart-close" title="{{ 'general.header.cart_info.remove' | t }}" href="javascript:;"><i class="demo-icon icon-close" aria-hidden="true"></i></a>\
                                 	</div>').appendTo(table);
                            }); 
                                  
                            jQuery('<div class="wrap-subtotal"> \
							<div class="wrap-progress"><div class="sb-progress"></div><div class="sb-text"></div></div> \
                            <div class="subtotal"><span>{{ "general.header.cart_info.subtotal" | t }}</span><span class="cart-item-total-price">' + Shopify.formatMoney(cart.total_price, '{{shop.money_format}}') + '</span></div>\
                            <div class="action"><a href="/cart\" class="btn btn-2">{{ "general.header.cart_info.view_shopping_bag" | t }}</a><button class="btn btn-1" type="submit" name="checkout">{{ "general.header.cart_info.checkout" | t }}</button></div>\
							</div>').appendTo(table_action);

                        }
                        else {
                            jQuery('<div class="cart-item-empty"><p>{{ 'general.header.cart_info.cart_empty_html' | t }}</p></div>').appendTo(cart_summary);
                        }
                    }
                });


            }
        }

        updateCartDesc(cart);
    };

    function updateCartDesc(data){
        var $cartLinkText = $('.cart-target .number');
      	var $cartPrice = Shopify.formatMoney(data.total_price, '{{shop.money_format}}');
        
        switch(data.item_count){
             case 0:
               $cartLinkText.html('<span class="n-item">0</span>');
               break;
             case 1:
               $cartLinkText.html('<span class="n-item">1</span>');
               break;
             default:
               $cartLinkText.html('<span class="n-item">' + data.item_count + '</span>');
               break;
        }
      
        $cartLinkText.append($cartPrice);
          
       	$('.addcart-modal-subtotal').html('<strong>{{ 'general.header.cart_info.subtotal' | t }}:</strong>' +Shopify.formatMoney(data.total_price, '{{shop.money_format}}'));
      
      	// Update free shipping
        {% if settings.enable_free_shipping %}
          var _cart_total = data.total_price
              ,_free_shipping_price = {{ settings.free_shipping_price }}
              ,_free_shipping_price_100 = {{ settings.free_shipping_price }}*100;
                                  
          if (_cart_total > _free_shipping_price_100){
            $('.sb-progress').html('<span class="sb-progress-bar" style="width: 100%">100%</span>');
            $('.sb-text').html('Free shipping for orders over ' + Shopify.formatMoney(_free_shipping_price_100, "{{shop.money_format}}"));
          }
                                                                                                                  
          else{
            var _progress = _cart_total / _free_shipping_price
                ,_spend = _free_shipping_price_100 - _cart_total;
                                                                                                            
            $('.sb-progress').html('<span class="sb-progress-bar" style="width:' + _progress +'%">' + _progress + '%</span>');
            $('.sb-text').html('Spend ' + Shopify.formatMoney(_spend, "{{shop.money_format}}") + ' reach FREE SHIPPING! Continue shopping to add more products to your cart and receive free shipping for orders over ' + Shopify.formatMoney(_free_shipping_price_100, "{{shop.money_format}}"));
          }
        {% else %}
          $('.wrap-progress').hide();
        {% endif %}  
                                                            
        {% if settings.show_multiple_currencies %}
           currenciesCallbackSpecial('.cart-target span.money', '.wrap-progress .sb-text span.money');
           currenciesCallbackSpecial('#cart-info span.money');
           currenciesCallbackSpecial('#layer-addcart-modal span.money');
       	{% endif %}
    }

    Shopify.onCartUpdate = function(cart) {
        Shopify.updateCartInfo(cart, '#cart-info #cart-content', 'shopping-cart');
    };

    $(window).load(function() {
        Shopify.getCart(function(cart) {
            Shopify.updateCartInfo(cart, '#cart-info #cart-content');	
        });
      
      	$('.cart-sb').on('click', '.cart-close', function(e){
            var prodId = $(e.currentTarget).data('cart-item');
            $('body').addClass('cart-loading');
            $.ajax({
                type: 'POST',
                url: '/cart/change.js',
                data: { quantity: 0, id: prodId },
                dataType: 'json',
                error: function(err){
                    console.log(err);
                },
                success: function(resp) {
                	$('body').removeClass('cart-loading');
                    Shopify.updateCartInfo(resp, '#cart-info #cart-content');
                    if(typeof cartObj != "undefined") {
                        cartObj.syncCartSB(resp, prodId)
                    } else {
                        Currency.convertAll('{{ settings.default_currency }}', Currency.currentCurrency);    
                    }
                                
                    // Update free shipping
                    {% if settings.enable_free_shipping %}
                      var _cart_total = resp.total_price
                          ,_free_shipping_price = {{ settings.free_shipping_price }}
                          ,_free_shipping_price_100 = {{ settings.free_shipping_price }}*100;
                                  
                      if (_cart_total > _free_shipping_price_100){
                        $('.sb-progress').html('<span class="sc-progress-bar" style="width: 100%">100%</span>');
                        $('.sb-text').html('Free shipping for orders over ' + Shopify.formatMoney(_free_shipping_price_100, "{{shop.money_format}}"));
                      }
                                                                                                                  
                      else{
                        var _progress = _cart_total / _free_shipping_price
                         	  ,_spend = _free_shipping_price_100 - _cart_total;
                        $('.sb-progress').html('<span class="sb-progress-bar" style="width:' + _progress +'%">' + _progress + '%</span>');
                        $('.sb-text').html('Spend ' + Shopify.formatMoney(_spend, "{{shop.money_format}}") + ' reach FREE SHIPPING! Continue shopping to add more products to your cart and receive free shipping for orders over ' + Shopify.formatMoney(_free_shipping_price_100, "{{shop.money_format}}"));
                      }
                    {% endif %}  
                                                                        
                   {% if settings.show_multiple_currencies %}
                       currenciesCallbackSpecial('.wrap-progress .sb-text span.money');
                   {% endif %}
                }
            })
        })
    });
</script>