/*
Image autochanger
2008 - Magic Wand
*/

/**
* @param ImageDelay images image collection
* @param delay it's delay between image showing
*/
ImageAutochanger = function(images, selector, delay, speed) {
    this.selector = selector;
    this.images = images;
    this.delay = delay;
    this.speed = speed;
    
    this.init();
}

ImageAutochanger.prototype = {
    
    init: function() {
        if(this.images.length() > 1)
            this.startTimer();
    },
    
    startTimer: function() {
        var _this = this;
        setTimeout(function() { _this.doChange(); }, this.delay);
    },
    
    doChange: function() {
        var _this = this;
        $(this.getEl()).fadeOut(this.speed, function() {
            var image = _this.images.current();
            if(!image) return;
            if(!_this.images.next())
                _this.images.rewind();
            _this.getEl().attr('src', image.image.src)
                         .attr('alt', image.alt)
                         .attr('title', image.alt)
                         .fadeIn(_this.speed, function() { _this.startTimer(); });
        });
    },
    
    getEl: function() {
        return $(this.selector);
    }
}

