
(function () {
    var _XM_TID = '%{XM_TID}';
    var _XM_TRACE_ELAPSE = '%{XM_TRACE_ELAPSE}';
    var _XM_CLIENT_API = '_XM_CLIENT_API%{XM_CLIENT_API}';
	var _XM_CLIENT_ADDR = '%{XM_CLIENT_ADDR}';
    var _XM_TID_HEADER = 'X-Xm-Tid';
    var _XM_TRACE_ELAPSE_HEADER = 'X-Xm-ElapseTime';


    var openProxied = window.XMLHttpRequest.prototype.open;
    window.XMLHttpRequest.prototype.open = function () {
        if (arguments[1].indexOf(_XM_CLIENT_API) == -1) {
            if (document.removeEventListener) {
                this.removeEventListener("loadstart", _xm_loadstart);
                this.removeEventListener("loadend", _xm_loadend);
            }
            else {
                this.detachEvent("loadstart", _xm_loadstart);
                this.detachEvent("loadend", _xm_loadend);
            }
            if (this.addEventListener) {
                this.addEventListener("loadstart", _xm_loadstart);
                this.addEventListener("loadend", _xm_loadend);
            }
            else if (this.attachEvent) {
                this.attachEvent("onloadstart", _xm_loadstart);
                this.attachEvent("onloadend", _xm_loadend);
            }
        }
        return openProxied.apply(this, [].slice.call(arguments));
    };

    var _xm_loadstart = function () {
        var eventProxied = this.onreadystatechange;
        this.onreadystatechange = function () {
            if (this.readyState === XMLHttpRequest.DONE) {
                this._xm_clientinfo.es = Date.now();
            }

            if (eventProxied != null && typeof eventProxied != 'undefined') {
                eventProxied.apply(this, [].slice.call(arguments));
            }

            if (this.readyState === XMLHttpRequest.DONE) {
                this._xm_clientinfo.ee = Date.now();
            }
        };
        this._xm_clientinfo = __xm_create_clientinfo();
        this._xm_clientinfo.ss = Date.now();
        this._xm_clientinfo.se = Date.now();
    };

    var _xm_loadend = function () {
        if (typeof this._xm_clientinfo == 'undefined') return;
        if (this._xm_clientinfo.es == 0) {
            this._xm_clientinfo.es = Date.now();
            this._xm_clientinfo.ee = Date.now();
        }
        this._xm_clientinfo.tid = this.getResponseHeader(_XM_TID_HEADER);
        var traceElapseTime = this.getResponseHeader(_XM_TRACE_ELAPSE_HEADER);

        if (typeof traceElapseTime == 'undefined' || typeof this._xm_clientinfo.tid == 'undefined') {
            return;
        }

        if (traceElapseTime > this._xm_clientinfo.ee - this._xm_clientinfo.ss) {
            return;
        }

        var _xm_clientinfo = this._xm_clientinfo;
        __xm_send_post(_xm_clientinfo);
    };

    function __xm_create_clientinfo() {
        var clientinfo = {};
        clientinfo.tid = 0;
        clientinfo.ss = 0;
        clientinfo.se = 0;
        clientinfo.es = 0;
        clientinfo.ee = 0;
        return clientinfo;
    }

    function __xm_send_post(clientinfo) {
        try{
            var queryParameters = 'tid=' + clientinfo.tid + '&ss=' + clientinfo.ss + "&se=" + clientinfo.se;
            queryParameters += '&es=' + clientinfo.es + "&ee=" + clientinfo.ee;

            var xmlhttp;
            if (window.XMLHttpRequest) {
                xmlhttp = new XMLHttpRequest();
            } else {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
			 
			if(_XM_CLIENT_ADDR == "") {
                xmlhttp.open('GET', './'+_XM_CLIENT_API+'?' + queryParameters, true);
			}else{
                xmlhttp.open('GET', 'http://' + _XM_CLIENT_ADDR + '/' + _XM_CLIENT_API + '?' + queryParameters, true);
			}
           	
            xmlhttp.send(null);

            if (true) {
                var sendElapseTime = clientinfo.se - clientinfo.ss;
                var recvElapseTime = clientinfo.ee - clientinfo.es;
                var serverElapseTime = clientinfo.es - clientinfo.se;

                console.log('__xm_send_post serverElapseTime=' + serverElapseTime + ', sendElapseTime=' + sendElapseTime + ', recvElapseTime=' + recvElapseTime);
            }
        }
        catch(e) {
			console.log(e);
        }
    }

    var _xm_window_load = function () {
        //return;
        setTimeout(function () {
            var timing = window.performance.timing;
            var clientinfo = __xm_create_clientinfo();
            clientinfo.ss = timing.navigationStart;
            clientinfo.se = timing.connectEnd;
            clientinfo.es = timing.responseEnd;
            clientinfo.ee = timing.loadEventEnd;
            clientinfo.tid = _XM_TID;

            if (_XM_TRACE_ELAPSE > clientinfo.ee - clientinfo.ss) {
                return;
            }

            __xm_send_post(clientinfo);
        }, 0);
    };

    if (!window.performance || !window.performance.timing) {
        return;
    }

    if (this.addEventListener) {
        this.addEventListener("load", _xm_window_load);
    }
    else if (this.attachEvent) {
        this.attachEvent("onload", _xm_window_load);
    }
})();
