var ImageShower = Class.create();
ImageShower.prototype = {
    
    initialize: function(images, targetObject, options) {
        
        this.options = {
            interval: 6000,
            displayCount: 1,
            margin: 0,
            randomSwitch: false
        };
        Object.extend(this.options, options || {});
        
        this.images = images;
        this.targetObject = $(targetObject);
        
        if (this.options.displayCount > this.targetObject.count) {
            this.options.displayCount = this.targetObject.count;
        }
        
        this.imageObjects = [];
        
        // objecten aanmaken
        this.images.each(function(image) {
            
            if (typeof(image) == 'string') {
                image = {src: image};
            }
            
            var img = document.createElement('IMG');
            img.src = image.src;
            img.style.display = 'none';
            
            if (image.title) {
                img.title = image.title;
            }
            if (image.width) {
                img.width = image.width;
            }
            if (image.height) {
                img.height = image.height;
            }
            if (this.options.margin) {
                img.style.marginRight = this.options.margin+'px';
            }
            
            if (image.link) {
                
                var a = document.createElement('A');
                a.href = image.link;
                a.target = '_blank';
                
                a.appendChild(img);
                
                this.targetObject.appendChild(a);
            } else {
                this.targetObject.appendChild(img);
            }
            
            this.imageObjects.push(img);
            
        }.bind(this));
        
        this.historyBuffer = [];
        
        if (this.imageObjects.length > 1) {
            window.setInterval(this.doSwitch.bind(this), this.options.interval);
        }
        this.doShow();
    },
    
    doSwitch: function() {
        
        if (!this.currentImages) {
            return;
        }
        
        var effects = [];
        
        this.currentImages.each(function(image) {
            effects.push(
                new Effect.Fade(image, {
                    duration: 2.0
                })
            );
        }.bind(this));
        
        new Effect.Parallel(effects, {
            beforeFinish: this.doShow.bind(this)
        });
    },
    
    doShow: function() {

        var effects = [];
        this.currentImages = [];
        
        for (var i = 0; i < this.options.displayCount; i++) {
        
            if (this.historyBuffer.length == this.imageObjects.length) {
                this.historyBuffer = [];
            }

            var selectedImage = (this.options.randomSwitch) ?
                this.imageObjects.withoudArray(this.historyBuffer).rand() :this.imageObjects.withoudArray(this.historyBuffer)[0];
            
            this.currentImages.push(selectedImage);
            this.historyBuffer.push(selectedImage);
            
            effects.push(
                new Effect.Appear(selectedImage, {
                    duration: 2.0
                })
            );
        
        }
        
        new Effect.Parallel(effects);
    }
};

Array.prototype.rand = function() {
    return this[Math.round((this.length - 1) * Math.random())];
};

Array.prototype.withoudArray = function(array) {
    return this.select(function(value) {
        return !array.include(value);
    });
};
