var YOOcarousel = new Class(
{
    initialize: function (container, options) {
        this.setOptions(
		{
		    onRotate: Class.empty,
		    onStop: Class.empty,
		    onAutoPlay: Class.empty,
		    onShowSlide: Class.empty,
		    panelSelector: '.panel',
		    slidesSelector: '.slide',
		    buttonsSelector: '.button',
		    buttonNextSelector: '.button-next',
		    buttonPrevSelector: '.button-prev',
		    slideInterval: 1000,
		    transitionDuration: 2000,
		    transitionEffect: 'crossfade',
		    startIndex: 0,
		    buttonOnClass: 'selected',
		    buttonOffClass: 'off',
		    rotateAction: 'none',
		    rotateActionDuration: 100,
		    rotateActionEffect: 'scroll',
		    autoplay: 'on'
		}, options);
        this.container = $(container);
        this.panel = this.container.getElement(this.options.panelSelector);
        this.slides = this.container.getElements(this.options.slidesSelector);
        this.buttons = this.container.getElements(this.options.buttonsSelector);
        this.buttonNext = this.container.getElement(this.options.buttonNextSelector);
        this.buttonPrev = this.container.getElement(this.options.buttonPrevSelector);
        this.currentSlide = null;
        if (this.options.transitionEffect == 'crossfade' || this.options.rotateActionEffect == 'crossfade') {
            this.fxCrossfade = new Array();
            this.slides.each(function (el, i) {
                this.fxCrossfade[i] = new Fx.Style(el, 'opacity');
                if (i != this.options.startIndex) el.setStyle('opacity', 0)
            }, this);
            this.options.transitionEffect = 'crossfade';
            this.options.rotateActionEffect = 'crossfade'
        }
        else {
            this.fxScroll = new Fx.Scroll(this.panel, {
                'wait': false
            });
            this.fxFade = new Fx.Style(this.panel, 'opacity', {
                'wait': false
            })
        }
        this.setupButtons();
        this.showSlide(this.options.startIndex, 1);
        if (this.options.autoplay == 'on' || this.options.autoplay == 'once') this.autoplay()
    },
    setupButtons: function () {
        if (this.options.rotateAction != 'none') {
            var timer = null;
            this.buttons.each(function (el, i) {
                $(el).addEvent(this.options.rotateAction, function () {
                    if (this.options.rotateActionEffect == 'scroll') {
                        this.showSlide(i, this.options.rotateActionDuration, this.options.rotateActionEffect)
                    }
                    else {
                        $clear(timer);
                        timer = this.showSlide.delay(this.options.rotateActionDuration, this, [i, this.options.rotateActionDuration, this.options.rotateActionEffect])
                    }
                    this.stop()
                } .bind(this))
            }, this)
        }
        if (this.buttonNext && this.buttonPrev) {
            this.buttonNext.addEvent('click', function () {
                if (this.currentSlide + 1 >= this.slides.length) {
                    next = 0
                }
                else {
                    next = this.currentSlide + 1
                };
                this.showSlide(next, this.options.rotateActionDuration, this.options.rotateActionEffect);
                this.stop()
            } .bind(this));
            this.buttonPrev.addEvent('click', function () {
                if (this.currentSlide - 1 < 0) {
                    next = this.slides.length - 1
                }
                else {
                    next = this.currentSlide - 1
                };
                this.showSlide(next, this.options.rotateActionDuration, this.options.rotateActionEffect);
                this.stop()
            } .bind(this))
        }
    },
    showSlide: function (index, duration, transition) {
        if (index == this.currentSlide) return;
        this.slides.each(function (slide, i) {
            var button = $(this.buttons[i]);
            if (i == index && i != this.currentSlide) {
                if (button) button.removeClass(this.options.buttonOffClass).addClass(this.options.buttonOnClass)
            }
            else {
                if (button) button.removeClass(this.options.buttonOnClass).addClass(this.options.buttonOffClass)
            }
        }, this);
        switch (transition) {
            case 'fade':
                this.fxFade.setOptions(
			{
			    'duration': duration
			});
                this.fxFade.start(1, 0.01).chain(function () {
                    this.fxScroll.setOptions(
				{
				    'duration': 1
				});
                    this.fxScroll.toElement(this.slides[index]);
                    this.fxFade.start(0.01, 1)
                } .bind(this));
                break;
            case 'crossfade':
                this.slides.each(function (el, i) {
                    this.fxCrossfade[i].setOptions(
				{
				    'duration': duration
				});
                    if (i == index) {
                        this.fxCrossfade[i].start(1)
                    }
                    else if (el.getStyle('opacity') > 0) {
                        this.fxCrossfade[i].start(0)
                    }
                }, this);
                break;
            case 'scroll':
                this.fxScroll.setOptions(
			{
			    'duration': duration
			});
                this.fxScroll.toElement(this.slides[index])
        }
        this.currentSlide = index;
        this.fireEvent('onShowSlide', index)
    },
    rotate: function () {
        if (this.currentSlide + 1 >= this.slides.length) {
            next = 0
        }
        else {
            next = this.currentSlide + 1
        };
        if (this.options.autoplay == 'once' && next == 0) {
            this.stop();
            return
        };
        this.showSlide(next, this.options.transitionDuration, this.options.transitionEffect);
        this.fireEvent('onRotate')
    },
    autoplay: function () {
        this.slideshowInt = this.rotate.periodical(this.options.slideInterval, this);
        this.fireEvent('onAutoPlay')
    },
    stop: function () {
        clearInterval(this.slideshowInt);
        this.fireEvent('onStop')
    }
});
YOOcarousel.implement(new Options);
YOOcarousel.implement(new Events);
