﻿var lightbox = new Class({
    _parentContainerId: "container",
    _call: function (e) {
        switch (e.a) {
            case "open":
                this._open(e);
                break;
        }
    },
    initialize: function () {
        site.addEvent("lightbox", function (e) { this._call(e); } .bind(this));
    },
    _init: function (e) {
        var container = $(e.c);
        var lightBoxContainer = new Element("div", { "class": "lightboxFullHeight", styles: { "opacity": 0} });
        container.store("lightBoxContainer", lightBoxContainer);
        var contentContainer = new Element("div", { "class": "lightboxContent", events: { "click": function (ev) { ev.stopPropagation(); } } });
        $(this._parentContainerId).adopt(
            lightBoxContainer.adopt(
                new Element("div", { "class": "lightboxTable", events: { "click": function (ev) { this._close(e) } .bind(this)} }).adopt(
                    new Element("div", { "class": "lightboxCell", events: { "click": function (ev) { this._close(e) } .bind(this)} }).adopt(
                            contentContainer
                    )
                )
            )
        )
        new Request.HTML({ "url": "/ajax/page.aspx?lang="+site.lang+"&lbl=" + e.id, "update": contentContainer }).get();
    },
    _open: function (e) {
        var container = $(e.c);
        if (!container.retrieve("lightbox")) {
            this._init(e);
            var fx = new Fx.Tween(container.retrieve("lightBoxContainer"), { property: "opacity" });
            container.store("lightbox", fx);
            fx.start(1);
        }
        else {
            var fx = container.retrieve("lightbox");
            fx.cancel();
            fx.start(1);
        }

    },
    _close: function (e) {
        var container = $(e.c);
        container.retrieve("lightbox").start(0).chain(function () { container.retrieve("lightBoxContainer").destroy(); container.store("lightBoxContainer", false); container.store("lightbox", false); });
    }
});
