window.addEvent('domready', function () {
    initGallery();
    initClosePopup();
});

function initClosePopup() {
    var popup = $$('div.popup')[0];
    var slide = popup.getElement('div.slide');
    var closer = popup.getElement('a.close');
    var animation = false;

    closer.addEvent('click', function () {
        if (slide == null) {
            alert('Non è stata utilizzata la struttura HTML corretta!');
            return;
        }
        if (!animation) {
            animation = true;
            if (closer.hasClass('open')) {
                slide.setStyles({ display: 'block', height: 'auto' });
                var h = slide.getSize().x;
                slide.setStyles({ height: 0 });
                var myFx = new Fx.Morph(slide, { duration: 550, onComplete: function () {
                    animation = false;
                    closer.removeClass('open');
                }
                })
                myFx.start({ height: h });
            } else {
                closer.addClass('open');
                var myFx = new Fx.Morph(slide, { duration: 550, onComplete: function () {
                    slide.setStyles({ display: 'none' });
                    animation = false;
                }
                })
                myFx.start({ height: 0 });
            }
        }
        return false;
    });
}

// cycle gallery init
function initGallery() {
    $$('div.gallery').each(function (obj, i) {
        var _gallery = new cycleCarousel(obj);
    })
}

// cycle gallery code
var cycleCarousel = new Class({
    options: {
        btPrev: 'a.btn-up',
        btNext: 'a.btn-down',
        slidesHolder: 'div.holder',
        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().y + this.slides[0].getStyle('marginBottom').toInt() : 0;
        this.visibleStep = Math.round(this.holder.getSize().y / 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({ 'marginTop': -offset })
        }
        else {
            var myFx = new Fx.Morph(this.slider, { duration: this.duration })
            myFx.start({ marginTop: -offset });
        }
    },

    // add options and events
    Implements: [Options, Events]
});
