(function() {
  $(document).ready(function() {
    var Cart = {
      init: function() {
        var that = this;
        that.clickHandler(that);
      },

      clickHandler: function(that) {
        $("#product_catalog .small_cart a").live('click', function() {
          var catalog_num = $(this).parent().attr("rel");
          var photo = $(this).closest(".item1").find("img:first");
          var product_x = photo.offset().left;
      		var product_y = photo.offset().top;
          var basket_x = $(".cart_menu").offset().left;
    			var basket_y = $(".cart_menu").offset().top;
          var goto_x = basket_x - product_x;
          var goto_y = basket_y - product_y;
          var new_image_width = photo.width / 3;
          var new_image_height = photo.height / 3;
          photo.clone()
          .prependTo(photo.parent())
          .css({
            'position' : 'absolute', 'top': '14px', 'left':'14px', 'z-index': 1
          })
          .animate({opacity: 0.4}, 100 )
          .animate({opacity: 0.1,marginLeft: goto_x,marginTop: goto_y,width: new_image_width,height: new_image_height}, 1200, function() {
            $(this).remove();
            $.ajax({
              method: "post",
              url: "/cart/actions?type=add_cart&item=" + catalog_num,
              success: function(item_count) {
                if (item_count) $(".cart_menu a").html("Корзина ("+ item_count +")");
              }
            })
          });
          return false;
        }),

        $("#cart_form .cart_body input").change(function() {
          if ($(".cart_edit").css("display") == 'none') $(".cart_edit").show();
          return false;
        }),

        $(".cart_edit").click(function() {
          $("#CartActionInput").val("edit");
          $("#cart_form").submit();
          return false;
        }),
        $(".cart_subm").click(function() {
          $("#CartActionInput").val("order");
          $("#cart_form").submit();
          return false;
        }),
        $(".product_desc .cart a").live('click', function() {
          var catalog_num = $(this).parent().attr('rel');
          var cart_obj = $(this);
          if (catalog_num) {
            $.ajax({
              method: "post",
              url: "/cart/actions?type=add_cart&item=" + catalog_num,
              success: function(item_count) {
                if (item_count) $(".cart_menu a").html("Корзина ("+ item_count +")");
                cart_obj.parent().prepend("<p class='notice_cart'>Вы успешно добавили товар в корзину</p>");
                setTimeout('$(".notice_cart").fadeOut()', 2000);
              }
            })
          }
          return false;
        }),
        $(".minus").click(function() {
          that.changeCount($(this), 'minus');
        }),
        $(".plus").click(function() {
          that.changeCount($(this), 'plus');
        }),
        $(".delivery select").change(function() {
          if($(this).val() == 2) {
            if ($(".delivery_time").css("display") == 'block') $(".delivery_time").hide();
          } else {
            if ($(".delivery_time").css("display") == 'none') $(".delivery_time").show();
          }
        })
      },
      changeCount: function(el, type) {
        var input_el = el.parent().find("input");
        var incr = el.parent().find("input").attr("rel");
        if (type == 'plus') {

          if (parseInt(input_el.val()) < incr) input_el.val(incr);
            else input_el.val(parseInt(input_el.val()) + parseInt(incr));
        } else if (type == 'minus') {
          if (parseInt(input_el.val() <= incr)) input_el.val(0);
            else input_el.val(parseInt(input_el.val()) - parseInt(incr));
          if (input_el.val() < 0) input_el.val(0);
        }
        if ($(".cart_edit").css("display") == 'none') $(".cart_edit").show();
        return false;
      }
    }

    Cart.init();
  })
})(jQuery);
