  function jq(t) {
    $(t)
      .bind("drag", function(event) {
        $(event.dragProxy).css({
          left: event.offsetX,
          top: event.offsetY
        });
      })
      
      .bind("dragstart", function(event) {
        $(this).css("cursor", "move");
        return $(this)
        	.clone()
        	.css({'z-index':100, 'position': 'absolute', 'opacity':.5})
        	.appendTo(document.body);
        	// .attr("id", "proxy");
      })
      
      .bind("dragend", function(event) {
        $(this).css("cursor", "default");
        $(event.dragProxy).fadeOut("fast", function() {
          $(this).remove();
        });
        var id = $(this).attr("id").match(/[0-9]+(_[0-9]+)*/)[0];
        var count = parseInt($("#" + id + " > p > .l").html().match(/[0-9]+/)[0]) - 1;
        if(!event.dropTarget) {
          var req = new JsHttpRequest();
          req.onreadystatechange = function() {
            if (req.readyState == 4) {
              var content = req.responseJS.content;
              var count = req.responseJS.cnt;
              var total = " " + req.responseJS.total;
              var cart = $(".cart_top");
              if(count <= 0) {
                cart.children(".cart_tov").remove();
              } else {
                var count = $("#" + id + " > p > .l");
                var cost = $("#" + id + " > p > .r");
                if(content == "") {
                  $("#" + id).remove();
                } else {
                  count.html("x" + content.quantity);
                  cost.html(" " + content.cost);
                }
              }
              $(".cart_sum > .chena").html(total);
            }
          }
          req.open(null, 'index.php', true);
            var send = {
              'ukey':'cart',
              'remove': id,
              'count': count,
              'id': id
            }
          req.send(send);
        }
      })
  }
  
  $(function() {
    jq($(".cart_tov img"));
    $(".img_thumbnail")
      .bind("drag", function(event) {
        $(event.dragProxy).css({
          left: event.offsetX,
          top: event.offsetY
        });
      })
      .bind("dragstart", function(event) {
        $(this).css("cursor", "move");
        return $(this)
        	.clone()
        	.css({'z-index':100, 'position': 'absolute', 'opacity':.5})
        	.appendTo(document.body);
      })
      .bind("dragend", function(event) {
        $(this).css("cursor", "default");
        $(event.dragProxy).fadeOut("fast", function() {
          $(this).remove();
        });
      })
    $(".cart")
      .bind("drop", function(event) {
        if(event.dragTarget.className == "img_thumbnail") {
          var req = new JsHttpRequest();
          req.onreadystatechange = function() {
            if (req.readyState == 4) {
              var content = req.responseJS.content;
              var count = req.responseJS.cnt;
              var total = " " + req.responseJS.total;
              var cart = $('.cart_top');
              var tov = $("#" + content.id);
              if(!tov[0]) {
                  if(typeof(content) == "object") {
                    var tov = $("<div id='" + content.id + "' class='cart_tov'></div>");
                    var img = $("<img id='thumb" + content.id + "' src='" + content.thumbnail_url + "' />");
                    var info = $("<p></p>");
                    var name = $("<span class='n'>" + content["name"] + "</span>");
                    var br = $("<br />");
                    var quantity = $("<span class='l'>x" + content.quantity + "</span>");
                    var cost = $("<span class='r'>" + content.cost + "</span>");
                        info.append(name);
                        info.append(br);
                        info.append(quantity);
                        info.append(cost);
                      tov.append(img);
                      tov.append(info);
                    cart.append(tov);
                    jq(img);
                  } else {
                      alert('Нет на складе');
                  }
              } else {
                $("#" + content.id + " > p > .n").html(content.name);
                $("#" + content.id + " > p > .l").html("x" + content.quantity);
                var coststring = " " + content.cost;
                $("#" + content.id + " > p > .r").html(coststring);
              }
              $(".cart_sum > .chena").html(total);
            }
          }
          req.open(null, 'index.php', true);
          var q = {
            'action':'add_product',
            'productID':event.dragTarget.getAttribute("id").match(/[0-9]+/)[0],
            'ukey':'cart'
          }
          var id = q.productID;
          var j = 1;
          var sel = "#" + id + "option_" + j;
          var newid = id;
          while($(sel).length != 0) {
            if((v = $(sel).attr("value")) != "") {
              q["option_" + j] = v;
              newid += "_" + v;
            }
            ++j;
            var sel = "#" + id + "option_" + j;
          }
          q["id"] = newid;
          /*
          var i = $().length;
          for(var j = 0; j < i; ++j) {
            if((v = $(s + " .product_options" + (j+1)).value) != "") {
              q["option_" + (j+1)] = v;
              id += "_" + v;
            }
          }
          q["id"] = id;
          */
          req.send( q );
        }
      })
      
      .bind("dropstart", function(event) {
        if(event.dragTarget.className == "img_thumbnail") {
          $("#cart_tip").css('color','#F00');
        }
      })
      
      .bind("dropend", function(event) {
        if(event.dragTarget.className == "img_thumbnail") {
          $("#cart_tip").css('color','#000');
        }
      })
  });
