var swfId = J1616.Const.getConst("crossDomainSwfId");
function JAJAX(timeout) {
    this.id = ++arguments.callee.id || (arguments.callee.id = 0);
    this.timeout = timeout;
};
JAJAX.prototype = {
    type: 'GET',
    data: null,
    open: function(url, id, data) {
        if (data) {
            this.type = 'POST';
            this.data = data;
            this.swfInterface = window[swfId] || document[swfId];
            this.swfInterface = this.swfInterface["1"] || this.swfInterface;
        } else { (this.s = document.createElement("script")).setAttribute("type", "text/javascript");
            this.s.src = url + (url.indexOf("?") == -1 ? "?": "&") + "callback=" + "JAJAX_CALLBACK_" + id + "&__=" + new Date().getTime();
        }
        this.url = url;
        return this;
    },
    send: function(fn) {
        var _this = this,
        IE = "\v" == "v";
        var _timer = setTimeout(function() {
            fn('{code:999}');
            clear();
        },
        this.timeout);
        var call = this.type == 'GET' ? 'JAJAX_CALLBACK_' + this.id: 'RUN_JAJAX_CALLBACK';
        window[call] = function(e) {
            clearTimeout(_timer, fn(e), clear());
        };
        if (this.type == 'GET') {
            var h = document.getElementsByTagName('head')[0];
            h.insertBefore(this.s, h.firstChild);
        } else {
            _this.swfInterface.JS_asRequest(_this.url, _this.data);
        }
        function clear() {
            try {
                if (_this.type == 'GET') {
                    _this.s.parentNode.removeChild(_this.s);
                } else {
                    _this.swfInterface.JS_abort();
                }
            } finally {
                _timer = null;
                IE ? window[call] = null: delete window[call];
            }
        }
    }
};
J1616.IO = {
    U: J1616.Util,
    prefix: "J1616_www_",
    req: "http://my" + J1616.Util.getDomain() + "/get.php",
    upd: "http://my" + J1616.Util.getDomain() + "/update.php",
    cdata: function(k, v) {
        var T = this;
        return typeof v == "undefined" ? T.U.getCookie(k) : T.U.setCookie(k, v, null, null);
    },
    request: function(pos, app, fn) {
        if (pos) {
            this.XHR(this.req + "?name=" + app, fn)
        } else {
            var apps = app.split(",");
            var s = "";
            for (var i = 0,
            p; p = apps[i]; i++) {
                s += this.cdata(this.prefix + p) + "|@|";
            }
            s = s.replace(/\|@\|$/, "");
            fn(s);
        }
    },
    save: function(pos, key, val, fn, post) {
        if (!pos) {
            this.cdata(this.prefix + key, val);
            fn('{code:1}')
        } else {
            if (post) {
                var data = {};
                data[key] = encodeURIComponent(val);
                this.XHR(this.upd, fn, data)
            } else this.XHR(this.upd + "?" + key + "=" + encodeURIComponent(val), fn)
        }
    },
    XHR: function(url, fn, data, timeout) {
        var req = new JAJAX(timeout || 10000);
        req.open(url, req.id, data).send(fn);
    }
};