/*
    Slideshow
    Version 1.0
    Creted by Cato: 27-11-2008
 
    This slideshow fades between a selection of photos

    Arguments:
        element (string) A string id of a html element where the slideshow will be injected.
        images (array) A string array of image urls.
        options - (object, optional) An object with options for the slideshow. See below.

    Options:
        timespan (int) the time between image swaps (default: 5000)

    Usage:
        var slideshow = new Slideshow('slideshow,' myImages {options});
     
    Todo in later versions:
        Add option of adding thumbnails
        Implement Transitions
 */

var Slideshow = new Class(
{
    Implements: Options,
    options: {
        timespan: 5000
    },
    
    initialize: function(div, images, options)
    {
        this.setOptions(options);

        this.lock = false;
        this.i = 0;
        this.div = $(div);
        this.images = images;
        
        this.el = new Element('img',
        {
            'src': images[this.i]     
        });
        
        this.el.inject(this.div);
        
        this.next.periodical(this.options.timespan, this);
        
    },
    
    next: function()
    {
        if(!this.lock)
        {
            this.lock = true;
            
            if(this.i != (this.images.length-1) )
            {
                this.i++;
            }
            else
            {
                this.i = 0;
            }
            $('slideshow').setStyle('background', 'url(' + this.images[this.i] + ') no-repeat');
            
            var fader = new Fx.Morph(this.el,
            {
                duration: 1000,
                transition: Fx.Transitions.Sine.easeOut     
            });
            
            fader.start(
            {
                'opacity': 0    
            });
        
            (function()
            {
                this.el.src = this.images[this.i];
            }).delay(1050, this);
            
            
            (function()
            {
                fader.set(
                {
                    'opacity': 1    
                });
            
            }).delay(2000);
            
            this.lock = false;
        }
    }
});

