$(document).ready(function(){
    // for add item to cart
    $('[rel=addToCart]').click(function(){
        addToCart($(this));
        return false;
    });
    // event drag - drop
    $('[rel=addToCart] img').draggable({
        containment: 'document',
        opacity: 0.6,
        revert: 'invalid',
        helper: 'clone',
        zIndex: 100
    });
    $("#cart").droppable({
        drop: function(e, ui) {
            addToCart($(ui.draggable).parent());
        }
    });

    function addToCart (_seft) {
        var _url = _seft.attr('href');
        $.ajax({
            url: _url,
            type: 'get',
            dataType: 'json',
            beforeSend: function (){
                _seft.parent().parent().find('.busy').show();
            },
            success: function(db) {
                _seft.parent().parent().find('.busy').hide();
                $('#totalItems').html(db.total);
                $('#totalPrice').html(db.price);
            }
        });
    }
    /* .......................................................................... */
    // for remove all cart
    $('[rel=removeAllCart]').click(function(){
        var _seft = $(this);
        var url = _seft.attr('href');
        $.ajax({
            url: url,
            type: 'get',
            dataType: 'html',
            beforeSend: function (){
                _seft.parent().parent().parent().find('.busy').show();
            },
            success: function(db) {
                _seft.parent().parent().parent().find('.busy').hide();
                $('#totalItems').html('0');
                $('#totalPrice').html('$0');
            }
        });
        return false;
    });
});
