var Carousel = new Class({
    Implements: Options,
    options: {
        slideInterval: 12000,
        transitionDelay: 400,
        startIndex: 0,
        autoplay: true,
        jump_button_class: 'jump-buttons',
        jump_button_parent: '.content',
        slide_width: 920
    },
    initialize: function(slides, slide_container, left_button, right_button, options) {
        this.setOptions(options);
        this.slides = $$(slides);
        this.slide_name = slides;
        this.slide_container = $$(slide_container)[0];
        this.left_button = $$(left_button);
        this.right_button = $$(right_button);
        this.current_slide = 0;
        this.current_position = 0;
        this.offset = 0;
        this.moving = false;
        this.auto_moving = false;
        this.create_fx();

        this.setup_buttons();
        if (this.options.autoplay) this.autoplay();
    },
    create_fx: function() {

        $$(this.slides).each(function(slide, index, slides) {
            slide.set('slider_position', index);
            slide.clone().inject(this.slides[0], 'before');
        }.bind(this));

        this.slide_container.setStyle('left', "-" + (this.slides.length) * this.options.slide_width + "px");
        this.fx = new Fx.Tween(this.slide_container, {
            duration: this.options.transitionDelay,
            transition: 'sine:in:out',
            link: 'chain'
        });
        this.fx.addEvent('chainComplete', function(event) {
            this.moving = false;
            this.auto_moving = false;
        }.bindWithEvent(this));

        return this;
    },
    setup_buttons: function() {
        this.left_button.addEvent('click', function(event) {
            this.stop();
            this.swap(((this.current_slide - 1) + this.slides.length) % this.slides.length);
        }.bind(this));
        this.right_button.addEvent('click', function(event) {
            this.stop();
            this.swap((this.current_slide + 1) % this.slides.length);
        }.bind(this));

        $$(this.slide_name).each(function(slide, index) {
            var content = slide.getChildren(this.options.jump_button_parent)[0];
            var c = new Element('div', {
                'class': this.options.jump_button_class
            }).inject(content);
            for (var i = 0; i < this.slides.length; i++) {
                var button = new Element('a', {
                  'href': '#'
                }).inject(c);
                button.addEvent('click', function(i){
                  this.stop();
                  this.swap(i);
                }.bind(this,i));
                if (slide.get('slider_position') == i) {
                    button.addClass('current');
                }
            }
        }.bind(this));
    },
    swap: function(swap_to) {

        if (this.current_slide != swap_to && !this.auto_moving) {

            offset = swap_to - this.current_slide;
            if (offset > Math.floor(this.slides.length / 2)) {
                offset = offset - this.slides.length;
            }
            if (offset <= -Math.floor(this.slides.length / 2)) {
                offset = offset + this.slides.length;
            }
            if (offset < 0 && this.current_position < 0 && !this.moving) {
                this.slide_container.setStyle('left', "-" + (this.slides.length + this.current_position) * this.options.slide_width + "px");
                this.current_position = this.current_slide;
            } else if (offset > 0 && this.current_position > 0 && !this.moving) {
                this.slide_container.setStyle('left', "-" + (this.current_position) * this.options.slide_width + "px");
                this.current_position = this.current_slide - this.slides.length;
            }

            var old_left = ((this.slides.length + this.current_position) * this.options.slide_width);
            this.current_position += offset;
            var new_left = ((this.slides.length + this.current_position) * this.options.slide_width);
            this.current_slide = swap_to;
            this.moving = true;
            this.fx.start('left', ["-" + old_left + "px", "-" + new_left + "px"]);

        }

        return this;
    },
    rotate: function(direction) {
        if (direction == "back") {
            var slide = this.current_slide - 1;
        } else {
            var slide = this.current_slide + 1;
        }
        this.swap(((slide) + this.slides.length) % this.slides.length);
        this.auto_moving = true;
    },
    autoplay: function() {
      this.auto = this.rotate.periodical(this.options.slideInterval, this);
    },
    stop: function() {
        $clear(this.auto);
    }
});


var carousel;


window.addEvent('domready',
function(event) {
		$$('.previous')[0].setStyle('display','block');
		$$('.next')[0].setStyle('display','block');
    carousel = new Carousel('.slider .slider-pane', '.slider-holder', '.slider .previous', '.slider .next', '.jump-buttons');
});


