if (!App) {
    var App = {};
}

App.ImageJuggler = function(element, images)
{
    this._element    = $(element);
    this._images     = images;
    this._currentIdx = 0;
};

App.ImageJuggler.prototype.start = function(delay)
{
    var cache = [];
    for (var i = this._images.length; i--;) {
        var cacheImage = document.createElement('img');
        cacheImage.src = this._images[i];
        cache.push(cacheImage);
    }

    var thizz = this;
    setInterval(
        function() {
            thizz.switchToNext();
        },
        delay
    );
};

App.ImageJuggler.prototype.switchToNext = function()
{
    this.switchTo(this._currentIdx + 1);
};

App.ImageJuggler.prototype.switchTo = function(idx)
{
    if (idx >= this._images.length || idx < 0) {
        idx = 0;
    }
    this._currentIdx = idx;
    
    this._element.css('background-image', "url('" + this._images[this._currentIdx] + "')");
};
