/**
 * LightboxAnimation Class
 */
LightboxAnimation = new Class({
    /**
     * Constructor
     *
     * @param Lightbox
     */
    initialize: function(lightbox) {
        this.lightbox = lightbox;
        this.current_step = null;
        this.step_arr = [];
        this.lightbox_min_width = 20;
        this.lightbox_min_height = 20;

        var thisObject = this;
        this.overlay_fx = new Fx.Morph(this.lightbox.overlay, {
            duration: 400, 
            transition: Fx.Transitions.Quart.easeIn,
            onComplete: function() {
                thisObject.animate();
            }
        });
        this.canvas_height_fx = new Fx.Morph(this.lightbox.canvas, {
            duration: 400, 
            transition: Fx.Transitions.Quart.easeIn,
            onComplete: function() {
                thisObject.animate();
            }
        });
        this.canvas_width_fx = new Fx.Morph(this.lightbox.canvas, {
            duration: 400, 
            transition: Fx.Transitions.Quart.easeIn,
            onComplete: function() {
                thisObject.animate();
            }
        });
    },

    /**
     * @param JSON
     * @return void
     */
    show: function(data) {
        this.data = data;

        if (this.lightbox.overlay.getStyle('opacity') === 0) {
            this.step_arr = ['start', 'overlay_max', 'canvas_height', 'canvas_width', 'display_content'];
        } else if (this.data.width == this.lightbox.canvas.getStyle('width').toInt() && this.data.height == this.lightbox.canvas.getStyle('height').toInt()) {
            this.step_arr = ['display_content'];
        } else {
            this.step_arr = ['overlay_max', 'canvas_height', 'canvas_width', 'display_content'];
        }

        this.lightbox.container.setStyle('display', 'block');
        this.current_step = 0;

        this.animate();
    },

    /**
     * @return void
     */
    hide: function() {
        this.data.width = this.lightbox_min_width;
        this.data.height = this.lightbox_min_height;

        this.step_arr = ['hide_content', 'canvas_width', 'canvas_height', 'overlay_min', 'end'];
        this.current_step = 0;

        this.animate();
    },

    /**
     * @param string
     * @return void
     */
    animate: function() {
        if (this.current_step >= this.step_arr.length) {
            return;
        }

        var step = this.step_arr[this.current_step++] +'_step';
        this[step]();
    },

    /**
     * @return void
     */
    end_step: function() {
        this.lightbox.content.setStyle('display', 'none');
        this.lightbox.container.setStyle('display', 'none');
        this.lightbox.canvas.setStyles({
            width: this.lightbox_min_width, 
            left: nl.code.lightbox.Lightboxer.calculateCanvasXPosition(this.lightbox_min_width),
            height: this.lightbox_min_height,
            top: nl.code.lightbox.Lightboxer.calculateCanvasYPosition(this.lightbox_min_height)
        });

        this.animate();
    },

    /**
     * @return void
     */
    start_step: function() {
        // nl.code.util.Debug.trace('start_step()');
        this.lightbox.content.setStyle('display', 'none');
        this.lightbox.container.setStyle('display', 'block');
        this.lightbox.canvas.setStyles({
            width: this.lightbox_min_width, 
            left: nl.code.lightbox.Lightboxer.calculateCanvasXPosition(this.lightbox_min_width),
            height: this.lightbox_min_height,
            top: nl.code.lightbox.Lightboxer.calculateCanvasYPosition(this.lightbox_min_height)
        });
        this.lightbox.overlay.setStyles({
            width: nl.code.lightbox.Lightboxer.calculateWindowWidth(this.data.width),
            height: nl.code.lightbox.Lightboxer.calculateWindowHeight(this.data.height),
            opacity: 0
        });

        this.animate();
    },

    /**
     * @return void
     */
    display_content_step: function() {
        this.lightbox.content.setStyle('display', 'block');
        // nl.code.util.Debug.trace('display_content_step()');
        this.lightbox.setContent(this.data.content);
        // this.lightbox.handleScripts(this.scripts);
        this.animate();
    },

    /**
     * @return void
     */
    hide_content_step: function() {
        this.lightbox.removeContent();

        this.animate();
    },

    /**
     * @return void
     */
    canvas_width_step: function() {
        this.lightbox.content.setStyle('display', 'none');

        // nl.code.util.Debug.trace('canvas_width_step()');
        if (this.lightbox.canvas.getStyle('width').toInt() == this.data.width) {
            return this.animate();
        }

        if (this.lightbox.canvas.getStyle('height').toInt() < this.lightbox_min_height) {
            this.lightbox.canvas.setStyle('height', this.lightbox_min_height);
        }

        this.canvas_width_fx.start({
            'left': nl.code.lightbox.Lightboxer.calculateCanvasXPosition(this.data.width),
            'width': this.data.width
        });
    },

    /**
     * @return void
     */
    canvas_height_step: function() {
        this.lightbox.content.setStyle('display', 'none');
        // nl.code.util.Debug.trace('canvas_height_step()');

        if (this.lightbox.canvas.getStyle('height').toInt() == this.data.height) {
            return this.animate();
        }

        if (this.lightbox.canvas.getStyle('width').toInt() < this.lightbox_min_width) {
            this.lightbox.canvas.setStyle('width', this.lightbox_min_width);
        }

        this.canvas_height_fx.start({
            'top': nl.code.lightbox.Lightboxer.calculateCanvasYPosition(this.data.height),
            'height': this.data.height
        });
    },

    /**
     * @return void
     */
    overlay_max_step: function() {
        // nl.code.util.Debug.trace('overlay_max_step()');
        this.overlay_fx.start({
            'opacity': 0.8
        });
    },

    /**
     * @return void
     */
    overlay_min_step: function() {
        this.overlay_fx.start({
            'opacity': 0
        });
    }
});
