(function($) {
    $.fn.extend({
        slider: function(settings) {
            var config = {
                items: 5,
                start: 0,
                left: '.left',
                right: '.right',
                duration: 'slow',
                mouseMove: true,
                onStart: function() {},
                onChange: function() {},
                onClick: function() {}
            };

            if (settings) {
                $.extend(config, settings);
            }

            return this.each(function() {
                var me = $(this);
                var parent = me.find('ul');
                var items = parent.find('li');
                var step = 0;
                var max = Math.ceil(items.size() / config.items) - 1;
                var ct = 1;

                var methods = {

                    init: function() {
                        methods.normalize();

                        $(config.left).click(methods.moveLeft);
                        $(config.right).click(methods.moveRight);

                        methods.bindClick();
                        methods.start();

                        if (config.mouseMove) {
                            methods.moveable();
                        }
                    },

                    normalize: function() {
                        me.css("width", 104 * config.items);    
                        me.css("overflow", "hidden");
                        parent.css("width", 104 * items.size());
                        items.css("float", "left");
                    },

                    moveable: function() {
                        parent.mousedown(function(e) {
                            var inicio = e.pageX;

                            parent.mousemove(function(e) {
                                var posicao = e.pageX;

                                if (posicao < inicio) {
                                    methods.moveRight();
                                    $(this).unbind('mousemove');
                                }
                                else if (posicao > inicio) {
                                    methods.moveLeft();
                                    $(this).unbind('mousemove');
                                }
                            });

                        }).mouseup(function() {
                            $(this).unbind('mousemove');
                        });
                    },

                    moveLeft: function() {
                        if (step > 0) {
                            me.animate({
                                'scrollLeft': (--step * (items.outerWidth(true) * config.items))
                            }, config.duration, config.onChange);
                        }
                    },

                    moveRight: function() {
                        if (step < max) {
                            me.animate({
                                'scrollLeft': (++step * (items.outerWidth(true) * config.items))
                            }, config.duration, config.onChange);
                        }
                       /* else {
							if(max>0){
								ct++;
								var clone = items.clone();
								parent.css("width", items.outerWidth(true) * (items.size() * ct));
								parent.append(clone);
								max = Math.ceil((items.size() * ct) / config.items) - 1;

								me.animate({
									'scrollLeft': (++step * (items.outerWidth(true) * config.items))
								}, config.duration, config.onChange);
							}
                        }*/
                    },

                    findStep: function(index) {
                        var item = Math.floor(index / config.items) - 1;
                        return item;
                    },

                    start: function() {
                        step = methods.findStep(config.start);
                        me.scrollLeft(++step * (items.outerWidth(true) * config.items));

                        config.onStart(items.eq(config.start));
                    },

                    bindClick: function() {
                        parent.unbind('mousemove');
                        
                        items.live('click', function() {
                            config.onClick($(this));
                        });
                    }
                };

                methods.init();
            });
        }
    });
})(jQuery);

