﻿var MooRotator = new Class({
    Implements: [Options, Events],
    options: {
        slideInterval: 4000,
        transitionDuration: 700,
        startIndex: 0,
        buttonOnClass: 'selected',
        buttonOffClass: 'off',
        rotateAction: 'none',
        rotateActionDuration: 100,
        autoplay: true,
        showButtons: true,
        playPause: true
    },
    initialize: function (container, options) {
        this.setOptions(options);
        this.container = $(container);
        if (this.container.hasClass('hasCarousel')) return false;
        this.container.addClass('hasCarousel');
        this.slides = $$('#' + this.container.id + ' .slide');
        if (this.options.showButtons) this.buttons = $$('#' + this.container.id + ' a.button');

        if (this.options.playPause) {
            this.playPause = $$('#' + this.container.id + ' a#playpause');
            this.playPause.addEvent('stop', function () { alert('debug'); });
        }

        this.createFx();
        this.showSlide(this.options.startIndex);
        if (this.options.autoplay) this.autoPlay();
        if (this.options.rotateAction != 'none') this.setupAction(this.options.rotateAction);
        return this;
    },
    toElement: function () {
        return this.container;
    },
    setupAction: function (action) {
        if (this.buttons != null) {
            this.buttons.each(function (el, idx) {
                $(el).addEvent(action, function () {
                    this.slideFx.setOptions(this.slideFx.options, { duration: this.options.rotateActionDuration });
                    if (this.currentSlide != idx) this.showSlide(idx);
                    this.stop();
                } .bind(this));
            }, this);
        }
    },
    createFx: function () {
        if (!this.slideFx) this.slideFx = new Fx.Elements(this.slides, { duration: this.options.transitionDuration });

        this.slides.each(function (slide) {
            slide.setStyle('display', 'none');
            slide.setStyle('opacity', 0);
        });
    },
    scroll: function (slideIndex) {

    },
    showSlide: function (slideIndex) {
        var action = {};
        this.slides.each(function (slide, index) {
            if (index == slideIndex && index != this.currentSlide) { //show
                if (this.buttons != null) $(this.buttons[index]).swapClass(this.options.buttonOnClass, this.options.buttonOffClass);
                action[index.toString()] =
				{
				    display: 'block',
				    opacity: 1
				};
            }
            else {
                if (this.buttons != null) $(this.buttons[index]).swapClass(this.options.buttonOffClass, this.options.buttonOnClass);
                action[index.toString()] =
				{
				    opacity: 0,
				    display: 'none'
				};
            }
        }, this);

        this.currentSlide = slideIndex;
        this.slideFx.start(action);
        return this;

    },
    autoPlay: function () {
        this.slideshowInt = this.rotate.periodical(this.options.slideInterval, this);

        if (this.options.playPause) {
            this.playPause.addEvent('click', function () { this.stop(); }.bind(this));
            this.playPause.setProperty('class', 'pause');
        }

        this.fireEvent('autoPlay');
    },
    stop: function () {
        $clear(this.slideshowInt);

        if (this.options.playPause) {
            this.playPause.addEvent('click', function () { this.autoPlay(); this.rotate(); }.bind(this));
            this.playPause.setProperty('class', 'play');
        }

        this.fireEvent('stop');
    },
    rotate: function () {
        current = this.currentSlide;
        next = (current + 1 >= this.slides.length) ? 0 : current + 1;
        this.showSlide(next);
        this.fireEvent('rotate');
    }
});
