// create namespace
Core.createNamespace('nl.code.lightbox');

/**
 * AjaxLightbox Class
 */
nl.code.lightbox.AjaxLightbox = new Class({
    /**
     * @extends nl.code.lightbox.Lightbox
     */
    Extends: nl.code.lightbox.Lightbox,

    /**
     * Constructor
     *
     * @param integer, the identifier
     */
    initialize: function(id, options) {
        var default_options = {
            css_class: 'js-ajax-lightbox',
            overlay_opacity: 0.4,
            overlay_color: '#000000',
            show_loading: true,
            animation_class: LightboxAnimation,
            autostart_animation: true,
            autostart_animation_width: 200,
            autostart_animation_height: 200
        };
        options = $merge(default_options, options);

        this.parent(id, options);
    },

    /**
     * Preload the lightbox
     *
     * @param Object, {width, height, content, trigger}
     * @return void
     */
    preload: function(data) {
        this.parent(data);

        // ajax lightbox preloadig
        if (this.options.autostart_animation && this.animation && !this.canvas.getSize().x) {
            data.content = '&nbsp;';
            if (this.options.show_loading) {
                data.content = '<div class="js-loading"></div>';
            }

            data.width  = this.options.autostart_animation_width;
            data.height = this.options.autostart_animation_height;

            this.show(data);
        }
    },

    /**
     * Hide the lightbox
     *
     * @return void
     */
    hide: function() {
        if (this.data.trigger.request) {
            this.data.trigger.request.cancel();
        }

        this.parent();
    }
});
/**
 * @param Element
 * @param Object, options
 * @return void
 */
nl.code.lightbox.AjaxLightbox.createTrigger = function(anchor, options) {
    anchor.addEvent('click', function(event) {
        event.stop();
        this.blur();

        var url         = nl.code.pager.Uri.getAnchorUrl(anchor);
        var lightbox_id = url.replace('/', '-');

        this.request = nl.code.pager.PageData.request(url, nl.code.lightbox.Lightboxer, 'lightbox_id='+ lightbox_id, anchor);

        nl.code.lightbox.Lightboxer.openLightbox(
            lightbox_id,
            0,
            0,
            '',
            anchor,
            nl.code.lightbox.AjaxLightbox,
            options
        );
    });
};