var _$body = $('body');
var _$couponsSelection = $('#coupons-selection');
var _$selectionArea = $('.js-coupon-selection-area');
var _$badge = $('.coupon__badge');
var _$badgeNumber = $('.js-coupon-badge-number');
var _closeButton = '<i class="coupon-remove icon-i_btn_mehr"></i>';

updateSelectionList = function (obj) {
    if (obj) {
        var selectItem = document.createElement('DIV');
        selectItem.className = 'flexbox-item box-xs-12 box-sm-12 box-md-3 box-lg-3 coupon-selected js-remove-this-coupon';
        selectItem.dataset.id = obj.id;

        var childClone = $('*[data-coupon="' + obj.coupon + '"]').children().clone();
        childClone.find('.effect-card-body').remove();
        childClone.find('.coupon-card-clicked').remove();

        $(selectItem).append(_closeButton);
        $(selectItem).append(childClone);

        _$selectionArea.append(selectItem);
    }
};

couponFlyAnimation = function (obj) {
    var _$this = obj.this;
    var _imageToAnimate = _$this.find('figure');

    if (_imageToAnimate) {
        var cloneContainer = document.createElement('DIV');
        var _imageClone = $(cloneContainer).append(_imageToAnimate.clone());

        _imageClone.offset({
            top: _imageToAnimate.offset().top,
            left: _imageToAnimate.offset().left
        });

        _imageClone.css({
            'opacity': '.5',
            'position': 'absolute',
            'width': '20rem',
            'z-index': '100'
        });

        _imageClone.appendTo(_$body);

        _imageClone.animate({
            'top': _$badgeNumber.offset().top,
            'left': _$badgeNumber.offset().left,
            'width': '10rem',
            'height': '10rem'
        },600,'linear');

        _imageClone.animate({
            'width': 0,
            'height': 0
        }, function () {
            updateSelectionList(obj);
            _$badgeNumber.text(obj.count);
            $(this).detach();
        })
    }
};

couponClickedAnimation = function (obj) {
    var _$this = obj.this;
    _$this.addClass('clicked');
    _$this.find('.coupon-card-clicked').toggleClass('active');
    setTimeout(function () {
        _$this.find('.coupon-card-clicked').toggleClass('active');
        _$this.removeClass('clicked');
    },3000);
};

couponsClear = function () {
    $('#coupon-list').val("");
    $('.js-remove-this-coupon').remove();
    _$couponsSelection.couponsSelection('clear')
};

if (_$couponsSelection.length > 0) {
    _$couponsSelection.couponsSelection();

    $('.js-add-this-coupon').on('click', function () {
        var _$this = $(this);
        if (!_$this.hasClass('clicked')) {
            _$couponsSelection.couponsSelection('addCoupon', _$this);
        }
    });

    $('.js-add-this-coupon-special').on('click', function () {
        var _$this = $(this);
        if (!_$this.hasClass('clicked')) {
            _$couponsSelection.couponsSelection('addSpecialCoupon', _$this);
        }
    });

    _$body.on('click', '.js-print-coupons', function () {
        _$couponsSelection.couponsSelection('printCoupons');
    });

    _$body.on('click', '.js-remove-this-coupon', function () {
        var _$this = $(this);
       _$couponsSelection.couponsSelection('removeCoupon',_$this.data('id'));
       _$this.remove();
    });

    _$body.on('bm.coupons.add.updated', function (event,obj) {
        couponFlyAnimation(obj);
        couponClickedAnimation(obj);
    });

    _$body.on('bm.coupons.remove.updated', function (event,obj) {
        _$badgeNumber.text(obj.coupons.length);
    });

    _$body.on('bm.coupons.max.default', function () {
        $('.open-modal-coupons-default').trigger('click');
    });

    _$body.on('bm.coupons.max.special', function () {
        $('.open-modal-coupons-special').trigger('click');
    });

    _$body.on('bm.coupons.print', function (event,txt) {
        if (txt !== '') {
            $('#coupon-list').val(txt);
            $('#coupon-form').submit();
            setTimeout(function () {
                couponsClear();
            }, 1000);
        }
    });
}