jQuery(document).ready(function  () {
  jQuery(".btn_buy").click(function (event) {
    event.preventDefault();
    window.timeouts = window.timeouts ? window.timeouts : new Array();
    while (timeout = window.timeouts.pop()) {
      clearTimeout(timeout);
    }
    window.timeouts = [];

    jQuery("#popcart").stop(true, true); // clear the queue and complete the last one
    jQuery.ajax({
      type: "POST",
      url: "cart/modify.html?" + jQuery(this).parents("form").first().serialize(),
      cache: false,
      success: function (data, jqXHR) {
        if (data["multiple"] != 0) {
          jQuery("#popcart").children(".single").hide();
          jQuery("#popcart").children(".multiple").show();
        } else {
          jQuery("#popcart").children(".single").show();
          jQuery("#popcart").children(".multiple").hide();
        }


        jQuery("#popcart").slideUp();
        jQuery("#popcart").slideDown();
        for (attr in ( data)) {
          jQuery("#popcart-" + attr).html(data[attr]);
        }
        // update the cart items
        jQuery(".cart_items").each(function () {
          jQuery(this).text(data.totalQuantity + " ");
        });
        show_or_hide_cart();
        timeouts.push(setTimeout(function () {
          jQuery("#popcart").slideUp();
        }, 10000));

      }
    });
  });

  jQuery(".close").click(function () {
    while (timeout = window.timeouts.pop()) {
      clearTimeout(timeout);
    }
    jQuery("#popcart").slideUp();
  });
});

jQuery(document).ready(function  () {
    jQuery(window).scroll();

    show_or_hide_cart = function () {
    if (jQuery("#cart").find(".cart_items").text() == "0") {
      jQuery("#cart").hide();
    } else {
      jQuery("#cart").show();
    }
  }
  show_or_hide_cart();
});



