/*
Image collection
2008 - Magic Wand
*/


ImageCollection = function(images, rand_order) {
    this.images = [];
    this.pos = 0;
    
    this.init(images, rand_order);
}

ImageCollection.prototype = {
    init: function(images, rand_order) {
        this.addImages(images);
        if(rand_order) this.doRandomOrder();
    },
    
    /**
    * Adds images to a collection.
    * Image array format:
    * images = [{ alt: alt, src: image_src}, { ... }]
    */
    addImages: function(images) {
        for(var i in images) {
            if(!images[i]['src'] instanceof String)
                continue;
            var image = new Image();
            image.src = images[i]['src'];
            var alt = images[i]['alt'] ? images[i]['alt'] : '';
            this.images.push({
                alt: alt,
                image: image
            });
        }
    },
    
    doRandomOrder: function() {
        this.images.sort(function(a, b) {
          return 2 - Math.random() * 4;
        });
    },
    
    length: function() {
        return this.images.length;
    },
    
    current: function() {
        if(this.length() == 0)
            return false;
        return this.images[this.pos];
    },
    
    next: function() {
        if(this.pos >= this.length() - 1)
            return false;
        return this.images[++ this.pos];
    },
    
    rewind: function() {
        this.pos = 0;
    }
}
