TESCO.UI.Flyout.Modal = (function() {

    var FLYOUT = TESCO.UI.Flyout;
    var NODE = TESCO.system.DOM.node;

    TESCO.system.event.attach(FLYOUT.rect, "click",
		function(e) {
		    if (NODE.hasClassName(e.target, "close")) {
		        FLYOUT.hide();
		        e.stopEvent();
		    }
		}
	);

    TESCO.system.event.manager.call(_constructor.event = {}, "loaded");

    //  constructor
    function _constructor(container, x, y, className) {
        var _modal = this;
        TESCO.system.event.attach(container, "click",
            function(e) {
                //  elements with flyout class
                var _actualTarget = NODE.getSelfOrAncestor(e.target, null, className || "flyout", 3);
                if (_actualTarget) {
                    var _id = _actualTarget.id;
                    if (_id) {
                        var _key = _id.slice(_id.indexOf("-") + 1, _id.length);
                        if (_key !== FLYOUT.key) {
                            FLYOUT.key = _key;
                            FLYOUT.Transient.stop();
                            _modal.load({
                                "target": e.target,
                                "type": _id.slice(0, _id.indexOf("-"))
                            }, FLYOUT.key);
                            e.stopEvent();
                        } else {
                            e.prevent();
                        }
                    } else {
                        throw new Error("TESCO.UI.Flyout.Modal: '" + e.target + "' id attribute is required");
                    }
                }
            }
        );
        this.position = {
            "x": x || FLYOUT.position.x.left,
            "y": y || FLYOUT.position.y.bottom
        }
        return this;
    }
    TESCO.sites.UI.Flyout.extend(_constructor);

    //  public default

    _constructor.prototype.NAME = "TESCO.UI.Flyout.Modal";

    _constructor.prototype.loaded = function() {
        _constructor.event.dispatchEvent("loaded", this);
    }

    //  return constructor as function pointer
    return _constructor;
})();