$(document).ready(function(){
    // for add item to cart
    $('[rel=addToCart]').click(function(){
        var $this = $(this);
            if($this.children('img').length == 0) {
                $this = $this.parent().parent().parent().parent().parent().parent().find('.images a');
            }
        flyToCart($this, function() {
            addToCart($this);
        })
        return false;
    });
    // event drag - drop
    $('[rel=addToCart] img').draggable({
        containment: 'document',
        opacity: 0.1,
        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);
            }
        });
    }

    // fly to cart when user click to product item
    function flyToCart ($this, fn)
    {
        var itemImgObj  = $this.children('img'),
            cartOffsetX = $('#cart').offset().left - itemImgObj.offset().left + 20, // offset top of cart
            cartOffsetY = $('#cart').offset().top - itemImgObj.offset().top; // offset top of cart

        $this.prepend('<img src="' + itemImgObj.attr('src') + '" id="itemFly" />');
        $('#itemFly')
            .css({'position' : 'absolute', zIndex: '999999'})
            .animate({opacity: 0.6}, 100 )
            .animate({
                opacity: 0.1,
                marginLeft: cartOffsetX,
                marginTop: cartOffsetY,
                width: $('#cart').width(),
                height: $('#cart').height()
            }, 1200, function(){
                $(this).remove();
                if(typeof fn == 'function') { fn(); } // if fn is the function, then calling it
            });
        ;
    }
    /* .......................................................................... */
    // 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;
    });
	    /* .......................................................................... */
    // for remove cart
    $('[rel=removeCart]').click(function(){
        var _seft = $(this);
        var url = _seft.attr('href');
        $.ajax({
            url: url,
            type: 'get',
            dataType: 'html',
        });
        return false;
    });
});
