﻿window.LiveCom = new (function() {
    document.domain = window.location.host.split(".")[1] + ".com";
    var onloaded = function() {
        var iFrameObj = document.createElement('IFRAME');
        iFrameObj.src = "http://livecom." + document.domain + "/xhr";
        iFrameObj.style.width = "1px";
        iFrameObj.style.height = "1px";
        iFrameObj.style.visibility = "hidden";
        document.body.appendChild(iFrameObj);
    };


    if (document.addEventListener) {
        window.addEventListener('load', onloaded, false); //W3C
    }
    else {
        window.attachEvent('onload', onloaded); //IE
    }


    this.xhr_request_func = null;
    function xhr_request(url, data, method, onsuccess, onerror) {
        if (LiveCom.xhr_request_func == null) {
            setTimeout(function() {
                xhr_request(url, data, method, onsuccess, onerror);
            }, 300);
            return;
        }

        LiveCom.xhr_request_func("http://livecom." + document.domain + "/" + url, data, method, onsuccess, onerror);
    }

    var self = this;
    this.set_flash = function(owner) {
        self.owner = owner;
    };
    this.request = function(url, data, method, call_id) {
        var on_success = function(r) {
			try
			{
				self.owner.LiveCom_XHR_success(call_id, r);
			}
			catch (e) {};
        };

        var on_error = function() {
			try
			{
				self.owner.LiveCom_XHR_error(call_id);
			}
			catch (e) {};
        };

        if (data != null) {
            while (data.indexOf("$#$") >= 0) {
                data = data.replace("$#$", "\\");
            }
        }
        xhr_request(url, data, method, on_success, on_error);
    };
})();
