lite_api = new Object();
lite_api._init = 0;
lite_api.init = function() {
    var m = dojo.byId("lite_main");
    if (!m) return false;
    var self = lite_api;
    if (self._init) return true;
    self.main = m;
    self.iframe = undefined;
    self.url = '';
    self.h = 0;
    self.count = 0;
    self.timer = undefined;
    self._init = 1;
    self.callback();
}
lite_api.callback = function() {
    if (!this._init) return false;
    var h = this.main.clientHeight;
    if (!h) return false;
    h+=40;
    if (Math.abs(h-this.h) > 30) {
        this.count++;
        this._xhr(h);
        this.timer = setTimeout("lite_api.callback()", 3000);
    }
}
lite_api._xhr = function(h) {
    var self = this;
    dojo.xhrGet({
        url: "/lite_api/xhr",
        load: function(r, ioArgs){
            if (r && r.indexOf("__XHR__") != -1) {
                var i = r.indexOf("__XHR__");
                var j = r.indexOf("__OK__");
                if (i>=0 && j>(i+7)) {
                    i+=7;
                    var count = r.substring(i,j);
                    if (count == self.count) {
                        self.h = h;
                        if (self.timer) {
                            clearTimeout(self.timer);
                            self.timer = undefined;
                        }
                        i = r.indexOf("__URL__");
                        j = r.indexOf("__END__");
                        if (i>0 && j>(i+7)) {
                            i+=7;
                            var url = r.substring(i,j);
                            self.url = unescape(url);
                            self._cookie();
                        }
                    }
                }
            }
            return r;
        },
        error: function(r, ioArgs){
            return r;
        },
        content: {c: self.count, h: h, t: new Date().getTime()}
    });
}
lite_api._cookie = function() {
    if (this.main && this.iframe && this.iframe.parentNode) {
        this.main.removeChild(this.iframe);
        this.iframe = undefined;
    }
    if (!this.url) return false;
    var i = document.createElement("iframe");
    var url = this.url;
    if (url.match(/\?/)) {
        url += "&ts_noCache="+Math.random();
    } else {
        url += "?ts_noCache="+Math.random();
    }
    i.src = url;
    i.width = "30px";
    i.height = "20px";
    i.marginwidth = "0";
    i.marginheight = "0";
    i.vspace = "0";
    i.hspace = "0";
    i.frameborder = "0";
    i.allowtransparency = "true";
    i.scrolling = "no";
    i.style.border = "none";
    i.style.position = "absolute";
    i.style.top = "-50px";
    this.main.appendChild(i);
    this.iframe = i;
}
dojo.addOnLoad(lite_api.init);

