Type.registerNamespace('CarInfo');

CarInfo.Box = function(id, te, de, tout) {
    this._te = document.getElementById(te);
    this._de = document.getElementById(de);
    this.box[id] = this;
    this.setTimeout(tout);
}
CarInfo.Box.prototype = {
    box: {},
    time: 0,
    _res: 1000,
    _text: "",
    _color: "#FF0000",
    _delay: 120000,
    _poll: 5,
    _polling: false,
    _timeout: 10,
    _usn: 0,
    _tick: function(span) {
        this._timeout -= span;
        if (this._timeout <= this._delay) {
            if (this._delay == 0) {
                this._timeout = 0;
                if (typeof (this._onExceed) == 'function') this._onExceed()
            } else {
                this._delay = 0;
                this._te.innerHTML = this._text;
                this._de.style.color = this._color
            }
        }
        if (this._delay == 0) {
            var tm = Math.floor(this._timeout / 1000), mm = Math.floor(tm / 60), ss = tm % 60;
            this._de.innerHTML = (mm < 10 ? "0" + mm : mm) + ":" + (ss < 10 ? "0" + ss : ss)
        }
    },
    _update: function(res, ctx) {
        var inf, box, val, boxes = this.box;
        for (var i in boxes) {
            box = boxes[i]; inf = res[parseInt(i)];
            if (inf == undefined) continue;
            if (!isNaN(val = parseInt(inf["timeout"])))
                box._timeout = val;
        }
        if (!isNaN(val = parseInt(res["usn"]))) {
            if (val - this._usn >= 10 && typeof (this._onExceed) == 'function')
                this._onExceed();
            this._usn = val;
        }
        this._polling = false;
    },
    _iofail: function(result, context) {
        this._polling = false;
        window.status = "Communication failure: " + result.toString()
    },
    _onInterval: function() {
        if (!this._polling) {
            var poll = this._poll;
            if (this._delay == 0)
                poll *= 10;
            if (Math.floor(this.time / 1000) % poll == 0) {
                var r = Function.createDelegate(this, this._update);
                var e = Function.createDelegate(this, this._iofail);
                var rq = Sys.Net.WebServiceProxy.invoke(this._svc, this._mth, false, { usn: this._usn }, r, e, this);
                this._polling = true;
            }
        }
        var time = (new Date()).getTime(), span = time - this.time, cnt = this.box;
        for (var i in cnt) {
            var c = cnt[i];
            if (c._timeout > 0)
                c._tick(span);
        }
        this.time = time
    },
    dispose: function() {
        if (this.timer != undefined) {
            clearInterval(this.timer);
            delete this["timer"]
        }
    },
    initialize: function(text, color, delay, res, svc, mth, usn) {
        this._text = text;
        this._color = color;
        this._delay = delay;
        this._svc = svc;
        this._mth = mth;
        this._usn = usn;
        if (this._res != res) { this.dispose(); this._res = res; }
        if (this.timer == undefined) {
            var _this = this;
            this.time = (new Date()).getTime();
            this.timer = setInterval(function() { _this._onInterval(); }, this._res)
        }
    },
    setTimeout: function(timeout) {
        this._timeout = timeout;
    },
    setText: function(text) { this._text = text },
    setOnExceed: function(hndr) { this._onExceed = hndr }
}

