
var Namespace = new function() {

    function TimeService() {
        this.UPDATE_RATE = 100;
        this.da_diff = 0;
        this.da_tz_diff = 0;
        this.da_resync_time = 0;
        this.time_synced = false;
        this.listeners = [];
        this.should_stop = false;
    }

    TimeService.prototype.init = function() {
    }

    TimeService.prototype.start = function() {
        this.resync_time();
    }

    TimeService.prototype.stop = function() {
        this.should_stop = true;
    }

    TimeService.prototype.add_listener = function(listener) {
        this.listeners.push(listener);
    }

    TimeService.prototype.remove_listener = function(listener) {
        var index = this.listeners.indexOf(listener);
        this.listeners.splice(index, 1);
    }

    TimeService.prototype.update_values = function() {
        var date = new Date();
        var curtime = date.getTime();
        date.setTime(curtime - this.da_diff + this.da_tz_diff);
        this.ticked_date = date;
        //nakresli
        for (var i=0; i<this.listeners.length; i++) {
            this.listeners[i].timeservice_callback(this);
        }
    }

    TimeService.prototype.tick_clock = function() {
        if (this.should_stop) {
            this.should_stop = false;
            return;
        }
        var self = this;
        this.update_values();
        var date = new Date();
        var curtime = date.getTime();
        if (curtime >= this.da_resync_time) {
            this.resync_time();
            setTimeout(function() { self.tick_clock_while_updating(); }, this.UPDATE_RATE);
            return;
        }
        setTimeout(function() { self.tick_clock(); }, this.UPDATE_RATE);
    }

    TimeService.prototype.tick_clock_while_updating = function() {
        if (this.should_stop) {
            this.should_stop = false;
            return;
        }
        if (this.time_synced) return;
        var self = this;
        this.update_values();
        setTimeout(function() { self.tick_clock_while_updating(); }, this.UPDATE_RATE);
    }

    TimeService.prototype.start_clock = function(milis) {
        var date = new Date();
        this.da_diff = date.getTime() - milis;
        var self = this;
        setTimeout(function() { self.tick_clock(); }, 0);
    }

    TimeService.prototype.received = function(req) {
        var response = req.xmlhttp.responseXML.documentElement;
        var milis = parseInt(response.getElementsByTagName('milis')[0].firstChild.data);
        var resync = parseInt(response.getElementsByTagName('resync')[0].firstChild.data);
        date = new Date();
        this.da_resync_time = date.getTime() + (resync * 1000);
        this.da_tz_diff = 0;
        this.time_synced = true;
        //milis = 1258844392440;  // 21.11.2009 23:59:52 CET
        this.start_clock(milis);
    }

    TimeService.prototype.resync_time = function() {
        this.time_synced = false;
        var conn = new DaConn();
        var req = new DaRequest();
        req.url = "/cas";
        req.func_done = callback(this.received, {bind: this});
        conn.connect(req);
    }

    return {
        TimeService: TimeService
    }
}

var TimeService = Namespace.TimeService;

