(function($) {
    // Code goes here
    $.fn.sliderB=function(opts)
    {
        var defaults={
           orientation:'horizontal', // 'horizantal', 'vertical
           per_page:3,
           ext_offset:0, //Дополнительный оффсет если есть падинг или марджин
           cycle:false,
           nextButton:false,
           prevButton:false,
           onSlideComplete:null,
           interval:6000,
           paging:false
        };
        var options=$.extend(defaults, opts);
        this.each(
            function(){
                var paging=options.paging==false?$(this).find('.paging a'):options.paging;
                var $this=$(this);
               // paging.parent().width(paging.first().width()*(paging.length+12));
                paging.first().addClass('active');
                var next=[];
                var list=$this.find('ul');
                var interval=0;
                var li_width=0;
                if (options.orientation=='horizontal')
                {
                    list.find('li').each(function(){
                        li_width+=$(this).width()+options.ext_offset;
                    });

                    list.width(li_width);
                }

                

                var slide=function(e){
                        e!=null && e.preventDefault();
                        paging.removeClass('active');
                        next.addClass('active');
                        var index=paging.index(next);

                        if (index>parseInt(list.children('li').length/options.per_page))
                        {

                            return false;
                        }
                        var offset=index*options.per_page;
                        var offset_css="margin-left";
                        switch(options.orientation) {
                            case "vertical":
                                offset*=-(list.children('li').first().height()+options.ext_offset);
                                offset_css="margin-top";

                            break;
                            default:
                                offset*=-(list.children('li').first().width()+options.ext_offset);
                            break;
                        }


                        //var offset=-(list.children('li').first().width()*index*3);
                       // list.css('margin-left',offset+"px");

                       list.queue(function(){
                            var css={};
                            css[offset_css]=offset+"px";
                            list.animate(
                                css, options.onSlideComplete
                            );
                            $(this).dequeue();
                       });



                    }
                if (options.nextButton!=false)
                {
                    options.nextButton.click(
                        function(e){

                            next=paging.filter('.active').next();
                            if (next.length==0)
                                if (options.cycle) next=paging.first();
                                else next=paging.filter('.active');

                        }
                    );
                    options.nextButton.click(slide);
                }


                if (options.prevButton!=false)
                {
                    options.prevButton.click(
                        function(e){
                            next=paging.filter('.active').prev();
                            if (next.length==0)
                                if (options.cycle) next=paging.first();
                                else next=paging.filter('.active');
                        }
                    );
                    options.prevButton.click(slide);
                }

                paging.click(
                    function(e){
                        next=$(this);
                    }
                );

                paging.click(slide);




                if (options.cycle)
                {
                    interval=setInterval(function(){
                        if (list.queue().length===0)
                        {
                            next=paging.filter('.active').next();
                            if (next.length==0) next=paging.first();
                            slide(null);
                        }
                    },options.interval)
                }

                list.mouseenter(
                    function(){
                        clearInterval(interval);
                        interval=0;
                    }
                ).mouseleave(
                    function(){
                        if (options.cycle && interval==0)
                        {
                            interval=setInterval(function(){
                                if (list.queue().length===0)
                                {
                                    next=paging.filter('.active').next();
                                    if (next.length==0) next=paging.first();
                                    slide(null);
                                }
                            },options.interval)
                        }
                    }
                )


            }
        );
    }
})(jQuery);
