﻿var diaporama = new Class({
    Implements: [Options],
    _slidesNumber: 0,
    _curSlideNum: 0,
    _curZ: 1,
    _inte: false,
    _call: function (o) {
        switch (o.a) {
            case "next":
                this._stop();
                this._moveNext();
                break;
            default: // o is a number 
                site.HM.set("i", o);
                this._stop();
                this._moveTo(o);
                break;
        }
    },
    options: {
        container: false,
        duration: 1500,
        delay: 6000,
        eltpath: false,
        linkpath: false,
        linktonextpath: false,
        linktoprevpath: false
    },
    initialize: function (options) {
        this.setOptions(options);
        this._init();
        site.addHistoryEvent('diaporamaHistory', 'i');
        site.addEvent('diaporamaHistory', function (o) { this._moveTo(o.v); } .bind(this));
        site.addEvent('diaporama', function (i) { this._call(i); } .bind(this));
    },
    _init: function () {
        if (this._inte)
            $clear(this._inte);
        this._curZ = 1;
        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;

        if (this.options.linktonextpath) {
            $(this.options.container).getElement(this.options.linktonextpath).addEvent('click', function () {
                this._stop();
                this._moveTo((this._curSlideNum + 1) % this._slidesNumber);
            } .bind(this));
        }
        if (this.options.linktoprevpath) {
            $(this.options.container).getElement(this.options.linktoprevpath).addEvent('click', function () {
                this._stop();
                if (this._curSlideNum <= 0) {
                    this._moveTo(this._slidesNumber - 1);
                }
                else
                    this._moveTo(this._curSlideNum - 1);
            } .bind(this));
        }
        var eltArray = [];
        var fxObj = {};
        imagesArr.each(function (image, index) {
            image.set({ "rel": index, styles: { "display": "block"} });
            eltArray.push(image);
            fxObj[index] = { "opacity": index == 0 ? 1 : 0 };
        }, this);
        if (this.options.linkpath)
            this._hightlightLink();
        var fx = new Fx.Elements(eltArray, { duration: this.options.duration });
        fx.set(fxObj);
        $(this.options.container).store("fx", fx);
        this._inte = this._moveNext.periodical(this.options.delay, this);
    },
    _stop: function () {
        if (this._inte)
            $clear(this._inte);
    },
    _moveNext: function () {
        this._moveTo((this._curSlideNum + 1) % 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 }; }
            else if (ind == slideNum) { image.setStyle("z-index", this._curZ++); fxObjStart[ind] = { "opacity": 1 }; }
            else { fxObjSet[ind] = { "opacity": 0 }; }
        }, this);
        $(this.options.container)
            .retrieve("fx")
            .cancel()
            .set(fxObjSet)
            .start(fxObjStart);
        this._curSlideNum = slideNum;
        if (this.options.linkpath)
            this._hightlightLink();
    },
    _hightlightLink: function () {
        var legend = "";
        $(this.options.container).getElements(this.options.linkpath).each(function (aElt, ind) {

            if (ind == this._curSlideNum) {
                aElt.addClass('active');
                legend = aElt.get('title');
            }
            else {
                aElt.removeClass('active');
            }
        }, this);
        if ($('slideShowControls')) {
            //set legend : 
            if (legend != "")
                $('slideShowControls').getElement('span.desc').set('html', legend);
        }
    }
});

