/**
 * based on jCarouselLite by Ganeshji Marwaha/ganeshread@gmail.com
**/

(function($) {
    $.fn.carousel = function(cfg) {
        cfg = $.extend({
            btnPrev: null,
            btnNext: null,
            btnGo: null,
            speed: 200,
            easing: null,

            visible: 3,
            scroll: 3
        }, cfg || {});

        return this.each(function() {
            var running = false,
                div = $(this),
                ul = $("ul", div),
                li = $("li", ul),
                total_items = li.size(),
                current_pos = 0,
                li_width = $('.mg-series-shelf .mg-book').outerWidth(true),
                ul_width = li_width * total_items;
                div_width = li_width * cfg.visible;

            li.css({
                'overflow': "hidden",
                'float': "left"
            });

            ul.css({
                'margin': "0",
                'padding': "0",
                'position': "relative",
                "list-style-type": "none"
            });

            div.css({
                'visibility': "visible",
                'overflow': "hidden",
                'position': "relative",
                'left': "0px",
                'width': div_width + "px"
            });


            li.css({
                'width': li_width
            });

            ul.css({
                'width': ul_width + "px",
                'left': "0"
            });

            if (total_items > cfg.visible) {
                $(cfg.btnPrev).click(function(event) {
                    event.preventDefault();
                    var pos = current_pos - cfg.scroll

                    if (pos < 0) {
                        pos = 0;
                    }
                    return seek_to(pos);
                }).show();

                $(cfg.btnNext).click(function(event) {
                    event.preventDefault();
                    var pos = current_pos + cfg.scroll

                    if (pos + cfg.visible > total_items) {
                        pos = total_items - cfg.visible;
                    }
                    return seek_to(pos);
                }).show();

                if(cfg.btnGo)
                    $.each(cfg.btnGo, function(i, val) {
                        $(val).click(function() {
                            return seek_to(i);
                        });
                    });
            } else {
                $(cfg.btnPrev).hide();
                $(cfg.btnNext).hide();
            }

            function seek_to(pos) {
                if(running) {
                    return false;
                }

                if (pos < 0 || pos > total_items) {
                    return;
                }

                current_pos = pos

                running = true;

                var left = -(current_pos * li_width)
                ul.animate({'left': left}, cfg.speed, cfg.easing,
                    function() {
                        running = false;
                    }
                );

                return false;
            };
        });
    };
    $(function() {
        var dialog_div = $('<div id="mg-series-dialog" style="display:none;">');
        dialog_div.html('<table id="mg-series-table"><tr>' +
                '<td width="41"><a href="#" class="mg-series-prev" style="display:none">&nbsp;</a></td>' +
                '<td><div id="carousel"><div id="carousel_holder" style="height:362px;"><img src="/images/spinners/bar.gif" alt="" /></div></div></td>' +
                '<td width="41"><a href="#" class="mg-series-next" style="display:none">&nbsp;</a></td>' +
                '</tr></table>');
        $('body', document).append(dialog_div);
        $(dialog_div).dialog({
            modal: true,
            resizable: false,
            draggable: false,
            autoOpen: false,
            width: 800,
            height: 400,
            dialogClass: 'mg-dialog mg-series-dialog',
            close: function(event, ui) {
            }
        });

        $('._series').bind('click', function(event) {
            event.preventDefault();
            var series_id = $(this).parse_class('_bseries');
            $.get('/book/ajax/get_series/', {'series': series_id},
                function(response) {
                    var resp = $(response);
                    var title = $('div.__series_title', resp);
                    $(dialog_div).dialog('option', 'title', title.html());
                    title.remove();
                    $("#carousel_holder").html(resp);
                    $("#carousel_holder").show();
                    $('#carousel').carousel({
                        'btnNext': "#mg-series-dialog .mg-series-next",
                        'btnPrev': "#mg-series-dialog .mg-series-prev",
                        'visible': 3,
                        'scroll': 3
                    });
                }
            );
            $(dialog_div).dialog('open');
        });
    });
})(jQuery);