
/**************************************************************

	Script		: SlideShow
	Version		: 1.3
	Authors		: Samuel Birch
	Desc		: 
	Licence		: Open Source MIT Licence

**************************************************************/

var SlideShow = new Class({

    getOptions: function() {
        return {
            duration: 2000,
            transition: Fx.Transitions.linear,
            wait: 5000
        };
    },

    initialize: function(container, images, options) {
        this.setOptions(this.getOptions(), options);

        this.container = $(container);
        this.container.setStyles({
            position: 'relative',
            overflow: 'hidden'
        });

        this.imagesHolder = new Element('div').setStyles({
            position: 'absolute',
            overflow: 'hidden',
            top: this.container.getStyle('height'),
            left: 0,
            width: '0px',
            height: '0px',
            display: 'none'
        }).injectInside(this.container);

        if ($type(images) == 'string') {
            var imageList = [];
            $$('.' + images).each(function(el) {
                imageList.push(el.src);
                el.injectInside(this.imagesHolder);
            }, this);
            this.images = imageList;
        } else {
            this.images = images;
        }

        this.b = new Element('div').setStyles({
            position: 'absolute',
            overflow: 'hidden',
            top: 0,
            left: 0,
            opacity: 0,
            width: this.container.getStyle('width'),
            height: this.container.getStyle('height')
        }).injectInside(this.container);

        this.a = this.b.clone();
        this.a.injectInside(this.container);

        this.timer = 0;
        this.image = -1;
        this.stopped = true;
        this.started = false;
        this.animating = false;
        this.first = true;
    },

    load: function() {
        $clear(this.timer);
        this.image++;
        var img = this.images[this.image];
        delete this.imageObj;

        doLoad = true;
        this.imagesHolder.getElements('img').each(function(el) {
            var src = this.images[this.image];
            if (el.src == src) {
                this.imageObj = el;
                doLoad = false;
                this.add = false;
                this.show();
            }
        }, this);

        if (doLoad) {
            this.add = true;
            this.imageObj = new Asset.image(img, { onload: this.show.bind(this) });
        }
    },

    show: function() {
        if (this.add) {
            this.imageObj.injectInside(this.imagesHolder);
        }
        this.b.setStyles({
            zIndex: 1,
            opacity: 0
        });
        var img = this.b.getElement('img');
        if (img) {
            this.imageObj.clone().replaces(img);
        } else {
            var obj = this.imageObj.clone();
            obj.injectInside(this.b);
        }
        if (this.first == false) {
            this.effect(this.options.duration);
        } else {
           this.effect(0);
        }
        this.first = false;
    },

    wait: function() {
        this.timer = this.load.delay(this.options.wait, this);
    },

    play: function(num) {
        if (this.stopped) {
            if (num > -1) { this.image = num - 1 };
            if (this.image < this.images.length) {
                this.stopped = false;
                if (this.started) {
                    this.next();
                } else {
                    this.load();
                }
                this.started = true;
            }
        }
    },

    stop: function() {
        if (this.effectObj) {
            this.effectObj.cancel();
            this.effectObj.start(0);
        }
        $clear(this.timer);
        this.stopped = true;
        this.a.dispose();
        this.b.dispose();
    },

    next: function(wait) {
        var doNext = true;
        if (wait && this.stopped) {
            doNext = false;
        }
        if (this.animating) {
            doNext = false;
        }
        if (doNext) {
            this.swapImage();
            $clear(this.timer);
            if (this.image >= this.images.length - 1) {
                this.image = -1;
            }
            this.wait();
        }
    },

    swapImage: function() {
        if (this.a.getElement('img')) {
            this.a.setStyles({
                zIndex: 1,
                top: 0,
                left: 0,
                opacity: 0
            });
        }

        this.b.setStyles({
            zIndex: 0
        });

        this.c = this.a;
        this.a = this.b;
        this.b = this.c;
    },

    effect: function(duration) {
        this.animating = true;
        this.effectObj = new Fx.Tween(this.b, {
            property: 'opacity',
            duration: duration,
            transition: this.options.transition
        });

        this.effectObj.start(0, 1);
        this.resetAnimation.delay(this.options.duration + 90, this);
        if (!this.stopped) {
            this.next.delay(this.options.duration + 100, this, true);
        }
    },

    resetAnimation: function() {
        this.animating = false;
    }

});
SlideShow.implement(new Options);
SlideShow.implement(new Events);


/*************************************************************/

