﻿/// <reference path="../k2046.js" />
//客户端心跳包,如果没有收到客户端心跳数据,则表示客户已经关闭当前页面.
K2046.HeartThrob = {
    Init: function(delay) {
        var me = this;
        this.Delay = delay ? (delay * 1000) : (1000 * 60 * 1);
        this.Events = {};
        this.Timer = null;
        //this.Identity = K2046.Guid.NewGuid();
        var xml = new XmlHelper();
        //xml.Append("ID", this.Identity);
        xml.Append("Referrer", document.referrer);
        this.Ajax = new Ajax(function(text, xml) {
            if (text) {
                var data = K2046.JsonParse(text);
                if (data && data.success) {
                    try {
                        me.DoEvent(data);
                    }
                    catch (error) {
                        //alert(error.message);
                        window.status = error.message;
                    }
                    me.CountDown();
                }
                else {
                    //alert(data ? data.msg : "未知错误.");
                    window.status = data ? data.msg : "未知错误";
                }
            }
        });
        //        this.Add("Delay", function(json) {
        //            if (json["Delay"]) {
        //                me.Delay = json["Delay"] * 1000;
        //                clearTimeout(me.Timer);
        //                //me.CountDown();
        //                document.title = me.Delay;
        //                //alert("this.Delay is changed to " + me.Delay);
        //            }
        //        });
        this.Count = 0;
        var self = this;
        new Ajax(function(text) {
            if (text) {
                var c = K2046.JsonParse(text);
                if (c && c.success) {
                    self.Identity = c.Identity;
                    if (K2046.Loaded) {
                        self.Throb();
                    }
                    else {
                        K2046.On(window, "load", function() { self.Throb(); });
                    }
                    //self.Throb();
                    return;
                }
            }
            //alert("建立连接失败!");
            window.status = "建立连接失败!";
        }).DoRequest("/Throb/Heart.aspx?K=I", xml.XmlDocument, "POST");
    },
    Throb: function() {
        var me = this;
        this.Ajax.DoRequest("/Throb/Heart.aspx?K=K", "ID=" + this.Identity + "&Count=" + this.Count, "POST");
        this.Count++;
    },
    CountDown: function() {
        clearTimeout(this.Timer);
        this.Timer = setTimeout(function(o) { return function() { o.Throb(); } } (this), this.Delay);
    },
    Add: function(name, method) {
        this.Events[name] = method;
    },
    DoEvent: function(json) {
        for (var c in this.Events) {
            this.Events[c](json);
        }
    },
    Kill: function() {
        //this.Ajax.Asynchronism = false;//同步执行
        this.Ajax.DoRequest("/Throb/Heart.aspx?K=E", "ID=" + this.Identity, "POST");
    }
};

//K2046.Events.Attach(window, "onload", function() {
//    K2046.HeartThrob.Init(60);
//    K2046.HeartThrob.Add("State", function(json) {
//        document.title = K2046.HeartThrob.Delay + ":" + K2046.HeartThrob.Count;
//    });
//});

K2046.HeartThrob.Init(300);

K2046.Events.Attach(window, "onunload", function() { K2046.HeartThrob.Kill(); });