$(document).ready(function(){
    $("a.plus").click(function () {

        var id = this.id;

        var count = isNaN(parseInt($("#d"+id).val())) ? 1 : parseInt($("#d"+id).val());

        var dwidth  = $(document).width();
        var dstop   = $(document).scrollTop();

        var mid = "addcartMsg"+this.id;

            $.ajax({
              url: "/ajax.addForm.php?id="+id,
              cache: false,
              success: function(html)
              {
                  if(html)
                  {
                      $(document.body).append("<div class='addCart' id='"+mid+"'></div>");

                      $("#"+mid).html(html);

                      $w = $("#"+mid).width();
                      $h = $("#"+mid).height();

                      $("#"+mid).css("top",($(window).height())/2+dstop-$h/2);
                      $("#"+mid).css("left",dwidth/2-$w/2);

                      $('#closeAddForm').click(function(){$("#"+mid).remove(); return false;});

                      var ocart = $("div.cart").offset();

                      $("#"+mid).show();

                      digitalinp_init();


                      $('#addAndBack').click(function(){

                          var count = $('#addCount').val();
                          
                          $.ajax({
                              url: "/ajax.addCart.php?id="+id+"&count="+count,
                              cache: false,
                              success: function(html)
                              {
                                
                                  $(".addCartBlock .count").html(html);
                                  $("#"+mid).animate({
                                  
                                       opacity : 0,
                                       top     : ocart.top,
                                       left    : ocart.left,
                                       width   : 0,
                                       height  : 0,
                                       fontSize: "1px",
                                       padding : "5px"

                                      },500
                                      ,'linear',function(){
                                      $("#"+mid).remove();
                                      $.ajax({
                                        url: "/ajax.getCart.php",
                                        cache: false,
                                        success: function(html)
                                        {
                                            if(html == '0')
                                            {
                                                $("div.gocart").fadeOut("slow");                
                                                $("div.cart").html("В корзине нет товаров");                              
                                            }
                                            else
                                            {
                                                if($("div.gocart").is(":hidden"))
                                                {
                                                    $("div.gocart").fadeIn("slow");               
                                                }
                                                $("div.cart").html(html);                              
                                            }
                                        }
                                      });
                                  });
                              }
                          });
                          

                          return false;
                      });                       
                      $('#addAndCart').click(function(){
                          var count = $('#addCount').val();                          
                          $.ajax({
                              url: "/ajax.addCart.php?id="+id+"&count="+count,
                              cache: false,
                              success: function(html)
                              {
                                  window.location = "/cart";
                              }
                          });
                          
                      });
                  }
              }
            });

            return false;
       
    });

    $('#closeAddForm').click(function(){
        
        return false;
    });

});




















