var Slider = {
    objects     : [],
    addObject   : function(el) {
        this.objects[this.objects.length] = el;
    },
    maxwidth    : 0,
    maxheight   : 0,
    space       : 10,
    currentIndex : 0,
    numelements : 0,
    init        : function(numelements, find) {
        this.numelements = numelements;
        if (!$defined(find))
            find = 'Slider';
            this.find = find;

        if ($defined($$('.' + find)[0])) {
            this.storeholder = $$('.' + find)[0];
            this.storeholder.setStyle('overflow', 'hidden');
            this.store = new Element('div').inject(this.storeholder);

            for(i = 0; i < numelements; i++) {
                this.createElement(i, false);
                this.currentIndex = i;
            }

        }

        this.startShow();
    },
    createElement : function(objectsindex, fancywidth) {
        objectsindex = objectsindex % this.objects.length ;

        if (!$defined(fancywidth)) {
            fancywidth = false;
        }

        var holder = new Element('div', { 'class' : 'block', styles : { width : 0}});

        this.objects[objectsindex].inject(holder);
        holder.inject(this.store);

        if (this.objects[objectsindex].getWidth() > this.maxwidth)
            this.maxwidth = this.objects[objectsindex].getWidth();


        if (this.objects[objectsindex].getHeight() > this.maxheight)
            this.maxheight = this.objects[objectsindex].getHeight();

        if (this.hasOption('autoformat') && !fancywidth) {
            $$('.' + this.find + ' .block').each(function(e) {
                e.setStyle('width', this.maxwidth); e.setStyle('height', this.maxwidth);

                if (Slider.hasOption('horizontal')) {
                    e.setStyle('float', 'left');
                    e.setStyle('margin-right',  Slider.space);
                }
            });
        } else if (fancywidth) {
            holder.set('tween', {duration : 'long'});
            holder.tween('width', this.maxwidth);

            if (Slider.hasOption('horizontal')) {
                holder.setStyle('float', 'left');
                holder.setStyle('margin-right', Slider.space);
            }
        }

        if (Slider.hasOption('horizontal')) {
            this.store.setStyle('height',   this.maxheight);
            this.store.setStyle('width',    this.maxwidth * (this.numelements + 1) + (Slider.hasOption('horizontal') ? this.space * (this.numelements + 1) : 0));
            this.storeholder.setStyle('width', this.maxwidth * (this.numelements) + (Slider.hasOption('horizontal') ? this.space * (this.numelements) : 0));
        }
    },
    hasOption   : function(option) {
        return this.storeholder.hasClass(option);
    },
    startShow   : function() {

        if (this.hasOption('horizontal')) {
            var element = this.store.getElement('.block');
            element.set('tween', {duration: 'long'})
            element.setStyle('margin-right', 0);
            element.tween('width', 0);
            setTimeout('Slider.store.getElement(".block").destroy()', 1500);

        }
        this.currentIndex++;
        this.createElement(this.currentIndex, true);
        setTimeout('Slider.startShow()', 5000);
    },
    setSpace    : function (inpx) {
        this.space = inpx;
    }
}
