var YOOcarousel = new Class({
    initialize: function (container, options) {
        this.setOptions({
            onRotate: $empty,
            onStop: $empty,
            onAutoPlay: $empty,
            onShowSlide: $empty,
            panelSelector: '.panel',
            slidesSelector: '.slide',
            buttonsSelector: '.button',
            buttonNextSelector: '.button-next',
            buttonPrevSelector: '.button-prev',
            slideInterval: 4000,
            transitionDuration: 700,
            transitionget: 'scroll',
            startIndex: 0,
            buttonOnClass: 'selected',
            buttonOffClass: 'off',
            rotateAction: 'none',
            rotateActionDuration: 100,
            rotateActionget: '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;
		
		//this.slides[0].style.display = 'none'
		this.slides[1].style.display = 'none'
		this.slides[2].style.display = 'none'
		this.slides[3].style.display = 'none'
		
		
        if (this.options.transitionget == 'crossfade' || this.options.rotateActionget == 'crossfade') {
            this.fxCrossfade = new Array();
            this.slides.each(function (el, i) {
                this.fxCrossfade[i] = new Fx.Tween(el, 'opacity');
                if (i != this.options.startIndex) el.setStyle('opacity', 0)
            },
            this);
            this.options.transitionget = 'crossfade';
            this.options.rotateActionget = 'crossfade'
        } else {
            this.fxScroll = new Fx.Scroll(this.panel, {
                'wait': false
            });
            this.fxFade = new Fx.Tween(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.rotateActionget == 'scroll') {
                        this.showSlide(i, this.options.rotateActionDuration, this.options.rotateActionget)
                    } else {
                        $clear(timer);
                        timer = this.showSlide.delay(this.options.rotateActionDuration, this, [i, this.options.rotateActionDuration, this.options.rotateActionget])
                    }
                    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.rotateActionget);
                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.rotateActionget);
                this.stop()
            }.bind(this))
        }
    },
    showSlide: function (index, duration, transition) {
        if (index == this.currentSlide) return;
		
		//hack 2
			if(this.currentSlide == null)
				this.slides[0].style.display = 'none'
			else
				this.slides[this.currentSlide].style.display = 'none'
			this.slides[index].style.display = 'block'
		
		
        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.transitionget);
        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);
