﻿var CDMenu = new Class({

    Implements: [Options, Events],

    options: {//set all the options here
        tipoEfeito:$empty,
        initOpacity: 0,
        finalOpacity: 1,
        initWidth: 0,
        finalWidth: 250,
        elemento: $empty,
        isArray: false
    },

    initialize: function(options) {
        $extend(this.options, options);
    }, 
    efectMenu: function(elemento) {
        var optionsList = this.options;
        if (this.options.isArray) {
            elemento.each(function(el, index) {
                new Fx.Morph(el, { duration: 'long', transition: optionsList.tipoEfeito }).start({
                    'opacity': optionsList.finalOpacity,
                    'width': optionsList.finalWidth
                });
            });
        } else {
            new Fx.Morph(elemento, { duration: 'long', transition: this.options.tipoEfeito }).start({
                'opacity': this.options.finalOpacity,
                'width': this.options.finalWidth
            });
        }

    }

});

var CDSlide = new Class({

    Implements: [Options, Events],

    options: {//set all the options here
        tipoEfeito: $empty,
        tipoEfeito2: $empty,
        isTransition: false,
        initOpacity: 0,
        finalOpacity: 1,
        initWidth: 0,
        finalWidth: 250,
        useRandom: false,
        current: 0,
        duracao: 400,
        galeria: $empty,
        autoplay: true,
        forceStop: false
    },
    countImages: 0,
    imageIndex: $empty,
    objStart: $empty,

    initialize: function(options) {
        $extend(this.options, options);
        this.initCDSlide();

    }, //end init

    initCDSlide: function() {
        this.countImages = this.options.galeria.getElements('img').length - 1;
        this.setImages(this.options.galeria.getElements('img'));
        if (this.options.autoplay && this.countImages >= 0) {
            this.firstView();
            this.objStart = this.moveNext.periodical(this.options.duracao, this);
        } else if (this.options.autoplay == false) {
            this.firstView();
        }
    },
    setImages: function(imgs) {
        var copyOptions = this.options;
        imgs.each(function(img, index) {
            img.setOpacity(copyOptions.initOpacity);
            if (copyOptions.isTransition) {
                img.setStyles({ visibility: 'hidden', display: 'none', width: copyOptions.initWidth, zIndex: 1 });
            } else {
                //var imgSize = img.getSize();
                //var templateSize = copyOptions.galeria.getSize();
                //alert(templateSize.x);
                //var leftPosition = (templateSize.x - imgSize.x) / 2;

                //leftPosition = leftPosition > 0 ? leftPosition : 0;

                img.setStyles({ visibility: 'hidden', display: 'none', zIndex: 1 });
            }
        });
    },
    firstView: function() {
        if (this.options.useRandom) {
            imageIndex = this.options.galeria.getElements('img').getRandom();
        } else {
            imageIndex = this.options.galeria.getElements('img')[this.options.current];
        }
        this.setImages(this.options.galeria.getElements('img'));
        this.execEffect(imageIndex);
    },

    changeTo: function() {
        if (this.options.useRandom) {
            imageIndex = this.options.galeria.getElements('img').getRandom();
        } else {
            imageIndex = this.options.galeria.getElements('img')[this.options.current];
        }
        this.setImages(this.options.galeria.getElements('img'));
        this.execEffect(imageIndex);
    },

    execEffect: function(image) {
        if (this.options.isTransition) {
            new Fx.Morph(image, { duration: this.options.duracao, transition: this.options.tipoEfeito }).start({
                'opacity': 1,
                'display': 'block',
                'width': this.options.finalWidth
            });
        } else {
            var fx = new Fx.Tween(image);
            image.set('tween', { duration: this.options.duracao });
            image.setStyle('display', 'block');
            fx.start('opacity', 1);
        }

    },

    moveNext: function() {
        if (this.objStart == $empty) {
            this.objStart = this.moveNext.periodical(this.options.duracao, this);
        }
        var next = this.options.current + 1;
        if (next <= this.countImages) {
            next = next == this.countImages ? this.countImages : next;
        } else {
            next = 0;
            if (this.options.forceStop) {
                $clear(this.objStart);
                this.objStart = $empty;
            }
        }
        this.options.current = next;
        this.changeTo();

    },

    next: function() {
        $clear(this.objStart);
        this.objStart = $empty;
        var next = this.options.current + 1;
        if (next <= this.countImages) {
            next = next == this.countImages ? this.countImages : next;
        } else {
            next = 0;

        }
        this.options.current = next;
        this.changeTo();
    },

    previous: function() {
        $clear(this.objStart);
        this.objStart = $empty;
        var prev = this.options.current - 1;
        if (prev < 0) {
            prev = 0;
        }
        this.options.current = prev;
        this.changeTo();
    },

    stop: function() {
        $clear(this.objStart);
        this.objStart = $empty;
    },

    play: function() {
        if (this.objStart == $empty) {
            this.objStart = this.moveNext.periodical(this.options.duracao, this);
        }
    },
    setDuracao: function(time) {
        $clear(this.objStart);
        this.objStart = $empty;
        this.options.duracao = time;
        if (this.objStart == $empty) {
            this.objStart = this.moveNext.periodical(this.options.duracao, this);
        }
    }
});
