(function($) { $(function() {

	var ajaxIndicator = $('.ajax-indicator');
	ajaxIndicator.css('position', 'absolute');

	var centerCheck = function(check) {
        check.css('margin-left', '-' + Math.floor(check.width()/2) + 'px');
        check.css('margin-top', '-' + Math.floor(check.height()/2) + 'px');
	};

	$('.b-choose-cover__check').each(function() {
		centerCheck($(this));
	});

	$('.b-choose-cover__cover').click(function() {
		var id = this.onclick();
		var cover = $(this);
		$.getJSON(
            '/books/set-cover/',
            {
                cover_id : id
            },
            function(data) {
                ajaxIndicator.hide();
                if(data.result == 'ok') {
                	cover.parent()
                		.find('.b-choose-cover__cover')
                		.find('i')
                		.remove()
                		.end()
                		.addClass('b-choose-cover__cover_faded');
                    cover.removeClass('b-choose-cover__cover_faded')
                    	.append('<i class="b-choose-cover__check">✓</i>');
                    centerCheck(cover.find('i'));
                } else {
                    document.location.reload(true);
                }
            }
        );
        var bookTitle = cover.parents('.b-choose-cover').find('.b-choose-cover__book');
        var offset = bookTitle.offset();
        ajaxIndicator.css('left', offset.left + bookTitle.find('a').width() + 10 + 'px');
        ajaxIndicator.css('top', offset.top + Math.floor(bookTitle.height() / 2) - 8 + 'px');
        ajaxIndicator.show();
	});
}); })(jQuery);