var BASKET_AUTO_START = true;
(function($) {
    $.pkg('mg.util.Basket');

    mg.util.Basket = function(cfg) {
        this.init(cfg);
    };

    mg.util.Basket.prototype = {
        init: function(cfg) {
            cfg = $.extend({
                'callback': this.callback,
                'callback_free': this.callback_free
            }, cfg || {});

            $('._add_to_basket').live('click',
                function(event) {
                    event.preventDefault();
                    var id = $(this).parse_class("_book");
                    $.post('/basket/add/', {"book": id},
                        function(response) {
                            $('#_basket_holder').html(response.full).show();
                            $('#mg-add-basket').html(response.header).show();
                            cfg.callback(id);
                        }, "json"
                    );
                }
            );
            $('._btn_free').live('click',
                function(event) {
                    event.preventDefault();
                    var id = $(this).parse_class("_book");
                    $.post('/basket/free/', {'book': id}, function(response) {
                        cfg.callback_free(id);
                    });
                }
            );
        },
        callback: function(book_id) {
            var book = $('._div_book_' + book_id);
            var span = $('<span>').html($._("Is in basket"));
            book.addClass('mg-book-inbasket');
            $('.mg-book-statusbar', book).html(span);
        },
        callback_free: function(book_id) {
            var book = $('._div_book_' + book_id);
            var span = $('<span>').html($._("Got it"));
            book.addClass('mg-book-purchased');
            $('.mg-book-statusbar', book).html(span);
        }
    };



    $(document).ready(
        function() {
            if (BASKET_AUTO_START === true) {
                var basket = new mg.util.Basket();
            }
        }
    );
}(jQuery));