var SlideShow = new Class({
    
    Implements: Options,
    
    options: {
        timer: 1000,
        transition: 'Quad',
        currentImage: 0,
        delay: 4000
    },
    
    images: [],

    initialize: function(container, options) {
        
        if (!options) options = {};
        
        this.setOptions(options);
        this._container = container;
        this._elements = this._container.getChildren();
        this.options.transition = Fx.Transitions[this.options.transition].easeInOut;
        this._maxElements = this._elements.length-1;
        this._currentContainer = this._container.getElement('.current');
        this._currentContainerIndex = this._elements.indexOf(this._currentContainer);
        this._lastContainer = this._currentContainer;
        this._elements.each(function (item, index) {
            item.fx = new Fx.Tween(item, {'property': 'opacity', 'transition': this.options.transition, 'duration': this.options.timer});
            item.block = item.getElement('.block');
            item.block.fx = new Fx.Tween(item.block, {
                'duration': 750,
                'fps' : 50,
                'transition': this.options.transition,
                'property': (item.block.hasClass('slide-left')) ? 'left' : 'right'
            });
            item.block.setStyle('top', '20px');
            item.block.setStyle((item.block.hasClass('slide-left')) ? 'left' : 'right', '-400px');
            item.setStyles({
                'position': 'absolute',
                'z-index': (1000 + this._maxElements) - index
            });
        }.bind(this));
        this._container.setStyle('display', 'block'); 
        if (options.timer && $type(options.timer) == 'number' && options.timer > 0) {
            this.options.timer = options.timer;
            this.options.delay = options.delay;
        }
        else {
            this.options.timer = 1000;
            this.options.delay = 2800;
        }
        this._delay = this.options.delay;
        this._container.addEvent('mouseenter', this.toggle.bind(this));
        this._container.addEvent('mouseleave', this.toggle.bind(this));
        this._FxState = false;
    },
    
    pause: function() {
        if (!this._FxState)
            this.toggle();
    },
    
    resume: function() {
        if (this._FxState)
            this.toggle();
    },
    
    toggle: function() {
        this._FxState = !(this._FxState);
        if (this._currentFx == 1 && !this._FxState) {
            this._delay = 2000;
            this._wait_description();
        }
        else if (this._currentFx == 1) {
            $clear(this._wait_id);
            this._delay -= (new Date().getTime() - this._delay_start);
        }
    },

    _show_description: function(){
        this._currentContainer.block.fx.start(43).chain(this._wait_description.bind(this));
    },
    
    _hide_description: function(){
        this._wait_id = null;
        this._delay = this.options.delay;
        this._currentFx = 0;
        this._currentContainer.block.fx.start(-400).chain(this._fade_image.bind(this));
    },
    
    _wait_description: function (){
        this._currentFx = 1;
        if (!this._FxState) {
            this._wait_id = this._hide_description.bind(this).delay(this._delay);
            this._delay_start = new Date().getTime();
        }
    },
    
    _hide_container: function(container) {
        container.setStyle('z-index', 1000);
        this._currentContainer.setStyle('z-index', 1000 + this._maxElements);
    },
    
    _fade_image: function () {
        var _container = this._currentContainer;
        var _hide = this._hide_container.bind(this, [_container]);
        this._check_current.bind(this)();
        _container.fx.start(0).chain(_hide);
    },
    
    _check_current: function () {
        this._lastContainer = this._currentContainer;
        this._currentContainerIndex = ((this._currentContainerIndex + 1) > this._maxElements) ? 0 : (this._currentContainerIndex + 1);
        this._currentContainer = this._elements[this._currentContainerIndex];
        this.play();
    },
    
    play: function(){
        this._currentFx = 0;
        this._currentContainer.setStyle('display', 'block');
        this._currentContainer.fx.start(1).chain(this._show_description.bind(this));
    }
            
});
