﻿var diaporama = new Class({
    Implements: [Options],
    _slidesNumber: 0,
    _curSlideNum: 0,
    _prevSlideNum: -1,
    _curZ: 1,
    _inte: false,
    options: {
        container: false,
        duration: 1500,
        delay: 6000,
        eltpath: false,
        linkpath: false,
        autostart: false,
        UID: false,
        initSlideNum: 0,
        onChange: false,

        onBeforeShow: false, /* function(eltShown) {} */
        //onAfterShow:false,
        //onBeforeHide:false,
        onAfterHide: false/* function(eltHidden) {} */
    },
    _call: function (o) {
        this._stop();
        if (this.options.UID && this.options.UID != o.UID)
            return;

        if (o.a || o.a == 0)
            o = o.a;
        switch (typeOf(o)) {
            case "string":
                switch (o) {
                    case "next":
                        this._moveNext();
                        break;
                    case "back":
                        this._moveBack();
                        break;
                    case "play":
                        this._play();
                        break;
                }
                break;
            case "number":
                this._moveTo(o);
                break;
        };
    },
    initialize: function (options) {

        this.init(options);
        site.addEvent('diaporama', function (i) { this._call(i); } .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._curZ = 1;

        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;

        this._curSlideNum = Math.min(this.options.initSlideNum, this._slidesNumber - 1);

        imagesArr[this._curSlideNum].setStyle('z-index', this._curZ);
        this._curZ++;

        var eltArray = [];
        var fxObj = {};
        imagesArr.each(function (image, index) {
            image.set({ "rel": index, styles: { "display": "block", "position": "absolute"} });
            eltArray.push(image);
            fxObj[index] = { "opacity": index == this._curSlideNum ? 1 : 0 };
        }, this);
        if (this.options.linkpath)
            this._hightlightLink();
        var fx = new Fx.Elements(eltArray, { duration: this.options.duration });
        
        if (this.options.onAfterHide) {
            fx.addEvent("complete", function () {
                if (this._prevSlideNum >= 0)
                    this.options.onAfterHide.apply(this, [$(this.options.container).getElements(this.options.eltpath)[this._prevSlideNum]]);
            } .bind(this));
        }
        fx.set(fxObj);
        $(this.options.container).store("fx", fx);
        if (this.options.autostart)
            this._inte = this._moveNext.periodical(this.options.delay, this);
    },

    _stop: function () {
        if (this._inte)
            $clear(this._inte);
    },
    _play: function () {
        if (!this._inte) {
            this._inte = this._moveNext.periodical(this.options.delay, this);
        }
    },
    _moveNext: function () {
        this._moveTo((this._curSlideNum + 1) % this._slidesNumber);
    },
    _moveBack: function () {
        this._moveTo((this._curSlideNum - 1 + this._slidesNumber) % this._slidesNumber);
    },
    _moveTo: function (slideNum) {
        if (slideNum == this._curSlideNum)
            return;
        var fxObjSet = {};
        var fxObjStart = {};
        $(this.options.container).getElements(this.options.eltpath).each(function (image, ind) {
            if (ind == this._curSlideNum) { fxObjStart[ind] = { "opacity": 0 }; this._prevSlideNum = ind; }
            else if (ind == slideNum) { image.setStyle("z-index", this._curZ++); fxObjStart[ind] = { "opacity": 1 }; if (this.options.onBeforeShow) this.options.onBeforeShow.apply(this, [image]); }
            else { fxObjSet[ind] = { "opacity": 0 }; }
        }, this);

        var fx = $(this.options.container).retrieve("fx");
        fx.cancel();

        if (this.options.duration > 0) {
            fx
                .set(fxObjSet)
                .start(fxObjStart);
        }
        else {
            fx.set(fxObjStart);
        }
        this._curSlideNum = slideNum;
        if (this.options.onChange) {
            this.options.onChange.apply(this, [this._curSlideNum, this._slidesNumber]);
        }
        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');
    }
});

