<script>

/**
 * Module to ajaxify all add to cart forms on the page.
 *
 * Copyright (c) 2015 Caroline Schnapp (11heavens.com)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
Shopify.AjaxifyCart = (function($) {
  
  // Some configuration options.
  // I have separated what you will never need to change from what
  // you might change.
  
  var _config = {
    
    // What you might want to change
   //addToCartBtnLabel:             "{{ settings.add_to_cart_button_text }}",
    addToCartBtnLabel:             "{{ 'products.product.add_to_cart' | t }}",
    addedToCartBtnLabel:           {{ "products.product.Thanks_you" | t | json }},
    addingToCartBtnLabel:          {{ "products.product.adding" | t | json }},
    soldOutBtnLabel:               {{ "products.product.sold_out" | t | json }},
    howLongTillBtnReturnsToNormal: 700, // in milliseconds.
    cartCountSelector:             '.cart-count, #CartCount span, #gocart p a, #cart .checkout em, .item-count',
    cartTotalSelector:             '#cart-price, #CartCost',
    itemsManage:                    '.items_manage',
    // 'aboveForm' for top of add to cart form, 
    // 'belowForm' for below the add to cart form, and 
    // 'nextButton' for next to add to cart button.
    feedbackPosition:              'nextButton',
    
    // What you will never need to change
    addToCartBtnSelector:          '[type="submit"]',
    addToCartFormSelector:         'form[action="/cart/add"]',
    
    shopifyAjaxAddURL:             '/cart/add.js',
    shopifyAjaxCartURL:            '/cart.js'
  };
  
  // We need some feedback when adding an item to the cart.
  // Here it is.  
  var _showFeedback = function(success, html, $addToCartForm) {
    $('.ajaxified-cart-feedback').remove();
    var feedback = '<p class="ajaxified-cart-feedback ' + success + '">' + html + '</p>';
    $(".price-lable").before(feedback);
    switch (_config.feedbackPosition) {
      case 'aboveForm':
        $addToCartForm.before(feedback);
        break;
      case 'belowForm':
        $addToCartForm.after(feedback);
        break;
      case 'nextButton':
      default:
        $addToCartForm.find(_config.addToCartBtnSelector).after(feedback);
		$('.stiky_button').after(feedback);
        break;   
    }
    // If you use animate.css
    // $('.ajaxified-cart-feedback').addClass('animated bounceInDown');
  
    $('.ajaxified-cart-feedback').fadeIn('fast').delay(4000).fadeOut('slow');
    
  };
  var _setText = function($button, label) {
    if ($button.children().length) {
      $button.children().each(function() {
        if ($.trim($(this).text()) !== '') {
          $(this).text(label);
        }
      });
    }
    else {
      $button.val(label).text(label);
    }
  };
  var _init = function() {   
    $(document).ready(function() { 
      $(_config.addToCartFormSelector).submit(function(e) {
       
        e.preventDefault();
        var $addToCartForm = $(this);
      
        var $addToCartBtn = $addToCartForm.find(_config.addToCartBtnSelector);
        var $addToCartBtn_stck = $('.stiky_button');
        _setText($addToCartBtn, _config.addingToCartBtnLabel);
        $addToCartBtn.addClass('disabled').prop('disabled', true);
        // Add to cart.
        $.ajax({
          url: _config.shopifyAjaxAddURL,
          dataType: 'json',
          type: 'post',
          data: $addToCartForm.serialize(),
          success: function(itemData) {

            // for upsell popup start
            $("#Quantity").val('1');
            {% if template == 'product'%}
            relate_pop();
            {% endif %}
          
                        setTimeout(function(){ 
                        $('.one-time').slick({
                            infinite: true,
                            speed: 300,
                            slidesToShow: 1,
                            adaptiveHeight: true,
                            prevArrow: '.thumbnails-slider__prev',
                            nextArrow: '.thumbnails-slider__next'
                          });
                          }, 2500);  
            $('.close').on('click', function(){
            {% if section.settings.buy_it_now_action == 'direct_to_cart' %}
               window.location.href='/cart';
              {% endif %}
            
            });
            {% if section.settings.enable_related_upsell and template == 'product' %}
        	  custome_addcart();
            {% endif %}            
            
            // for upsell popup end
            {% if template == 'product' or template == 'index'%}
               related_poup();
            {% endif %}
            var added_to_cart={{ 'cart.general.added_to_cart' | t | json }};
           var view_cart ={{ "cart.general.view_cart" | t | json }};
           var continue_shopping={{ "cart.general.continue_shopping" | t | json }};
            // Re-enable add to cart button.
            $addToCartBtn.addClass('inverted');
            _setText($addToCartBtn, _config.addedToCartBtnLabel);
            
            _showFeedback('success','<i class="fa fa-check"></i>'+added_to_cart+' <a href="/cart">'+view_cart+'</a> or <a href="/collections/all">'+continue_shopping+'</a>.',$addToCartForm);
             window.setTimeout(function(){
              $addToCartBtn.prop('disabled', false).removeClass('disabled').removeClass('inverted');
              _setText($addToCartBtn,_config.addToCartBtnLabel);
              
            }, _config.howLongTillBtnReturnsToNormal);
            // Update cart count and show cart link.
            $.getJSON(_config.shopifyAjaxCartURL, function(cart) {
              if (_config.cartCountSelector && $(_config.cartCountSelector).size()) {
                var value = $(_config.cartCountSelector).html() || '0';
                $(_config.cartCountSelector).html(value.replace(/[0-9]+/,cart.item_count)).removeClass('hidden-count');
           
               var items_count_one={{ 'layout.cart.items_count.one' | t | json }};
                var items_count_other={{ 'layout.cart.items_count.other' | t | json }};
                if(cart.item_count <=1){
                  $('.items_manage').html(items_count_one);

                } else{
                  $('.items_manage').html(items_count_other);

                }
              }
              if (_config.cartTotalSelector && $(_config.cartTotalSelector).size()) {
               
                
                if (typeof Currency !== 'undefined' && typeof Currency.moneyFormats !== 'undefined') {
                   $(_config.cartTotalSelector).html(theme.Currency.formatMoney(cart.total_price, "{{ shop.money_format | remove: "'" | remove: '"' }}"));
                 }
                else {                                   
                  $(_config.cartTotalSelector).html(theme.Currency.formatMoney(cart.total_price, "{{ shop.money_format | remove: "'" | remove: '"' }}"));
                }
                 convertCurrencies();
              };
            });  
                                                    
                                                   
          }, 
          error: function(XMLHttpRequest) {
                     
            var response = eval('(' + XMLHttpRequest.responseText + ')');
            response = response.description;
                    
            if (response.slice(0,4) === 'All ') {
              _showFeedback('error', response.replace('All 1 ', 'All '), $addToCartForm);
              $addToCartBtn.prop('disabled', false);
              $addToCartBtn_stck.prop('disabled', false);
              _setText($addToCartBtn, _config.soldOutBtnLabel);
              _setText($addToCartBtn_stck, _config.soldOutBtnLabel);
              $addToCartBtn.prop('disabled',true);
              $addToCartBtn_stck.prop('disabled',true);
              
            
            }
            else 
            {
              _showFeedback('error', '<i class="fas fa-warning"></i>' + response, $addToCartForm);
              $addToCartBtn.prop('disabled', false).removeClass('disabled');
              _setText($addToCartBtn, _config.addToCartBtnLabel);
              
            }
          }
        });   
        return false;    
      });
    });
  };
  return {
    init: function(params) {
        // Configuration
        params = params || {};
        // Merging with defaults.
        $.extend(_config, params);
        // Action
        $(function() {
          _init();
        });
    },    
    getConfig: function() {
      return _config;
    }
  }  
})(jQuery);

    Shopify.AjaxifyCart.init({
      moneyFormat: {{ shop.money_format | json }}
    });

</script>

{% comment %}
  If you want to animate your feedback message.
{% endcomment %}

{% comment %}
{{ '//cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.0/animate.min.css' | stylesheet_tag }}


{{ '//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.0.3/css/font-awesome.css' | stylesheet_tag }}
{% endcomment %}
<style>
.ajaxified-cart-feedback {
  display: block;
  line-height: 36px;
  font-size: 90%;
  vertical-align: middle;
}
.ajaxified-cart-feedback.success { 
  color: #3D9970;
  text-align: center;
}
.ajaxified-cart-feedback.error { 
  color: #FF4136; 
  text-align: center;
} 
.ajaxified-cart-feedback a {
  border-bottom: 1px solid;
}
  .inverted span{ color:#fff;}
</style>
<!-- <script>
  $(document).ready(function(){
    var wd = $(window).height();
       alert(wd);
    var mis = wd - pophgt;
    });
</script> -->