﻿var slider = new Class({
    Implements: [Options],
    _slidesNumber: 0,
    _curSlideNum: 0,
    _inte: false,
    _paused: false,
    options: {
        container: false,
        duration: 1000,
        delay: 5000,
        eltpath: false,
        linkpath: false,
        linktonextpath: false,
        linktoprevpath: false,
        slideWidth: 695,
        autostart: false,
        id: ""
    },
    _call: function (e) {

        if (!e.i || e.i == this.options.id) {

            this._stop();
            this._moveTo(e.p);

        }
    },
    initialize: function (options) {
        this.init(options);
        $(this.options.container).getElement("div#produitSlider").setStyle('overflow', 'hidden');
        site.addEvent('slider', function (e) { this._call(e); } .bind(this));
    },
    init: function (options) {
        this.setOptions(options);
        if (this._inte)
            $clear(this._inte);
        if (this.options.container && $(this.options.container).retrieve("fx"))
            $(this.options.container).eliminate('fx');
        this._curSlideNum = 0;
        if (!$(this.options.container) || !(this.options.eltpath))
            return;
        var imagesArr = $(this.options.container).getElements(this.options.eltpath);
        this._slidesNumber = imagesArr.length;
        if (this._slidesNumber == 0)
            return;
        site.fireEvent("imageloading", imagesArr[0]);
        site.call("glasses", { a: 'try', id: imagesArr[0].get('data-fittingBox') });
        site.call("noFittingBox", { i : 0 });
        if (this.options.linktonextpath) {
            $(this.options.container).getElements(this.options.linktonextpath).each(function (nextElt) {
                nextElt.addEvent('click', function () {
                    this._stop();
                    this._moveTo((this._curSlideNum + 1) % this._slidesNumber);
                } .bind(this))
            }, this);
        }
        if (this.options.linktoprevpath) {
            $(this.options.container).getElements(this.options.linktoprevpath).each(function (prevElt) {
                prevElt.addEvent('click', function () {
                    this._stop();
                    if (this._curSlideNum <= 0) {
                        this._moveTo(this._slidesNumber - 1);
                    }
                    else
                        this._moveTo(this._curSlideNum - 1);
                } .bind(this));
            }, this);
        }
        var fx = new Fx.Scroll($(this.options.container).getElement("div#produitSlider"), { duration: this.options.duration, wheelStops: false });

        fx.cancel().start(0, 0);
        $(this.options.container).store("fx", fx);
        imagesArr.each(function (image, index) {
            image
                .addEvents({
                    "mouseenter": this._pause.bind(this),
                    "mouseleave": this._resume.bind(this)
                })
                .set({ "rel": index });

        }, this);
        if (this.options.linkpath)
            this._hightlightLink();
        if (this.options.autostart)
            this._inte = this._moveNext.periodical(this.options.delay, this);
    },
    _pause: function () {
        this._paused = true;
    },
    _resume: function () {
        this._paused = false;
    },
    _stop: function () {
        if (this._inte)
            $clear(this._inte);
    },
    _moveNext: function () {
        if (!this._paused)
            this._moveTo((this._curSlideNum + 1) % this._slidesNumber);
    },
    _moveTo: function (slideNum) {

        site.fireEvent("imageloading", $(this.options.container).getElements(this.options.eltpath)[slideNum]);
        site.call("glasses", { a: 'try', id: $(this.options.container).getElements(this.options.eltpath)[slideNum].get('data-fittingBox') });
        site.call("noFittingBox", { i : slideNum});
        if (slideNum == this._curSlideNum)
            return;

        $(this.options.container).retrieve("fx").cancel().start(slideNum * this.options.slideWidth, 0);
        this._curSlideNum = slideNum;

        if (this.options.linkpath)
            this._hightlightLink();
    },
    _hightlightLink: function () {
        $(this.options.container).getElements(this.options.linkpath).each(function (aElt, ind) {
            if (ind == this._curSlideNum) {
                aElt.addClass('active');
            }
            else {
                aElt.removeClass('active');
            }
        }, this);
    },
    destroy: function () {
        if (this._inte)
            $clear(this._inte);
        if (this.options.container && $(this.options.container).retrieve("fx"))
            $(this.options.container).eliminate('fx');
    }
});
