/**
 * Auto gallery
 * 
 * @copyright 2009 MagicWand
 * @version $Id: auto_gallery.js 402 2009-07-03 21:43:11Z  $
 */
AutoGallery = function(images, gallery_selector, template_selector, count, delay, speed, lag) {
    this.images = images;
    this.real_images = null;
    this.gallery_selector = gallery_selector;
    this.count = count;
    this.delay = delay;
    this.speed = speed;
    this.lag = lag;
    
    this.init(template_selector);
}

AutoGallery.prototype = {
    init: function(template_selector) {
        this.buildGallery(template_selector);
        if(this.images.length() > this.count)
            this.startTimer();
    },
    
    buildGallery: function(template_selector) {
        var images = this.getNextImages();
        var template = $(template_selector);
        var gallery = this.getGalleryEl();
        for(var i in images) {
            var image = images[i];
            if(image.image) {
                var gallery_elem = template.clone().css('display', 'block').appendTo(gallery);
                var real_image = $('.pict', gallery_elem);
                this._setupImage(real_image, image);
            }
        }
        this.real_images = $('.pict', gallery);
    },
    
    startTimer: function() {
        var _this = this;
        setTimeout(function() { _this.onTimeout(); }, this.delay);
    },
    
    onTimeout: function() {
        this.doChange();
    },
    
    doChange: function() {
        var images = this.getNextImages(true);
        var _this = this;
        var delay = 0;
        this.real_images.each(function(i) {
          var on_change = 
            i == _this.real_images.length - 1 
            ? function() { _this.startTimer(); }
            : undefined;
          _this._initImageChanger($(this), images[i], delay, on_change);
          delay += _this.lag;
        });
    },
    
    _initImageChanger: function(real_image, image, delay, on_change) {
        if(delay) {
            var _this = this;
            setTimeout(function() { _this.onChangeBegin(real_image, image, on_change); }, delay);
        } else this.onChangeBegin(real_image, image, on_change);
    },
    
    onChangeBegin: function(real_image, image, on_change) {
        var _this = this;
        real_image.fadeOut(this.speed, function() {
           _this._setupImage(real_image, image);
           real_image.fadeIn(_this.speed, function() {
             if(on_change) on_change();
           });
        });
    },
    
    /**
    * Returns next images from a collection.
    * 
    * @param boolean close if it's true then add the first image to end
    */
    getNextImages: function(close) {
        var images = [];
        if(this.images.length() == 0)
            return;
        for(var i = 0; i < this.count; i ++) {
            images.push(this.images.current());
            if(!this.images.next()) {
                if(close) {
                    close = false;
                    this.images.rewind();
                } else break;
            }
        }
        return images;
    },
    
    _setupImage: function(real_image, image) {
        real_image.attr('src', image.image.src)
                  .attr('alt', image.alt.entityDecode())
                  .attr('title', image.alt.entityDecode());
    },
    
    getGalleryEl: function() {
        return $(this.gallery_selector);
    }
}
