var Fader = new Class({
    Implements: [Options],
    
    options: {
        duration: 600,
        delay: 16,
        splash:'splash',
        image_set:null
        },
    
    initialize: function (options){
        this.setOptions(options);
        
        if (this.options.image_set)
        this.options.image_set.each(
        function(image){
            var img = new Element('img', {'alt':image.fields.description, 'id':image.fields.link, 'title':image.fields.title, 'class':'splash_image', 'src':'/media/' + image.fields.image}).inject(this.options.splash);
        }.bind(this));
        
        this.images = $$('.splash_image');
        this.index = 0;

        this.current_image = this.images[this.index];
        this.current_image.setStyle('opacity','1');
        $('splash_link').set('href', this.current_image.id);

        
        this.sales_link = new Element('a', {'id':'pitch_wrap', 'href':this.current_image.getProperty('id')}).inject(this.options.splash);
        this.sales = new Element('div', {'id':'pitch'}).inject(this.sales_link);
        this.sales_title = new Element('div', {'id':'sales_title', 'html':this.current_image.getProperty('title')}).inject(this.sales);

        this.changes.bind(this).delay(this.options.delay * 1000);
        },
        
    changes:function(options){
    
        if (this.index+1 != this.images.length)
            this.index++;
        else
            this.index = 0;

        next_image = this.images[this.index];

        var change_out_image = new Fx.Tween(this.current_image,  {'duration':this.options.duration, 'transition':'quad:in:out'});
        var change_in_image = new Fx.Tween(next_image, {'duration':this.options.duration, 'transition':'quad:in:out'});
        
        this.sales_link.setProperty('href', next_image.getProperty('id'));
        this.sales_title.set('html', next_image.getProperty('title'));

        change_out_image.start('opacity','0');
        next_image.setStyle('opacity', '0');
        change_in_image.start('opacity','1');
        $('splash_link').set('href', next_image.id);
        this.current_image = next_image;
        this.changes.bind(this).delay(this.options.delay*1000);
           

        }
    });
