jQuery.fn.slider = function( options ) {

    var settings = {
        // Since 0.0.1
        wait: 4000,
        fade: 1000,
        showPosition: true,
        // Since 0.0.2
        direction: 'left',
        showControls: false,
        // Since 0.0.3
        showText: true,
        // Since 0.0.4
        beforeSlide: function() {},
        afterSlide: function() {},
        onRewind: function() {},
        hoverPause: false
    };

    if (typeof options == 'object')    {
        if (typeof options.showPosition != 'undefined') settings.showPosition = (options.showPosition == true) ? true : false;
        if (typeof options.wait != 'undefined') settings.wait = options.wait;
        if (typeof options.fade != 'undefined') settings.fade = options.fade;
        if (typeof options.direction != 'undefined') settings.direction = options.direction;
        if (typeof options.showControls != 'undefined') settings.showControls = (options.showControls == true) ? true : false;
        if (typeof options.showText != 'undefined') settings.showText = (options.showText == true) ? true : false;
        if (typeof options.beforeSlide == 'function') settings.beforeSlide = options.beforeSlide;
        if (typeof options.afterSlide == 'function') settings.afterSlide = options.afterSlide;
        if (typeof options.onRewind == 'function') settings.onRewind = options.onRewind;
        if (typeof options.hoverPause != 'undefined') settings.hoverPause = (options.hoverPause == true) ? true : false;
        if (settings.fade < 1) settings.fade = 1000;
        if (settings.wait <= settings.fade) settings.wait = settings.fade * 1.25;
    }

    var _timer = false;
    var elements = [];
    var _inner = false;
    var _outer = false;
    var _pos = false;
    var _pos2 = false;
    var _text = false;
    var _text2 = false;
    var _previous = false;
    var _next = false;
    var $this = $(this);
    var t = false;
    var _position = 0;
    var i = 0;
    var _width = this.width();

    var c = function(d, x) {

        if (typeof d == 'undefined' || d == 0) d = (settings.direction == 'left') ? 1 : -1;
        if (typeof x == 'undefined' || x == 0) x = false;
        if (Math.abs(d) != 1) d = parseInt(d / Math.abs(d));
        if (d == 0) d = -1;
        clearTimeout(_timer);
        _position += d;

        if (_position < 0) {
            _position = elements.length - 1;
            settings.onRewind();
        }
        if (_position >= elements.length) {
            _position = 0;
            settings.onRewind();
        }
        var el = elements[_position];
        sd((_position + 1) + ' / ' + elements.length);

        if (!(i % 2)) {
            settings.beforeSlide();
            $(_outer).stop(true, true).css({'background-image': $(el).css('background-image'), 'left': d * _width});
            $(_inner).stop(true, true).css('left', 0);
            $(_outer).stop(true, true).animate({left: '-='+ d * _width +'px'}, {duration: settings.fade, complete: function() {settings.afterSlide()}});
            $(_inner).stop(true, true).animate({left: '-='+ d * _width +'px'}, {duration: settings.fade});
        } else {
            settings.beforeSlide();
            $(_inner).stop(true, true).css({'background-image': $(el).css('background-image'), 'left': d * _width});
            $(_outer).stop(true, true).css('left', 0);
            $(_inner).stop(true, true).animate({left: '-='+ d * _width +'px'}, {duration: settings.fade, complete: function() {settings.afterSlide()}});
            $(_outer).stop(true, true).animate({left: '-='+ d * _width +'px'}, {duration: settings.fade});
        }

        i++;

        if (!x && !$this.hasClass('slider-paused'))
            _timer = setTimeout(c, settings.wait);
    }

    var sd = function(t) {
        var el = $(elements[_position]);
        _pos.text(t);
        _pos2.text(t);

        _text.text(el.text());
        _text2.text(el.text());
    }

    elements = this.children();
    this.html(' ').css({'overflow': 'hidden', 'position': 'relative'});

    if (settings.hoverPause) {
        this.bind({
            'mouseenter': function() {
                clearTimeout(_timer);
                $(this).addClass('slider-paused');
            },
            'mouseleave': function() {
                $(this).removeClass('slider-paused');
                if (elements.length > 1)
                    _timer = setTimeout(c, settings.wait);
            }
        });
    }

    t = $('<div></div>').addClass('slider-position').css('width', this.width());
    _pos = t.clone().addClass('slider-position-overlay').css({'right': '6px', 'bottom': '1px'});
    _pos2 = t.clone().addClass('slider-position-shadow').css({'right': '5px', 'bottom': '0px', 'color': '#000'});

    t = $('<a></a>').addClass('slider-control');
    _previous = t.clone().addClass('slider-control-previous').html('&laquo;').bind('click', function() { c(-1, true); });
    _next = t.clone().addClass('slider-control-next').html('&raquo;').bind('click', function() { c(1, true); });

    t = $('<div></div>').addClass('slider-text');
    _text = t.clone().addClass('slider-text-overlay').text('');
    _text2 = t.clone().addClass('slider-text-shadow').text('');

    t = $('<div></div>').addClass('slider-image').css({'display': 'block', 'position': 'absolute', 'width': this.width(), 'height': this.height(), 'background-color': 'transparent', 'background-repeat': 'no-repeat', 'background-position': '50% 50%'});
    _inner = t.clone().css('background-image', $(elements[_position]).css('background-image'));
    _outer = t.clone().css('left', _width);

    this.append(_inner).append(_outer);

    if (settings.showPosition == true)
        this.append(_pos2).append(_pos);

    if (settings.showControls == true)
        this.append(_previous).append(_next);

    if (settings.showText == true)
        this.append(_text2).append(_text);
    
    sd((_position + 1) + ' / ' + elements.length);

    if (elements.length > 1)
        _timer = setTimeout(c, settings.wait);
};


