/**
 * Preloader Class
 */
Core.createNamespace('nl.code.lightbox');
nl.code.lightbox.Preloader = new Class({
    Extends: nl.code.util.Observable,

    /**
     * Constructor
     *
     * @param JSON
     * @param Lightbox
     */
    initialize: function(data, lightbox) {
        this.data = data;
        this.lightbox = lightbox;

        this.images_preloaded = 0;

        this.preload_content = this.createContentContainer();
        this.addImageEvents();
    },

    /**
     * @return void
     */
    addImageEvents: function() {
        var thisObject = this;
        this.preload_image_arr = this.preload_content.getElements('img');
        if (this.preload_image_arr.length) {
            for (var i = 0; i < this.preload_image_arr.length; i++) {
                var preload_image = new Asset.image(this.preload_image_arr[i].src, {
                    onload: function() {
                        thisObject.preloadImage();
                    },
                    onerror: function() {
                        thisObject.preloadImage();
                    },
                    onabort: function() {
                        thisObject.preloadImage();
                    }
                });
            }
        } else {
            this.preloadComplete();
        }
    },

    /**
     * @return Element
     */
    createContentContainer: function() {
        var preload_container = new Element('div', {
            'class': 'lightbox-preload-container'
        });
        preload_container.injectInside(document.body);

        var preload_content = new Element('div', {
            'class': 'lightbox-preload-content'
        });

        // check wich dimension is missing
        if (this.data.height != 0) {
            preload_content.setStyles({
                height: this.data.height,
                top: (-2 * this.data.height)
            });
        } else if (this.data.width != 0) {
            preload_content.setStyles({
                width: this.data.width,
                left: (-2 * this.data.width)
            });
        }

        preload_content.injectInside(preload_container);
        preload_content.set('html', this.data.content);

        return preload_content;
    },

    /**
     * @return void
     */
    preloadComplete: function() {
        var coordinates = this.preload_content.getCoordinates();

        if (nl.code.lightbox.Lightboxer.fit_screen) {
            this.data.width = (coordinates.width > (Window.getSize().x - 40) ? (Window.getSize().x - 40) : coordinates.width);
            this.data.height = (coordinates.height > (Window.getSize().y - 40) ? (Window.getSize().y - 40) : coordinates.height);
        } else {
            this.data.width = coordinates.width;
            this.data.height = coordinates.height;
        }

        this.lightbox.show(this.data);
    },

    /**
     * @return void
     */
    preloadImage: function() {
        this.images_preloaded++;

        if (this.images_preloaded == this.preload_image_arr.length) {
            this.preloadComplete();
        }
    }
});
