var RokNewsRotator = new Class({
    version: '1.5',
    options: {
        duration: 1000,
        delay: 5000,
        autoplay: false,
        corners: true,
        controls: true,
        autohide: true,
        fadeIn: 0.7,
        blankimage: '/images/blank.png'
    },
    initialize: function(container, options) {
        this.container = $(container);
        this.setOptions(options);
        this.images = this.container.getElements('.image > img');
        this.content = $$('#' + container + ' .story-block .divider div.story div.padding');
        this.current = 0;
        this.build();
        this.attachEvents();
        this.status = 'stop';
        if (this.options.autoplay) window.addEvent('load', this.play.bind(this));
        return this
    },
    build: function() {
        var self = this;
        $$(this.content, this.images).setStyle('position', 'absolute');
        var images = this.images.slice(1);
        var content = this.content.slice(1);
        images.each(function(image) {
            image.injectAfter(this.images[0]).setStyle('opacity', 0)
        },
        this);
        content.each(function(story) {
            story.injectAfter(this.content[0]).setStyle('opacity', 0)
        },
        this);
        var stories = $$('.story-block').slice(1);
        stories.each(function(story) {
            story.empty().remove()
        });
        var controls = new Element('div', {
            'class': 'controls'
        }).inject(this.container);
        this.arrowPrev = new Element('img', {
            'class': 'control-prev',
            'title': 'Previous',
            'alt': 'prev',
            'src': this.options.blankimage
        }).inject(controls);
        this.arrowNext = new Element('img', {
            'class': 'control-next',
            'title': 'Next',
            'alt': 'next',
            'src': this.options.blankimage
        }).inject(controls);
        this.arrowPlay = new Element('img', {
            'id': 'play-stop',
            'class': 'control-stop',
            'title': 'Play/Stop',
            'alt': 'play/stop',
            'src': this.options.blankimage
        }).inject(controls);
        if (!this.options.controls) controls.setStyle('display', 'none');
        if (this.options.autohide && this.options.controls) {
            var controlsFx = new Fx.Style(controls, 'opacity', {
                wait: false
            }).set(0);
            this.container.addEvents({
                'mouseenter': controlsFx.start.bind(controlsFx, this.options.fadeIn),
                'mouseleave': controlsFx.start.bind(controlsFx, 0)
            })
        };
        if (this.options.corners) { (this.images.length).times(function(i) { (2).times(function(j) {
                    new Element('div', {
                        'class': 'i' + (j + 1)
                    }).inject(this.images[i])
                }.bind(this))
            }.bind(this))
        } (4).times(function(i) {
            new Element('div', {
                'class': 'corner c' + (i + 1)
            }).inject(this.content[0].getParent())
        }.bind(this));
        this.fx = []; (this.content.length).times(function(i) {
            this.fx[i] = [new Fx.Style(this.images[i], 'opacity', {
                duration: this.options.duration,
                onStart: function() {
                    self.transitioning = true
                },
                onComplete: function() {
                    self.transitioning = false
                }
            }), new Fx.Style(this.content[i], 'opacity', {
                duration: this.options.duration
            })]
        }.bind(this));
        return this
    },
    attachEvents: function() {
        var self = this,
        playstop = $('play-stop');
        this.arrowPrev.addEvent('click', this.previous.bind(this));
        this.arrowNext.addEvent('click', this.next.bind(this));
        this.arrowPlay.addEvent('click',
        function() {
            if (self.status == 'play') {
                self.stop();
                playstop.className = 'control-play'
            } else {
                self.play();
                playstop.className = 'control-stop'
            }
        });
        return this
    },
    previous: function() {
        if (this.transitioning) return this;
        var prev = (!this.current) ? this.content.length - 1: this.current - 1;
        this.fx[this.current].each(function(fx) {
            fx.start(0)
        });
        this.fx[prev].each(function(fx) {
            fx.start(1)
        });
        this.current = prev;
        return this
    },
    next: function() {
        if (this.transitioning) return this;
        var next = (this.current == this.content.length - 1) ? 0: this.current + 1;
        this.fx[this.current].each(function(fx) {
            fx.start(0)
        });
        this.fx[next].each(function(fx) {
            fx.start(1)
        });
        this.current = next;
        return this
    },
    play: function() {
        if (this.status == 'play') return this;
        this.status = 'play';
        this.timer = this.next.periodical(this.options.delay + this.options.duration, this);
        return this
    },
    stop: function() {
        this.status = 'stop';
        $clear(this.timer);
        return this
    }
});
RokNewsRotator.implement(new Events, new Options);
