if( isUndefinedOrNull(Widget) ){
    var Widget = {};
}

Widget.Slideshow = function(container_id_or_element, options) {
    if( typeof options != 'object' ){
        options = {};
    }
    this.options = options;
    this.container = $(container_id_or_element);
    this.getImages();
    this.current_image = 0;
    this.timeout_delay = 10000;
    if( this.options['timeout'] ){
        this.timeout_delay = this.options['timeout'];
    }

    if( this.images.length > 0 ){
		this.timeout = setTimeout( bind(this.doSlide, this), this.timeout_delay / 3 );
    }
    this.i = 0;
}

Widget.Slideshow.prototype = {
    getImages : function() {
        var images = this.container.getElementsByTagName('IMG');
        this.images = new Array();
        for( var i = 0; i < images.length; i ++ ){
            this.images.push(images[i]);
        }
    },

    doSlide : function() {
        var newImgIndex = this.current_image + 1;
        if(newImgIndex >= this.images.length)
            newImgIndex = 0;
        this.fadeImageFromIndex(newImgIndex);
    },

    fadeImageFromIndex : function(index) {
        // console.log( "Adding fade:", this.i, this.timeout_delay );
        this.i ++;
        if( index < 0 || index >= this.images.length ){
            index = 0;
        }

        new Effect.Fade( this.images[this.current_image], {queue:'end'} );
        new Effect.Appear( this.images[index], {queue:'end'} );
        this.current_image = index;

        this.timeout = setTimeout( bind(this.doSlide, this), this.timeout_delay );
    }
}

