var bgfader = {
    init: function(options) {
        this.options = options;

        $.each(options.images, function() {
            (new Image()).src = this;
        });

        var target = $(options.target);
        target.css({
            'position': 'relative'
        });
        target.append('<div class="inner_fader" style="height: 100%;"></div>');
        this.timer();
    },

    timer: function() {
        this.shownext();

        var self = this;
        setTimeout(function() { self.timer() }, this.options.interval);
    },

    shownext: function() {
        var nextidx;
        if (this.idx == undefined) {
            nextidx = 0;
        } else {
            nextidx = this.idx + 1;
        }
        if (nextidx >= this.options.images.length) nextidx = 0;

        $('.slide', this.options.target).fadeOut(this.options.fadetime, function() {
            $(this).remove();
        });

        $('div.inner_fader', this.options.target).prepend('<div class="slide"></div>');
        var newslide = $('.slide:first', this.options.target);
        newslide.css({
            'position': 'absolute',
            'width': '100%',
            'height': '100%',
            'display': 'none',
            'background': 'no-repeat ' + this.options.position + ' url(' + this.options.images[nextidx] + ')'
        });
        newslide.fadeIn(this.options.fadetime);

        this.idx = nextidx;
    }
};


