  var blocked = false;
  var messaged = false;
  var prid = 0

  function addToCart(id) {
    if(blocked == false) {
      blocked = true;
      prid = id;
      $("#busyproduct_"+id).css("display","");
      $.post("/ajax/cart/", {id:id, count:$('#count_'+id).val()},
        function(data){
          $('#cart').html(data);
          $('div[id^="busyproduct"]').css('display','none');
          renderMessage("Корзинка обновлена", "buy_"+prid, 6, 55, 210);
      });
    }
  }

  function delFromCart(id) {
    if(!blocked) {
      blocked = true;
      prid = id;
      $.post("/ajax/delcart/", {id:id},
        function(data){
          $('#cart').html(data);
          renderMessage("Корзина обновлена", "cart", 0, 0, 161);
      });
    }
  }

  function renderMessage(message, id, top, left, width) {
    if(messaged == false) {
      messaged = true;
      offset = $("#"+id).offset();
      top = $("#"+id).scrollTop()+offset.top - top;
      left = offset.left - left;

      mess = $("<div id='message' style='height:32px; padding-top:9px; text-align:center; top:"+top+"px; left:"+left+"px; background:#fff; border:2px solid #D0914D; width:"+width+"px; position:absolute;'>"+message+"</div>");
      $("body").append(mess);
      setTimeout("renderMessage(0)",3000);
    } else {
      $("#message").remove();
      blocked = false;
      messaged = false;
    }
  }

  function destroyCart() {
    $(".main_cart").remove();
    $(".hideblock").remove();
  }

  function makeCart() {
    height = $("body").height();
    offsetTop = $(document).scrollTop();
    hideLayer = $("<div class='hideblock' style='z-index:100;left:0px; top:0px; width:100%; height:"+height+"px'></div>");
    $("body").append(hideLayer);
    $("body").append("<table cellspacing='0' cellpadding='0' border='0' style='top:"+(offsetTop+100)+"px;' width='80%' height='300px' class='main_cart' id='main_cart'><tr><td align='center' style='vertical-align:center' id='cart_inner'><img src='/images/ajax-loader.gif'/></td></tr></table>");
    $.post("/ajax/getcart/", {id:null},
      function(data){
        $('#cart_inner').html(data);
    });
  }

  function reMakeCart() {
    c = new Array();
    k = 0;
    $("input[id^='prod_count']").each(function(){
      c[k] = {product_id: $(this).attr("prod_id"), count: $(this).val()};
      k++;
    })
    $('#cart_inner').html("<img src='/images/ajax-loader.gif'/>");
    
    $.post("/ajax/changecart/", {counts:$.toJSON(c)},
      function(data){
        $('#cart_inner').html(data);
    });
  }

  function removeCartProduct(id) {
    $("#prod_count_"+id).val("0");
    reMakeCart();
  }