//#region 桌面消息区域

//w3c 消息区域 还在草案中
Namespace.register("U.MN");

U.MN.nif = window.notificationInstance; //消息总控

//#region


if (window.webkitNotifications) {

    //消息使用
    U.MN._UPS = window.webkitNotifications;

    //函数初始化使用
    U.M.HEPropert(U.MN._UPS, "onshow", function () {
        return this.ondisplay;
    },
    function (UVE) { this.ondisplay = UVE; });


    U.MN.UFN = function (UMT, UDE) {
        var _UCE = U.MN._UPS.createNotification(UDE.icon, UMT, UDE.body);
        _UCE.show(); return _UCE;
    }

    //继承消息
    U.MN.UFN.prototype = window.webkitNotifications;

    U.M.Setprototype(U.MN.UFN, {
        requestPermission: function (UCB) { U.MN._UPS.requestPermission(UCB); },
        permission: function () {
            return { 0: "granted", 1: "denied", 2: "default"}[U.MN._UPS.checkPermission()];
        },
        close: function () { this.cancel(); }
    });
}

//消息初始化
U.MN.Notis = window.Notification = window.Notification || window.webkitNotifications;

//#endregion

//#region

//添加消息提示
U.MN.Noti = function (UDE) {
    return U.MN.Noti.get(UDE);
}

//初始化
U.MN.Noti.get = function (UDE) {
    var _UDE = U.MN.Notis;
    if (_UDE) {
        if (_UDE.permission == "granted") {
            if (UDE) {
                return new U.MN.Noti.init(UDE);
            }
        } //初始化消息
        if (_UDE.permissi0on != "denied") {
            _UDE.requestPermission(function (UTF) {
                if (UTF == "granted") { U.MN.Noti.get(UDE); }
            });
        } //消息
    }
}

//初始化消息
U.MN.Noti.init = function (UDE) {
    this.UNM; this.UT = UDE; this.UE = UDE.e; this.time = UDE.t || 0;
}

U.MN.Noti.checkPermission = function () {

}

//显示所有消息
U.MN.Noti.show = function () {
    try { U.MN.nif.show() }
    catch (e) { return e; }
}

//清除消息
U.MN.Noti.cancel = function () {
    try { U.MN.nif.cancel() }
    catch (e) { return e; }
}

//消息功能区域
U.MN.Noti.init.prototype = {
    create: function (UMT, UDE, USE) { //创建消息 body  dir icon lang onlcick onclose onerrer onshow slient tag title  data  noscreen renotify sound vibrate
        var _UME, _UME, _UTF = UDE == "string", Noti = U.MN.Notis; this.UE = USE = USE ? USE : this.UE;
        if (Noti.permission == "granted") {
            _UME = this.UNM = new Noti(UMT, UDE);
            (USE) && (this.event(USE)); (this.time) && (this.timerc(this.time));
        } //允许消息传递
        else if (Noti.permission != "denied") { Noti.requestPermission(U.M.apply(this, [[this.Permission, [UMT, UDE]]])); } //认证允许
        else { U.Alert("请启动消息推送!"); }
    },
    Permission: function (UMT, UDE, UTF) {//认证成功后
        if (UTF == "granted") { this.create(UMT, UDE); }
    },
    set: function (UDE) { //消息设置
        U.Ut.AddObj(this.UNM, UDE);
    },
    bind: function (UDE, UAE) { //事件绑定
        $(this.UNM).bind(UDE, UAE);
    },
    unbind: function () {//取消事件绑定
        $(this.UNM).unbind(UDE, UAE);
    },
    event: function (UDE) { //初始化消息
        var i, _UND = this.UNM, _UDE = { oc: "onclick", ocl: "onclose", oe: "onerror", os: "onshow" };
        for (i in UDE) { _UND[i] = _UND[_UDE[i]] = U.M.apply(this, [[this.aevent, [i, _UDE[i]]]]); }
    },
    aevent: function (UTF, UTP) {//事件统一出口
        var _UDE = this.UE;
        if (_UDE && _UDE[UTF]) {
            if (U.Ut.isFunction(_UDE[UTF])) {
                _UDE[UTF]();
            }
            else { U.M.apply(this, _UDE[UTF])(); }
            if (UTF == "oc" || UTF == "onclick") {//执行函数
                this.close();
            }
        }
    },
    close: function () {//关闭
        if (this.UNM) {
            this.UNM.close();
            this.dispose();
        }
    },
    dispose: function () { this.UT = this.UE = this.UNM = null; },
    timerc: function (UT, UFE) { //指定事件关闭消息
        setTimeout(U.M.apply(this, [[UFE], [this.close]]), UT);
    },
    show: function () { U.MN.show(); },
    cancel: function () { U.MN.cancel(); }
}

//#endregion

//#region

//IFrame之间的消息通知。全兼容解决方案,太复杂,需要精简版本。


// 页面消息传递,解决跨域问题。
U.MN.message = function (UDE) {
    return new U.MN.message.get.init(UDE);
}

U.MN.message._UTF = ("onmessage" in window);


U.MN.message._UFS = function (UDE) {
    try {
        var _UME, _USE = "";
        if (U.MN.message._UTF) { _USE = UDE.data; }
        else { _USE = UDE || window.name; }  //设置
        _USE = _USE.parseJSON();
        _UME = U.MN.message.get.func[_USE[1]];
        (_UME) && (_UME.message[_USE[0], _USE[2], _USE[3]]);
    } catch (e) { }
};

if (U.MN.message._UTF) {
    U.M.AddEvent("message", window, U.MN.message._UFS);
}

U.MN.message.get = U.MN.message.prototype = {
    func: {},
    ZS: 1,
    init: function (UDE) { //初始化消息
        U.Ut.AddObj(this, UDE);
        this.id = this.id || Guid.guidNoDash();
        this.func[this.id] = this;
        if (!U.MN.message._UTF) {
            if (this.Sender) {
                var _USE = this.obj;
                (U.Ut.isString(_USE)) && (_USE = $(_USE)[0]);
                this.url = U.M.getUrl(_USE.src).host;
                this.obj = $$("iframe", { style: { display: "none" }, width: "0", height: "0" }, document.body);
            }
            else { $().ready(U.M.apply(this, U.MN.message._UFS)); }
        }
    },
    message: function (UDE) {//接收消息
        (U.Ut.isFunction(this.me)) && (this.me(UDE));
        if (this.Sender && !U.MN.message._UTF) {
            var _UD = this.obj.contentWindow.document;
            _UD.open();
            _UD.write("");
            _UD.close();
            $(this.obj).remove();
        } //释放
    },
    post: function (UDE, UDID, UCE) {//提交消息
        var _UKE, _USE = this.obj;
        UDE = U.MS.jsonToStr[UDE, UDID || "", this.id, UCE];
        (U.Ut.isString(_USE)) && (_USE = $(_USE)[0]);
        if (U.MN.message._UTF) {
            try { _USE = _USE.contentWindow || _USE; }
            catch (e) { _USE = this.obj; } _USE.postMessage(UDE, "*");
        }
        else {
            if (this.url) {
                _USE.src = "about:blank";
                U.MD.IframeLoad(_USE, [[U.M.apply(this, [[this.ie, [_USE, UDE]]])]]);
            }
            else { window.name = UDE; window.location = this.lurl; }
        } //消息提示
    },
    ie: function (USE, UDE) { USE.contentWindow.name = UDE; USE.contentWindow.location.href = this.url; }
}

U.MN.message.get.init.prototype = U.MN.message.get;
U.MN.message.cb = U.MN.message._UFS;

//#endregion