window.addEvent('domready', function () {
    initGallery();
});


// cycle gallery init
function initGallery() {
    $$('div#gallery-holder').each(function (obj, i) {
        var _gallery = new cycleCarousel(obj);
    })
}

// cycle gallery code
var cycleCarousel = new Class({
    options: {
        btPrev: 'a.prev',
        btNext: 'a.next',
        slidesHolder: 'div#gallery',
        slider: 'ul',
        slides: 'li',
        startSlide: false,
        circleSlide: true,
        duration: 350,
        step: 1
    },

    // create class
    initialize: function (element, options) {
        this.setOptions(options);
        var _this = this;

        if (this.options.btNext) this.next = element.getElement(this.options.btNext);
        else this.next = false;

        if (this.options.btPrev) this.prev = element.getElement(this.options.btPrev);
        else this.prev = false;

        this.animated = false;
        this.holder = element.getElement(this.options.slidesHolder);
        this.slider = this.holder.getElement(this.options.slider);
        this.slides = this.holder.getElements(this.options.slides);
        this.duration = this.options.duration;



        this.slideH = this.slides[0] != null ? this.slides[0].getSize().x + this.slides[0].getStyle('marginRight').toInt() : 0;
        this.visibleStep = Math.round(this.holder.getSize().x / this.slideH);
        this.options.step ? this.step = this.options.step : this.step = this.visibleStep;
        this.options.startSlide ? this.current = Math.floor(this.options.startSlide / this.step) : this.current = 0;
        this.max = this.slides.length - this.visibleStep;
        this.stepCount = Math.ceil((this.slides.length - this.visibleStep) / this.step) + 1;

        // gallery control
        if (this.next) {
            this.next.addEvent('click', function () {
                if (!_this.animated) {
                    _this.nextSlide();
                }
                return false;
            });
        }
        if (this.prev) {
            this.prev.addEvent('click', function () {
                if (!_this.animated) {
                    _this.prevSlide();
                }
                return false;
            });
        }
    },
    nextSlide: function () {
        if (this.options.circleSlide && this.current == this.stepCount - 1) {
            this.previous = this.current;
            this.current = 0;
            this.move();
        }
        else if (this.current < this.stepCount - 1) {
            this.previous = this.current++
            this.move();
        }
    },

    prevSlide: function () {
        if (this.options.circleSlide && this.current == 0) {
            this.previous = this.current;
            this.current = this.stepCount - 1;
            this.move();
        }
        else if (this.current > 0) {
            this.previous = this.current--;
            this.move();
        }
    },

    move: function (instant) {
        var offset;
        if (this.step * this.current < this.max) offset = (this.step * this.current) * this.slideH;
        else offset = this.max * this.slideH
        if (instant) {
            this.slider.setStyles({ 'marginLeft': -offset })
        }
        else {
            var myFx = new Fx.Morph(this.slider, { duration: this.duration })
            myFx.start({ marginLeft: -offset });
        }
    },

    // add options and events
    Implements: [Options, Events]
});
