var Slideshow = new Class({
        
    initialize: function(slides, container, delay) {
        
        this.currentIdx = 0;
        this.nextIdx = 0;
        this.fxIdx = 1;
        this.nextFxIdx = 0;
        
        container = $(container);
        
        if ($type(delay) == false) delay = 0; // Default start delay
        
        this.loader = container.getElement('.loading'); // Loader
        
        // Slide list and info/fx arrays
        
        this.slides = slides;
        this.fx = [];
        
        var preload = [];
        
        this.slides.each(function(item) {
            preload.push(item.img);
        });
        
        // Preload images
        
        new Asset.images(preload, {
            onComplete: function() {
                
                // Create two image elements
                
                var obj0 = {};
                
                var img0 = new Element('img', {'class': 'img-slide'});
                obj0.img = img0.injectInside(container);
                obj0.fx = new Fx.Style(obj0.img, 'opacity', {duration: 750}).set(0);
                
                this.fx[0] = obj0;
                
                if (this.slides.length > 1) {
                
                    var obj1 = {};
                
                    var img1 = new Element('img', {'class': 'img-slide'});
                    obj1.img = img1.injectInside(container);
                    obj1.fx = new Fx.Style(obj1.img, 'opacity', {duration: 750}).set(0);
                
                
                    this.fx[1] = obj1;
                    
                }
                
                this.begin.delay(delay, this); // Start slideshow
                
            }.bind(this)
        });
    },
    
    begin: function() {
        this.loader.setStyle('display', 'none');
        this.setImg();
        if (this.slides.length > 1) {
            this.fx[0].fx.start(1).chain(function(){
                this.nextImg.delay(4500, this);
            }.bind(this))
        } else { // Single image, just fade out loader and fade up first image
            this.fx[0].fx.start(1);
        }
    },
    
    nextImg: function() {
        this.currentIdx = this.nextIdx;
        if (this.currentIdx == (this.slides.length - 1)) {
            this.nextIdx = 0;
        } else {
            this.nextIdx = this.currentIdx + 1;
        }
        this.fxIdx = this.nextFxIdx;
        this.nextFxIdx = (this.fxIdx == 0) ? 1 : 0;
        this.setImg();
        this.cycle();
    },
    
    setImg: function() {
        
        // Clear link settings
        
        this.fx[this.nextFxIdx].img.removeClass('linked');
        this.fx[this.nextFxIdx].img.removeEvents('click');
        this.fx[this.nextFxIdx].img.removeEvents('over');
        this.fx[this.nextFxIdx].img.removeEvents('out');
        
        // Update properties
        
        this.fx[this.nextFxIdx].img.src = this.slides[this.nextIdx].img;
        this.fx[this.nextFxIdx].img.alt = this.slides[this.nextIdx].imgName;
        
        // Set linking if necessary
        
        if (this.slides[this.nextIdx].imgUrl != '') {
            this.fx[this.nextFxIdx].img.addClass('linked');
            this.fx[this.nextFxIdx].img.addEvent('click', function(evt) {
                document.location.href = this.slides[this.nextIdx].imgUrl;
            }.bind(this));
            this.fx[this.nextFxIdx].img.addEvent('over', function(evt) {
                this.fx[this.nextFxIdx].img.setStyle('cursor', 'pointer');
            }.bind(this));
            this.fx[this.nextFxIdx].img.addEvent('out', function(evt) {
                this.fx[this.nextFxIdx].img.setStyle('cursor', 'normal');
            }.bind(this));
        }
        
    },
    
    cycle: function() {
        
        // Set z index
        
        this.fx[this.nextFxIdx].img.setStyle('zIndex', '0');
        this.fx[this.fxIdx].img.setStyle('zIndex', '1');
        
        this.fx[this.nextFxIdx].fx.set(1);
        this.fx[this.fxIdx].fx.start(0).chain(function() {
            this.nextImg.delay(4500, this);
        }.bind(this));
    }
    
});