﻿var detail = new Class({
    _current: false,
    _initialized: [],
    _call: function (e) {
        switch (e.a) {
            case "open":
                this._openMenu(e);
                break;
            case "close":
                this._closeCurrent();
                break;
        }
    },
    initialize: function () {
        site.addEvent("detail", function (e) { this._call(e); } .bind(this));
        window.addEvent("resize", this._windowResized.bind(this));
        $("menu").addEvent("click", this._closeCurrent.bind(this));
    },
    _openMenu: function (e) {
        this._closeCurrent();
        if (!$(e.i))
            return;
        if (!this._initialized.contains(e.i))
            this._initMenu(e);
        $(e.c).setStyle("display", "block");
        $(e.i).addClass("active");
        this._current = e;
    },
    _windowResized: function () {
        this._closeCurrent();
        this._initialized = [];
    },
    _closeCurrent: function () {
        if (this._current)
            this._closeMenu(this._current);
    },
    _closeMenu: function (e) {
        $(e.c).setStyle("display", "none");
        $(e.i).removeClass("active");
    },
    _initMenu: function (e) {
        $(e.c)
            .setStyles({ "display": "block", "visibility": "hidden" })
            .addEvent("click", this._closeMenu.pass(e, this))
            .getElement("div.popWarp").position({
                "relativeTo": $(e.i),
                "edge": "centerTop",
                "position": "centerBottom",
                offset: { x: 30, y: 12 }
            })
            .addEvent("click", function (e) { e.stopPropagation(); });
        $(e.c).setStyles({ "display": "none", "visibility": "visible" });
        this._initialized.push(e.i)
    }
});
