(function($) {

    $.fn.easySlider = function(options){
      
        // default configuration properties
        var defaults = {
                prevText    :'Previous',
                nextText    :'Next',
                speed       :'normal',
                delay       :3500,
                autostart   :true,
                pause       :'PausePlay',
                pauseText   :['Slideshow pause', 'Slideshow continue']
            },
            options = $.extend(defaults, options);
        
        // if you want to change these, you have to change
        // the action-binding at the end of the script
        options.next = 'Btn next';
        options.prev = 'Btn prev';
                
        this.each(function() {
            
            var active = options.autostart ? true : false,
                
                $slider = $(this),
                $wrapper = $('div', $slider),
                $list = $('ul', $wrapper),
                $items = $('li', $list),
                
                height = $items.eq(0).height(),
                width = $wrapper.width(),
                
                $prev = $('<a>')
                            .attr({'href': '#', 'class': options.prev})
                            .text(options.prevText),
                            
                $next = $('<a>')
                            .attr({'href': '#', 'class': options.next})
                            .text(options.nextText),
                            
                $pause = $('<a>')
                            .attr({'href': '#', 'class': options.pause})
                            .text(active ? options.pauseText[0] : options.pauseText[1]),
                
                $PlayPause,
                
                currentPage = 1,
                pages = $items.length,
                gotoPage;
            
            // apply styles
            $wrapper.height(height);
            $list.css({'position': 'absolute', 'top': 0});
            $items.css({'float': 'left', 'width': width});
            
            // clone and append first and last li
            $items.filter(':first').before($items.eq($items.length - 1).clone().addClass('cloned'));
            $items.filter(':last').after($items.eq(0).clone().addClass('cloned'));
            
            // re-select $items (because we appended)
            $items = $('li', $list);
            
            // calculate new width of the ul and assign
            $list.width($items.length * width);
            
            // set left to -width, to show the first picture
            $list.css('left', -width);
            
            
            // paging function
            gotoPage = function (page) {
                var direction = page < currentPage ? -1 : 1,
                    n = Math.abs(currentPage - page),
                    left = width * direction * n;
                
                // if not animated, animate (to prevent multiple clicks)
                $list.filter(':not(:animated)').animate({
                    left: '-=' + left
                }, options.speed, function () {
                    if(page === 0) {
                        // reset to last picture
                        $list.css('left', -width * pages);
                        page = pages;
                    }else if(page > pages) {
                        // reset to first picture
                        $list.css('left', -width);
                        page = 1;
                    }
                    
                    // set currentPage to the current page
                    currentPage = page;
                });
            };
            
            // append elements and refresh
            $slider.append($prev, $pause, $next);
            $slider = $(this);
            
            // bind actions
            $slider.find('.next').click(function () {
                gotoPage(currentPage + 1);
                return false;
            });
            
            $slider.find('.prev').click(function () {
                gotoPage(currentPage - 1);
                return false;
            });
            
            $PlayPause = $slider.find('.' + options.pause);
            
            $slider.bind('next', function () {
                gotoPage(currentPage + 1);
            });
            
            
            setInterval(function () {
                if(active) {
                    $slider.trigger('next');
                }
            }, options.delay);
            
            
            $PlayPause.click(function () {
                if(active) {
                    active = false;
                    $(this).text(options.pauseText[1]);
                } else {
                    active = true;
                    $(this).text(options.pauseText[0]);
                }
                return false; 
            });
            
        });
        
        // return the instance to enable chaining
        return this;
        
    };

})(jQuery);
