(function($){
        $.fn.slider = function(options) {
                
                options = $.extend({
                        transitionTimer : 700,
                        timer : 5000
                }, options);
                
                var isRunning = false;
                var timerFunction = null;
                var currentIndex = 0;
                var inTransition = false;
                var pages = $(this);
                var width = $(options.container).width();
                
                $(options.pageContainer).width(width*pages.length);
                
                makeControls();
                timerFunction = setTimeout(function(){start();}, options.timer);
                
                function move(index) {
                        
                        
                        if (inTransition) return;
                        
                        if (timerFunction) {
                                clearTimeout(timerFunction);
                        }
                        
                        inTransition = true;
                        
                        var nextIndex = (index || index === 0) ? index : currentIndex + 1;
                        
                        if (nextIndex > pages.length -1) {
                                nextIndex = 0;
                        }
                        
                        var left = nextIndex * width;
                        
                        changeNav(nextIndex);
                        $(options.container).animate({
                               scrollLeft : left 
                        }, options.transitionTimer, function(){
                                currentIndex = nextIndex;
                                inTransition = false;
                                timerFunction = setTimeout(function(){move();}, options.timer);
                        });
                }
                
                function changeNav(index) {
                        $('#SliderControls li').removeClass('selected');
                        $('#SliderControls li#SliderNav' + index).addClass('selected');
                }
                
                function moveTo(index) {
                        return function(e) {
                                move(index);
                                e.preventDefault();
                        };
                }
                
                function makeControls() {
                        $(options.container).after(
                                $('<div id="SliderControls" class="controls"></div>').css({
                                        position : 'absolute',
                                        'z-index' : 3
                                })
                        );
                        
                        $('#SliderControls')
                                .append('<ul></ul>')
                                .append(
                                        $('<a href="#" class="toggle">Stop</a>').click(function(e){
                                                toggle();
                                                e.preventDefault();
                                        })
                                );
                        
                        for (var i = 0; i < pages.length; i++) {
                                $('#SliderControls ul').append(
                                        $('<li>&nbsp;</li>')
                                                .attr('id', 'SliderNav' + i)
                                                .attr('class', (i == 0) ? 'selected' : '')
                                                .click(moveTo(i))
                                )
                        }
                        
                }
                
                function toggle() {
                        if (isRunning) {
                                stop();   
                        }
                        else {
                                start();
                        }
                }
                
                function start() {
                        $('#SliderControls .toggle').html('Stop');
                        isRunning = true;
                        move();
                }
                
                function stop() {
                        $('#SliderControls .toggle').html('Start');
                        $(options.container).clearQueue();
                        $(options.container).stop();
                        clearTimeout(timerFunction);
                        isRunning = false;
                        inTransition = false;
                }
                
                
                
        };
})(jQuery);
